链表操作
#include#include #include #include using namespace std;typedef struct student{ int data; struct student *next;}node;/* 链表的创建*/node *creat(){ node *head,*p,*s; int x; int cycle=1; head=(node*)malloc(sizeof(node)); p=head; while(cycle) { cout<<"please enter a number"< >x; if(x!=0) { s=(node*)malloc(sizeof(node)); s->data=x; p->next=s; p=s; } else cycle=0; } head=head->next; p->next=NULL; return (head);}/*链表的测长*/ int length(node *head){ int n=0; node *p; p=head; while(p!=NULL) { p=p->next; n++; } return n;} /*链表的排序*/ node *sort(node* head, int n){ int i; int j; int temp; node *p; if(head==NULL||head->next==NULL) return (head); p=head; for(i=1;i data>p->next->data) { temp=p->next->data; p->next->data=p->data; p->data=temp; } p=p->next; } } return (head);} /*链表的逆置*/ node *reverse(node* head){ node *p1; node *p2; node *p3; if(head==NULL||head->next==NULL) return head; p1=head; p2=p1->next; while(p2) { p3=p2->next; p2->next=p1; p1=p2; p2=p3; } head->next=NULL; head=p1; return head;} /*链表插入一个节点*/ node *insert(node* head,int num){ node *p0,*p1,*p2; p1=head; p0=(node*)malloc(sizeof(node)); p0->data=num; while((p0->data)>(p1->data)&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(p0->data<=p1->data) { if(head==p1) //插入的是头结点 { p0->next=p1; head=p0; } else { p2->next=p0; //插入的是中间结点 p0->next=p1; } } else //插入的是尾结点 { p1->next=p0; p0->next=NULL; }return (head);} /*链表删除一个节点*/ node *del(node* head,int num){ node *p1,*p2; p1=head; while(num!=p1->data&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->data) { if(head==p1) { head=p1->next; free(p1); } else p2->next=p1->next; }else cout< <<"couldn't be found"< data<<" "; p=p->next; } cout< data<<" "; p=p->next; } cout< data<<" "; p=p->next; } cout< >a; head=insert(head,a); p=head; cout<<"插入后链表是:"; if(head!=NULL) //插入后链表的打印 while(p!=NULL) { cout< data<<" "; p=p->next; } cout< >b; head=del(head,b); p=head; cout<<"删除后链表是:"; if(head!=NULL) //删除后链表的打印 while(p!=NULL) { cout< data<<" "; p=p->next; } cout<