数据结构---链表

来源:互联网 发布:flyme系统依赖网络 编辑:程序博客网 时间:2024/06/09 14:00
typedef struct node{int data;struct node *next;}*Linklist,Lnode;

尾插法建链表:

Linklist creat_list_tail(){Linklist Head;Head=(Linklist)malloc(sizeof(Lnode));Head->next=NULL;Lnode *r,*p;r=Head;int x;scanf("%d",&x);while(x!=-1){p=(Linklist)malloc(sizeof(Lnode));p->data=x;p->next=r->next;r->next=p;r=p;scanf("%d",&x);}return Head;}

头插法建链表:

Linklist creat_list_head(){Linklist Head;Lnode *p;Head=(Linklist)malloc(sizeof(Lnode));Head->next=NULL;int x;scanf("%d",&x);while(x!=-1){p=(Linklist)malloc(sizeof(Lnode));p->data=x;p->next=Head->next;Head->next=p;scanf("%d",&x);}return Head;}
删除链表重复值:

void pur_list(Linklist Head){Lnode *p,*q,*r;p=Head->next;if(p!=NULL)while(p){q=p;while(q->next){if(q->next->data==p->data){r=q->next;;q->next=r->next;free(r);}elseq=q->next;}p=p->next;}}
链表插入排序:

void insert_operation(Linklist head,Lnode *temp){Lnode *prev,*cur;//第一个数据前插入 tempif(temp->data< head->data){temp->next=head;//此处的head是第一个数据 head=temp;return ; }  cur=head; while(cur) { if(temp->data < cur->data) break;//找到插入位置,大于prev 小于prev->next  插入到prev之后,prev->next之前  prev=cur; cur=cur->next; } temp->next=prev->next;//  插入到prev之后,prev->next之前  prev->next=temp; return ;}void sort_list(Linklist Head)//插入法排序 {Lnode *prev,*curr,*head;head=Head->next;if(Head==NULL)return ;curr=head->next;head->next=NULL;while(curr){prev=curr;curr=curr->next;insert_operation(head,prev);//寻找prev的插入位置 }return ;}

链表倒置:

void Reverse(Linklist Head)//链表的倒置 {Lnode *p,*q;p=Head->next;Head->next=NULL;while(p){q=p;p=p->next;q->next=Head->next;Head->next=q;}}




0 0