随着逐渐地学习,我慢慢体会到了程序=数据结构+算法这句话的内涵了。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define ANODE struct node
  4 typedef struct node *Node;
  5 typedef int DataType;
  6 struct node
  7 {
  8     DataType info;
  9     Node node;
 10     int index;
 11 };
 12 
 13 // new an empty LinkList
 14 Node creEmList()
 15 {
 16     Node node = (Node) malloc(sizeof(ANODE));
 17     if (node != NULL)
 18     {
 19         node->node = NULL;
 20         node -> index = -1;
 21     }
 22     else
 23         printf("fail to create an empty LinkList");
 24     return node;
 25 }
 26 
 27 // judge whether the LinkList is empty
 28 int isEmList(Node list)
 29 {
 30     return list->node == NULL;
 31 }
 32 
 33 // insert an element into the last index of LinkList
 34 void insertToLast(Node list, DataType x)
 35 {
 36     Node aNode = (Node) malloc(sizeof(ANODE));
 37     aNode->node = NULL;
 38     aNode->info = x;
 39     while (list->node != NULL)
 40         list = list->node;
 41     aNode->index = list->index + 1;
 42     list->node = aNode;
 43 }
 44 
 45 //insert an element after the node of the index
 46 void insertAfter(Node list, DataType x, int index)
 47 {
 48     void createIndex(Node);
 49     Node aNode = (Node) malloc(sizeof(ANODE));
 50     aNode->index = index + 1;
 51     aNode -> info = x;
 52     Node first = NULL, second = NULL;
 53     while(list -> node != NULL)
 54     {
 55         if(list -> index == index)
 56         {
 57             first = list;
 58             second = list -> node;
 59             break;
 60         }
 61         list = list -> node;
 62     }
 63     if(second != NULL)
 64     {
 65         aNode -> node = second;
 66         first -> node = aNode;
 67         createIndex(second);
 68     }
 69     else if(list->index == index)
 70     {
 71         first = list;
 72         first->node = aNode;
 73     }
 74     else 
 75     {
 76     printf("out of bounds");
 77     return;
 78     }
 79         
 80 }
 81 
 82 //create the index of nodes after the node given by the index
 83 void createIndex(Node node)
 84 {
 85     while(node->node != NULL)
 86     {
 87         node -> index++;
 88         node = node -> node;
 89     }
 90     node -> index++;
 91 }
 92 
 93 //delete the node which value is x from the LinkList
 94 void deleteNode(Node list, DataType x)
 95 {
 96     void downIndex(Node);
 97     Node find(Node, DataType);
 98     Node node = NULL, first = NULL, second = NULL,third = NULL;
 99     node = find(list, x);
100     if(node != NULL)
101     {
102         if(node -> node != NULL)
103         {
104             first = node;
105             second = node -> node;
106             if(second -> node != NULL)
107             {
108                 third = second -> node;
109                 first -> node = third;
110                 downIndex(second);    
111                 free(second);
112             }
113             else
114             {
115                 first -> node = NULL;
116                 free(second);
117             }
118         }
119         else
120         free(node);    
121     }    
122 }
123 
124 //find the note whie value is equal to x
125 Node find(Node list, DataType x)
126 {
127     while(list -> node != NULL)
128     {
129         if(list -> node ->info == x)
130                 return list;
131         list = list -> node;
132     }
133     if(list -> info == x)
134         return list;
135     else
136         printf("no this value in the LinkList\n");
137     return NULL;    
138 }
139 //Auto downgrade node's index after the given node
140 void downIndex(Node node)
141 {
142     while(node -> node != NULL)
143     {
144         node->index--;
145         node = node -> node;;
146     }
147     node -> index--;
148 }
149 
150 //get the index of the Node
151 int getIndex(Node list, DataType x)
152 {
153     while(list -> node != NULL)
154     {
155         if(list->info == x)
156             return list->index;
157         list = list->node;
158     }
159     if(list -> info == x)
160         return list->index;
161     else 
162     {
163         printf("value %d is not exist\n", x);
164         return -1;
165     }    
166 }
167 
168 int main()
169 {
170     Node list = creEmList();
171     insertToLast(list, 55);
172     insertToLast(list,66);
173     insertAfter(list, 77, 0);
174     deleteNode(list, 77);
175     int index = getIndex(list, 66);
176     printf("value's index is: %d\n", index);
177     while (list->node != NULL)
178     {
179         printf("index:%d value:%d\n", list->index, list->info);
180         list = list->node;
181     }
182     printf("index:%d value:%d\n", list->index, list->info);
183 }

相关文章:

  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-01-14
  • 2021-05-05
猜你喜欢
  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-11-17
  • 2021-06-12
相关资源
相似解决方案