#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }Lnode,*LinkList; //creat a linklist and insert some new node at the begain of linklist LinkList Creat_LinkList1(LinkList L, int n) { Lnode *s; int x; while(n) { printf("input a data:"); scanf("%d",&x); s = malloc(sizeof(Lnode)); s->data = x; s->next = L; L = s; n--; } return L; } //creat a linklist and insert some new node at the end of linklist LinkList Creat_LinkList2(LinkList L2, int n) { Lnode *s,*r; int x; r=L2; if(L2!=NULL) { while(r->next) { r = r->next; } } //if L2 is NULL then set the L2 point to the new node if(!r) { printf("input a data:"); scanf("%d",&x); s=malloc(sizeof(Lnode)); s->data = x; s->next = NULL; L2=s; r=s; n--; } //if L2 is not NULL set the new node at the end of L2 while(n) { printf("input a data:"); scanf("%d",&x); s=malloc(sizeof(Lnode)); s->data = x; s->next = NULL; r->next = s; r = s; n--; } //Display_LinkList(L2); return L2; } void Display_LinkList(LinkList L) { Lnode *p=NULL; p=L; if(!p) { printf("the LinkList is NULL!!/n"); return ; } printf("the LinkList is:/n"); while(p) { printf("->%d",p->data); p = p->next ; } printf("/n"); } int Length_LinkList(LinkList L) { int count=0; Lnode *p=L; while(p) { count++; p = p->next; } return count; } Lnode *Locate_LinkList(LinkList L, int x) { Lnode *p=L; while(p) { if(p->data == x) return p; p = p->next; } return NULL; } LinkList Insert_LinkList(LinkList L, int a) { Lnode *new,*p=L; Lnode *s=p; if(!Locate_LinkList(L,a)) { printf("not find %d /n",a); return L; } new = malloc(sizeof(Lnode)); printf("input a data you want to insert:/n"); scanf("%d",&(new->data)); new->next = NULL; if(L==NULL) { L=new; return L; } while(p) { if((p->data) == a) break; s = p; p = p->next; } new->next = s->next; s->next = new; return L; } LinkList Reverse_LinkList(LinkList L) { Lnode *p=L,*q=L; L=NULL; while(p) { q = p; p = p->next; q->next = L; L = q; } return L; } LinkList Select_Sort(LinkList L) { Lnode *p=NULL,*q=NULL; int temp; p = L; while(p->next) { q = p->next; while(q) { if((p->data) < (q->data)) { temp = p->data; p->data = q->data; q->data = temp; } q = q->next; } p = p->next; } return L; } LinkList Bubble_Sort(LinkList L) { Lnode *p=NULL,*q=NULL,*r=NULL; int temp; p = L; r = L; while((p->next) && ((p->next)!=r)) { q = L; while((q->next) && ((q->next)!=r)) { if((q->data) > (q->next->data)) { temp = q->data; q->data = q->next->data; q->next->data = temp; } q = q->next; } r = q; } return L; } int main() { LinkList L=NULL; LinkList L2=NULL; Lnode *p; int n,a; printf("how long you want to add to the linklist:/n"); scanf("%d",&n); //L=Creat_LinkList1(L,n); L=Creat_LinkList2(L,n); Display_LinkList(L); printf("the length of LinkList2 is:%d/n",Length_LinkList(L)); printf("input the data you want to search:/n"); scanf("%d",&n); p = Locate_LinkList(L, n); if(!p) printf("not find!!/n"); else printf("located %d/n",p->data); printf("input a data which you should insert before it:/n"); scanf("%d",&a); L = Insert_LinkList(L, a); printf("/nafter insert,"); Display_LinkList(L); printf("/nafter reverse the linklist is:/n"); L=Reverse_LinkList(L); Display_LinkList(L); printf("/nafter select sort(large-->small),"); L=Select_Sort(L); Display_LinkList(L); printf("/nafter bubble sort(small-->large),"); L=Bubble_Sort(L); Display_LinkList(L); return 0; }