其他分享
首页 > 其他分享> > CLIST

CLIST

作者:互联网

原文链接:http://www.cnblogs.com/fengbo/archive/2012/08/22/2651692.html

                                  CList数组再写

 

#include<iostream>

using namespace std;

 

struct node{

int data;

node* next;

};

 

void insertnode(node* list,int num);

void deletenode(node* list,int num);

 

int main() {

int a[5]={3,5,8,1,2};

node* head=new node;//开辟一个空间地址给头指针

node* p=head;//p指向头结点

for(int i=0;i<5;i++)//顺序建立单链表

{

p->next=new node;

p=p->next;

p->data=a[i];

p->next=NULL;

}

 

insertnode(head->next,10);

deletenode(head,6);

p=head->next;

while(p)

{

cout<<p->data<<" ";

p=p->next;

}

cout<<endl;

return 0;

}


 

void insertnode(node* list,int num) {

if(!list)//空链表

{

list=new node;

list->data=num;

list->next=0;

return ;

}

node* r=new node;//待插入结点

r->data=num;

if(list->data>num)//在第一个结点插入

{

r->next=list;

list=r;

return;

}

node* p=list->next,*q=list;

while(p&&p->data<num)

{ q=p;

p=p->next;

}

r->next=p; q->next=r; return;

}


 

void deletenode(node* list,int num) {

if(!list)//空链表则删除

return;

node* p=list->next,*q;

if(p->data==num) {

list->next=p->next; d

elete p;

return;

}

while(p&&p->data!=num)

{

q=p;

p=p->next;

}

if(!p)

return;

q->next=p->next;

delete p;

}

转载于:https://www.cnblogs.com/fengbo/archive/2012/08/22/2651692.html

标签:node,int,list,next,CLIST,num,data
来源: https://blog.csdn.net/weixin_30794499/article/details/98989296