Basic list operation set

Source: Internet
Author: User

Basic list operation set


//List code by Denis#include <stdio.h>#include <malloc.h>#define ture 1 #define false 0typedef int ElemType;typedef struct list{ElemType data;struct list *next;}LNode; //Create a list,nLength is the length of the line listLNode *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 listLn_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 Listint 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 lengthElemType GetElem(LNode *Head, int i, ElemType e){int j=0;LNode *P, *tmp;P = Head->next;while(P && j!=i){tmp = P;//Save the PP = P->next;j++;}return tmp->data;}//verify whether E is the one of list//if return value==1,E is in the listint LocateElem(LNode *Head, ElemType E){LNode *P;P = Head->next;while(P){if(P->data == E)//,E is in the listreturn ture;elseP = P->next;}return false;}//Insert E to the list' tailint ListInsert(LNode *Head, int ListLength, ElemType E){LNode *P, *Tail;P = Head->next;//Get the tail pointor of the listwhile(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 dataShowList(Head1);Head2 = Create(4);//Create the second list that has 4 dataShowList(Head2);//nLength = ListLength(Head2);//Get the length of the listElem_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();}

Zookeeper

Basic list operation set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.