Single-chain table (chain storage structure)-C Implementation of linear table

Source: Internet
Author: User
/* * LinkedList * linc * 2013.2.26 */#include <stdio.h>#include <stdlib.h>#include <time.h>#define OK 1  #define ERROR -1  #define TURE 1  #define FALSE 0 struct Node{int data;struct Node *next;};typedef struct Node *Head;typedef struct Node *LinkedList;//create the linked list,head insertvoid createList(LinkedList *list,int size,Head *head){/**head = (Head)malloc(sizeof(struct Node));printf("head test0");(*head)->data = size;printf("head test1");//(*head)->next = (*list);printf("head test2");*/LinkedList tmpList;srand(time(0));*list = (LinkedList)malloc(sizeof(struct Node));(*list)->next = NULL;for(int i = 0; i < size; i++){tmpList = (LinkedList)malloc(sizeof(struct Node));tmpList->data = rand()%100 + 1;tmpList->next = (*list)->next;(*list)->next = tmpList;printf("list->data %d is %d\n",i,tmpList->data);}//head nodetmpList = (LinkedList)malloc(sizeof(struct Node));tmpList->data = size;tmpList->next = (*list)->next;(*list)->next =tmpList;/*while((*list) != NULL){printf("data is %d\n",(*list)->data);(*list)->next;}*/}//get element of listint getElement(LinkedList list,int index,int *element){printf("getElement\n");LinkedList tmpList = list->next;printf("getElement---test\n");int size = tmpList->data;//the list sizeprintf("the size is %d\n",size);if(index > size){return ERROR;}int count = 0;while(tmpList && count < index+1)//+1 for head node{printf("data is %d\n",tmpList->data);tmpList = tmpList->next;++count;}if(!tmpList)return ERROR;*element = tmpList->data;return OK;}//clear listint clearList(LinkedList *list){//use 2 points free the listLinkedList tmpList1,tmpList2;tmpList1 = (*list)->next;while(tmpList1){printf("free the data is %d\n",tmpList1->data);tmpList2 = tmpList1->next;free(tmpList1);tmpList1 = tmpList2;}(*list)->next = NULL;return OK;}//insert//after indexint insert(LinkedList *list,int index,int element){printf("insert index is %d,element is %d\n",index,element);LinkedList tmpList = (*list)->next;int size = tmpList->data;if(index > size){printf("ERROR:the index > size.\n");return ERROR;}int count = 0;while(tmpList && count < index+1){tmpList = tmpList->next;++count;}if(!tmpList)return ERROR;LinkedList node = (LinkedList)malloc(sizeof(struct Node));node->data = element;node->next = tmpList->next;tmpList->next = node;return OK;}//deleteint delete(LinkedList *list,int index){printf("delete,index is %d\n",index);LinkedList tmpList = (*list)->next;int size = tmpList->data;if(index > size){printf("ERROR:the index > size.\n");return ERROR;}int count = 0;while(tmpList && count < index+1){tmpList = tmpList->next;++count;}if(!tmpList)return ERROR;LinkedList node = tmpList->next;tmpList->next = node->next;free(node);return OK;}int main(){LinkedList *list;Head *head;createList(list,10,head);int i = 1;getElement(*list,5,&i);insert(list,5,20);printf("getElement is %d\n",i);getElement(*list,5,&i);printf("getElement is %d\n",i);delete(list,5);clearList(list);return 0;}

The advantage of the chain storage structure is that it is very convenient to delete or insert the target element after finding the target element, just move the pointer once; the disadvantage is that you cannot quickly find the target element as in the sequential storage structure, and you can only traverse the elements one by one.

Related Article

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.