标签:style blog io ar for strong div 2014 on
链表基本操作集合
//List code by Denis #include <stdio.h> #include <malloc.h> #define ture 1 #define false 0 typedef int ElemType; typedef struct list{ ElemType data; struct list *next; }LNode; //Create a list,nLength is the length of the line list LNode *Create(int nLength) { int i; LNode *Ln_Head,*Ln_tmp1,*Ln_tmp2; Ln_Head = (LNode *)malloc(sizeof(LNode)); Ln_tmp1 = Ln_Head; /*Save the Head pointor*/ printf("Input %d data\n",nLength); for(i=0;i<nLength;i++) { Ln_tmp2 = (LNode *)malloc(sizeof(LNode)); scanf("%d",&Ln_tmp2->data);//Input the data //Insert the data to the end odf list Ln_tmp1->next = Ln_tmp2; Ln_tmp1 = Ln_tmp2; Ln_tmp2->next = NULL; } return Ln_Head; } void ShowList(LNode *Head) { LNode *P= Head->next; printf("The list is:\n"); while(P) { printf("%d ",P->data); P = P->next; } printf("\n"); } //Get the length of the List int ListLength(LNode *Head) { int length=0; LNode *P; P = Head->next; while(P) { length++; P = P->next; } return length; } //Get the number i data that in the list to the e parameter //i must be >0,and <= List's length ElemType GetElem(LNode *Head, int i, ElemType e) { int j=0; LNode *P, *tmp; P = Head->next; while(P && j!=i) { tmp = P;//Save the P P = P->next; j++; } return tmp->data; } //verify whether E is the one of list //if return value==1,E is in the list int LocateElem(LNode *Head, ElemType E) { LNode *P; P = Head->next; while(P) { if(P->data == E)//,E is in the list return ture; else P = P->next; } return false; } //Insert E to the list' tail int ListInsert(LNode *Head, int ListLength, ElemType E) { LNode *P, *Tail; P = Head->next; //Get the tail pointor of the list while(P->next) { P = P->next; } Tail = P; P = (LNode *)malloc(sizeof(LNode)); P->data = E; Tail->next = P; P->next = NULL; //ListLength++; } LNode *Union(LNode *Head1, LNode *Head2) { int L1_len = ListLength(Head1); int L2_len = ListLength(Head2); ElemType E=Head2->next->data; int i=1; printf("Union list_1 and list_2\n"); for(i=1; i<=L2_len; i++) { E=GetElem(Head2,i,E); if(!LocateElem(Head1,E)) ListInsert(Head1,++L1_len, E); } return Head1; } void main() { LNode *Create(int nLength); void ShowList(LNode *Head); LNode *Head1,*Head2; int nLength; int equal = 0; int Verify_eq; ElemType Elem_E=0; Head1 = Create(4);//Create the first list that has 4 data ShowList(Head1); Head2 = Create(4);//Create the second list that has 4 data ShowList(Head2); //nLength = ListLength(Head2);//Get the length of the list Elem_E = GetElem(Head1, 2,Elem_E); Verify_eq = LocateElem(Head1, 5); //debug insert function //ListInsert(Head1,nLength,10000); Head1 = Union(Head1,Head2); ShowList(Head1); getch(); }
标签:style blog io ar for strong div 2014 on
原文地址:http://blog.csdn.net/denyz/article/details/40385243