1) sequential storage of linked list --- Code 2, storage ---
Reference:
1) big talk Data Structure
2) http://blog.chinaunix.net/uid-20680669-id-147844.html
3) http://blog.csdn.net/strommaybin/article/details/51919464
1. Linear table (list): finite sequence of zero or multiple data elements
Linear_list is the most common and simple data structure. In short, a linear table is a finite sequence of n data elements. A linear table is a table with a linear structure. What is a linear structure? A linear structure is an ordered set of n data elements. It has four basic features:
1. A unique "first element" must exist in the collection ";
2. There must be a unique "last element" in the collection ";
3. Apart from the last element, other data elements have unique "successors ";
4. Apart from the first element, other data elements have a unique "precursor ".
For example, (a1, a2, a3 ,....., An), a1 is the first element, and an is the last element. This set is a set of extremely linear structures.
The logical feature of a non-linear structure is that a node element may correspond to multiple direct prefixes and multiple rear drives.
Common linear structures include linear tables, stacks, queues, dual queues, arrays, linked lists, and strings.
2. Functions and implementation:
1 typedef int Status; 2 3 4 // 1. struct definition: 5 # define MAXSIZE 100 6 typedef int ElemType; 7 8 // struct Definition 9 typedef struct list 10 {11 ElemType data [MAXSIZE]; // data 12 int length; // The current length of the linear Table 13} sqlist; 14 15 16 // 2. initialize 17 sqlist * init_sqlist () 18 {19/tmp = (TSeqList *) malloc (sizeof (TSeqList); // allocate space 20 sqlist * list = (sqlist *) malloc (sizeof (sqlist); 21 list-> length = 0; 22 return list; 23} 24 25 // 3. tail plug Value 26 int insert_rear_sqlist (sqlist * list, ElemType data) 27 {28 int ret = 0; 29 if (list-> length = MAXSIZE | list-> length = 0) 30 {31 ret =-1; 32 printf ("func insert_rear_sqlist erro: % d", ret); 33 return-1; 34} 35 list-> length = list-> length ++; // length + 1 36 list-> data [list-> length] = data; // insert data 37 return 0; 38} 39 40 // 4. print the node value 41 int print_sqlist (sqlist * list) 42 {43 int ret =-1; 44 if (list-> length = 0) 4 5 {46 printf ("\ nThe list is empty: % d \ n", ret); 47 return ret; 48} 49 50 else 51 {52 for (int I = 0; I <list-> length; I ++) 53 printf ("% 5d \ n", list-> data [I]); 54} 55 return 0; 56} 57 58 // 5. judge whether it is null 59 int is_empty_sqlist (sqlist * list) 60 {61 return list-> length = 0?; 62} 63 64 // 6. locate the position of the node whose value is x (to be corrected if any problem exists) 65 int find_num_sqlist (sqlist * list, ElemType data) 66 {67 int ret = 0; 68 int temp = 0; 69 if (list-> length = MAXSIZE | list-> length = 0) 70 {71 ret =-1; 72 printf ("func insert_rear_sqlist erro: % d ", ret); 73 return-1; 74} 75 for (int I = 0; list-> length <MAXSIZE; I ++) 76 {77 if (list-> data [I] = data) 78 temp = I; 79} 80 81 return temp; 82} 83 84 // 7. get the value of the I node 85 int get_elem (sqlist * list, int I) 86 {87 if (list-> length <= 0 | I <0 | I> list-> length) 88 {89 return-1; 90} 91 return list-> data [I]; 92} 93 94 // 8. insert the e value 95 int insert_list (sqlist * list, int I, ElemType e) 96 {97 int k = 0, ret = 0; 98 if (list-> length = MAXSIZE) 99 {100 ret =-1; 101 printf ("insert_list erro: % d", ret ); 102} 103 if (I <0 | I> list-> length | list-> length> MAXSIZE) 104 {105 ret =-2; 106 printf ("insert_list erro: % d ", ret); 107} 108 109 for (k = list-> length; k> = I; k --) 110 {111 list-> data [k] = list-> data [k-1]; // element move back 112} 113 list-> data [I] = e; 114 list-> length ++; 115 116 return 0; 117} 118 119 // 9. delete node 120 int delete_list (sqlist * list, int I, ElemType * e) 121 {122 int k = 0; 123 int ret = 0; 124 if (list-> length = 0) 125 {126 ret =-1; 127 printf ("delete_list erro: % d", ret ); 128} 129 if (I <0 | I> list-> length) 130 {131 ret =-1; 132 printf ("delete_list erro: % d", ret ); 133} 134 // if (I <list-> length) 135 // {136 * e = list-> data [I]; 137 for (k = I; k <= list-> length; k ++) 138 {139 list-> data [k] = list-> data [k + 1]; // move the element forward 140} 141 //} 142 list-> length --; 143 return 0; 144}
---------------------------------------
3. Test code
1 int main () 2 {3 int ret = 0; 4 int t1 = 1; 5 int t2 = 2; 6 int t3 = 3; 7 int t4 = 4; 8 int t5 = 5; 9 10 // sqlist * list; 11 // void init_sqlist (sqlist * list) 12 sqlist * list = init_sqlist (); 13 14 // cout <sizeof (* list); 15 // int insert_list (sqlist * list, int I, ElemType e); 16 // 1. header Insertion Method 17 ret = insert_list (list, 0, t1); 18 ret = insert_list (list, 0, t2); 19 ret = insert_list (list, 0, t3 ); 20 ret = insert_list (list, 0, t4); 21 ret = insert_list (list, 0, t5); 22 23 // 2. print the traversal 24 // int print_sqlist (sqlist * list) 25 printf ("insert data: \ n"); 26 ret = print_sqlist (list); 27 int tmp = 0; 28 29 // 3. delete all nodes in the linked list 30 printf ("\ n delete node data: \ n"); 31 while (list-> length> 0) 32 {33 // int delete_list (sqlist * list, int I, ElemType * e) 34 ret = delete_list (list, 0, & tmp ); 35 printf ("% 5d \ n", tmp); 36} 37 38 // 5. judge whether it is null 39 // int is_empty_sqlist (sqlist * list) 40 ret = is_empty_sqlist (list); 41 printf ("Whether the linked list is empty % 5d \ n", ret ); 42 43 system ("pause"); 44 return 0; 45}
4. Running result