CLIST

                                  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;

}

posted @ 2012-08-22 23:23  Alan Perlis  阅读(222)  评论(0编辑  收藏  举报