文章
2
粉丝
0
获赞
2
访问
163
#include<iostream>
using namespace std;
// 链表结点类
class ListNode{
public:
int val;
ListNode* next;
ListNode(){
val=0;
next=NULL;
}
ListNode(int x):val(x){
next=NULL;
}
};
// 升序链表
class DelinkList{
private:
// 头结点
ListNode* head;
public:
// 默认初始化头结点
DelinkList(){
head=new ListNode();
}
void add(int val){
ListNode* node=new ListNode(val);
ListNode* cur=head;
ListNode* next=cur->next;
while(next!=NULL){
// 保证升序
if(next->val>=val)
break;
cur=cur->next;
next=cur->next;
}
// 保证链表依旧连接
cur->next=node;
node->next=next;
}
void print(){
ListNode* cur=head->next;
while(cur!=NULL){
cout<<cur->val<<" ";
cur=cur->next;
}
cout<<"\n";
}
};
// 面向对象程序设计思想
int main(){
DelinkList list;
int n;
cin>>n;
while(n--){
int temp;
cin>>temp;
list.add(temp);
...
登录后发布评论
暂无评论,来抢沙发