Can judge whether there is a static linked list with the remaining space, and the remaining static

Source: Internet
Author: User

Can judge whether there is a static linked list with the remaining space, and the remaining static

The first time the system learned the data structure was half a year ago. I watched the data structure and algorithm video of the little turtle. If I learned it myself, I had a lot of questions about AVL Tree, red tree, and the shortest path of the graph, minimum Spanning Tree... however, I always have a general impression on data structures and algorithms. Now, as I keep writing code and doing OJ, I have become increasingly aware of the importance of data structures and algorithms. I plan to read it again, looking at this: big talk data structure (by Cheng Jie) and data structure (by Yan Weimin in C language), we do not recommend new users to use data structure and algorithm analysis (by Mark Allen Weiss) this book is really hard to understand.

Back to the question, I read a lot of books about the description and code of the static linked list and found that I didn't judge whether there is any space left. I figured out a night to complete the code.

1 # include <stdio. h> // Anjaxs 2 # include <stdbool. h> 3 4 # define MAXSIZE 7 5 typedef int ElemType; 6 7 typedef struct {8 ElemType data; 9 int cur; 10} StaticList; 11 12 int Malloc_SL (StaticList * space ); 13 void Free_SL (StaticList * space, int k); 14 void InitList (StaticList * space); 15 bool ListInsert (StaticList * L, int I, ElemType e ); 16 bool ListDelete (StaticList * L, int I); 17 int ListLength (StaticList * L); 18 void show (StaticList * L); 19 20 int main () 21 {22 int I; 23 int choice; 24 StaticList L [MAXSIZE]; 25 InitList (L); 26 printf ("1: insert 2: delete 3: Output 0: Exit \ n"); 27 while (scanf ("% d", & choice) & choice! = 0) 28 {29 int x; 30 int pos; 31 switch (choice) 32 {33 case 1: 34 printf ("Enter the number and position to insert \ n "); 35 scanf ("% d", & x, & pos); 36 ListInsert (L, pos, x); 37 break; 38 case 2: 39 printf ("Enter the location to be deleted \ n"); 40 scanf ("% d", & pos); 41 ListDelete (L, pos); 42 break; 43 case 3: 44 show (L); 45 break; 46 default: 47 printf ("Enter 1-3. \ n "); 48 break; 49} 50 printf (" 1: insert 2: delete 3: Output 0: Exit \ n "); 51} 52 53 return 0; 54} 55 56 57 void InitList (StaticList * space) 58 {59 int I; 60 // space [0]. cur is the pointer to the first idle position 61 // space [MAXSIZE-1]. cur is the header pointer 62 for (I = 0; I <MAXSIZE-1; ++ I) {63 space [I]. data = 0; 64 space [I]. cur = I + 1; 65} 66 // The current static linked list is empty, and the cur of the last element is 0 67 space [MAXSIZE-1]. cur = 0; 68 space [MAXSIZE-1]. data = 0; 69} 70 71 bool ListInsert (StaticList * L, int I, ElemType e) 72 {73 int j, k, l; 74 k = MAXSIZE-1; 75 if (I <1 | I> ListLength (L) + 1) {76 printf ("enter a valid location. \ N "); 77 return false; 78} 79 if (ListLength (L)> = MAXSIZE-2) 80 {81 printf (" no free space is available. \ N "); 82 return false; 83} 84 j = Malloc_SL (L); 85 if (j) 86 {87 L [j]. data = e; 88 for (l = 1; l <= I-1; ++ l) 89 k = L [k]. cur; 90 L [j]. cur = L [k]. cur; 91 L [k]. cur = j; 92 return true; 93} 94 95 return false; 96} 97 98 bool ListDelete (StaticList * L, int I) 99 {100 int j, k; 101 if (I <1 | I> ListLength (L) {102 printf ("enter a valid location. \ N "); 103 return false; 104} 105 106 k = MAXSIZE-1; 107 for (j = 1; j <= I-1; + + j) 108 k = L [k]. cur; 109 j = L [k]. cur; 110 L [k]. cur = L [j]. cur; 111 Free_SL (L, j); 112 return true; 113} 114 115 int ListLength (StaticList * L) 116 {117 int j = 0; 118 int I = L [MAXSIZE-1]. cur; 119 while (I) 120 {121 I = L [I]. cur; 122 j ++; 123} 124 125 return j; 126} 127 128 int Malloc_SL (StaticList * space) 129 {130 // returns the first idle subscript 131 int I = space [0]. cur; 132 133 if (space [0]. cur) 134 space [0]. cur = space [I]. cur; 135 136 return I; 137} 138 139 void Free_SL (StaticList * space, int k) 140 {141 space [k]. cur = space [0]. cur; 142 space [0]. cur = k; 143} 144 145 void show (StaticList * L) 146 {147 int I; 148 printf ("% 30s \ n", "static Linked List "); 149 printf ("% 8 s", "index:"); 150 for (I = 0; I <MAXSIZE; ++ I) 151 {152 printf ("% 5d ", i); 153} 154 printf ("\ n % 8 s", "data:"); 155 for (I = 0; I <MAXSIZE; ++ I) 156 {157 printf ("% 5d", L [I]. data); 158} 159 printf ("\ n % 8 s", "cur:"); 160 for (I = 0; I <MAXSIZE; ++ I) 161 {162 printf ("% 5d", L [I]. cur); 163} 164 printf ("\ n"); 165 I = L [MAXSIZE-1]. cur; 166 while (I) 167 {168 printf ("% d->", L [I]. data); 169 I = L [I]. cur; 170} 171 printf ("^ \ n"); 172}

 

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.