线性表之单链表
来源:互联网 发布:网络涉军举报平台上线 编辑:程序博客网 时间:2024/05/21 09:25
-
-
-
-
-
- #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;
-
-
- void createList(LinkedList *list,int size,Head *head)
- {
-
-
-
-
-
-
-
-
- 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);
- }
-
- tmpList = (LinkedList)malloc(sizeof(struct Node));
- tmpList->data = size;
- tmpList->next = (*list)->next;
- (*list)->next =tmpList;
-
-
-
-
-
-
-
-
-
- }
-
- int getElement(LinkedList list,int index,int *element)
- {
- printf("getElement\n");
- LinkedList tmpList = list->next;
- printf("getElement---test\n");
- int size = tmpList->data;
- printf("the size is %d\n",size);
- if(index > size)
- {
- return ERROR;
- }
-
- int count = 0;
- while(tmpList && count < index+1)
- {
- printf("data is %d\n",tmpList->data);
- tmpList = tmpList->next;
- ++count;
- }
- if(!tmpList)
- return ERROR;
- *element = tmpList->data;
- return OK;
- }
-
-
- int clearList(LinkedList *list)
- {
-
- LinkedList 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;
- }
-
-
-
- int 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;
- }
-
- int 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;
- }
链式存储结构的优点在于它在找到目标元素后,删除或者插入都十分方便,只需将指针挪动一次就好;缺点就是不能像顺序存储结构那样迅速的找到目标元素,只能笨笨的一个一个元素的遍历下去。