线性表-链式存储

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <malloc.h>
 5 using namespace std;
 6 
 7 typedef int ElemType;
 8 typedef bool Status;
 9 typedef struct LinkList{
10     ElemType data;
11     struct LinkList * NextNode;
12     size_t Length;
13 } *LNode, LinkList;
14 
15 Status InitList(LinkList * list){
16     list->data = -1;
17     list->NextNode = NULL;
18     list->Length = 0;
19     return true;
20 }
21 Status DestoryList(LinkList * list){
22     LNode node = list->NextNode;
23     for(;node!=NULL;node=node->NextNode){
24         free(node);
25     }
26     return true;
27 }
28 Status ListInsert(LinkList * list, ElemType data){
29     LNode node = (LinkList *)malloc(sizeof(LinkList));
30     if(node==NULL) return false;
31     node->data = data;
32     node->NextNode = list->NextNode;
33     list->NextNode = node;
34     list->Length++;
35     return true;
36 }
37 LNode GetElem(LinkList list, size_t position){
38     LNode node = &list;
39     size_t pos_l = 0;
40     while(pos_l<position&&node!=NULL){
41         pos_l++;
42         node=node->NextNode;
43     }
44     if(node!=NULL) return node;
45     else return false;
46 }
47 Status ListDelete(LinkList * list, size_t position, ElemType * data){
48     LNode node = NULL;
49     if(position==1)
50         node = list;
51     else
52         node = GetElem(*list, position-1);
53     if(node==NULL||node->NextNode==NULL) return false;
54     LNode TodelPtr = node->NextNode;
55     *data = node->NextNode->data;
56     node->NextNode = node->NextNode->NextNode;
57     list->Length--;
58     free(TodelPtr);
59     return true;
60 }
61 
62 size_t Length(LinkList list){
63     return list.Length;
64 }
65 
66 Status Empty(LinkList list){
67     return (list.Length==0);
68     //return list.NextNode==NULL;
69 }
70 void PrintList(LinkList list){
71     printf("Linked List(%d): ",  Length(list));
72     LNode node = list.NextNode;
73     while(node != NULL && list.Length > 0){
74         printf("[%d] ", node->data);
75         node = node->NextNode;
76     }
77     printf("\n");
78 }
79 void test(){
80     static int b = 10;
81 }
82 int main()
83 {
84 
85     LinkList List;
86     InitList(&List);
87     for(int i=233;i>0;--i)
88        ListInsert(&List, i);
89     PrintList(List);
90     int a;
91     ListDelete(&List, 5, &a);
92     PrintList(List);
93 
94     return 0;
95 }

 

posted @ 2020-04-15 09:33  wkfxm  阅读(115)  评论(0编辑  收藏  举报