Linked List exercises and table exercises

Source: Internet
Author: User

Linked List exercises and table exercises

This article describes operations on linked lists (including single-chain tables and two-way cyclic linked lists) 
1. Create a single-chain table and a double-chain table.
2. Single-chain table and double-chain table printing.
3. insert or delete a single-chain table.
4. insert and delete double-stranded tables.
5. Reverse placement of a single-chain table.
6. Number of Single-linked table nodes.
7. Single-chain table and double-chain table search.

Function source code:

1 // linked list related issues
  2 
  3 typedef int DataType;
  4 typedef struct LinkNode // Single linked list structure
  5 {
  6 struct LinkNode * next;
  7 DataType data;
  8} LinkNode;
  9 
 10 LinkNode * CreateNode (DataType x) // Create a single linked list node
 11 {
 12 LinkNode * tmp = (LinkNode *) malloc (sizeof (LinkNode));
 13 if (NULL == tmp)
 14 {
 15 printf ("Failed to allocate memory! \ N");
 16 return NULL;
 17}
 18 tmp-> next = NULL;
 19 tmp-> data = x;
 20 return tmp;
 twenty one }
 twenty two 
 23 void PrintLinkList (LinkNode * phead) // Single linked list printing
 twenty four {
 25 while (phead)
 26 {
 27 printf ("% d", phead-> data);
 28 phead = phead-> next;
 29}
 30 printf ("\ n");
 31}
 32
 33 void InsertLinkList (LinkNode ** phead, LinkNode * pos, DataType x) // Single linked list insertion
 34 {
 35 LinkNode * newNode, * tmp = * phead;
 36 assert (phead);
 37 if (NULL == * phead)
 38 {
 39 * phead = CreateNode (x);
 40 return;
 41}
 42 while (* phead! = Pos)
 43 {
 44 tmp = * phead;
 45 * phead = (* phead)-> next;
 46}
 47 newNode = CreateNode (x);
 48 newNode-> next = tmp-> next;
 49 tmp-> next = newNode;
 50}
 51
 52 size_t ListNodeCount (LinkNode * phead) // Calculate the number of nodes in a single linked list
 53 {
 54 size_t count = 0;
 55 while (phead)
 56 {
 57 count ++;
 58 phead = phead-> next;
 59}
 60 return count;
 61}
 62
 63 LinkNode * LinkListSearch (LinkNode * phead, DataType x) // Find a number in a single linked list
 64 {
 65 while (phead)
 66 {
 67 if (phead-> data == x)
 68 return phead;
 69 phead = phead-> next;
 70}
 71 return NULL;
 72}
 73
 74 LinkNode * ReverseLinkList (LinkNode * phead) // Inverse of single linked list
 75 {
 76 LinkNode * first = phead;
 77 LinkNode * cur = first-> next;
 78 first-> next = NULL;
 79
 80 while (cur)
 81 {
 82 LinkNode * tmp = cur-> next;
 83 cur-> next = first;
 84 first = cur;
 85 cur = tmp;
 86}
 87 return first;
 88}
 89
 90 size_t RemoveLinkList (LinkNode ** phead, LinkNode * pos)
 91 {
 92 LinkNode * first = * phead;
 93 while (first)
 94 {
 95 if (* phead == pos) // delete the head node
 96 {
 97 * phead = first-> next;
 98 free (pos);
 99 pos = NULL;
100 return 1;
101}
102 else if (first-> next == pos) // non-head node case
103 {
104 first-> next = pos-> next;
105 free (pos);
106 pos = NULL;
107 return 1;
108}
109 first = first-> next;
110}
111 return 0;
112}
113
114 typedef struct DoubleLinkList // Double linked list structure
115 {
116 DataType data;
117 struct DoubleLinkList * prev;
118 struct DoubleLinkList * next;
119} DoubleList;
120
121 DoubleList * CreateDoubleList (DataType x) // Create a double linked list node
122 {
123 DoubleList * newNode = (DoubleList *) malloc (sizeof (DoubleList));
124 assert (newNode);
125 newNode-> next = NULL;
126 newNode-> prev = NULL;
127 newNode-> data = x;
128 return newNode;
129}
130
131 void PrintDoubleList (DoubleList * phead) // Print double linked list
132 {
133 DoubleList * tmp = phead;
134 while (tmp)
135 {
136 printf ("% d", tmp-> data);
137 tmp = tmp-> next;
138 if (tmp == phead)
139 break;
140}
141 printf ("\ n");
142}
143
144 DoubleList * DoubleListSearch (DoubleList * phead, DataType x) // Double linked list search
145 {
146 DoubleList * tmp = phead;
147 while (phead)
148 {
149 if (phead-> data == x)
150 return phead;
151 if (tmp == phead-> next)
152 break;
153 phead = phead-> next;
154}
155 return NULL;
156}
157
158 void DoubleListInsert (DoubleList ** phead, DataType x) // Head insertion of double linked list
159 {
160 DoubleList * tmp = (* phead);
161 DoubleList * newNode = CreateDoubleList (x);
162
163 if (NULL == * phead)
164 {
165 * phead = newNode;
166 (* phead)-> next = * phead;
167 (* phead)-> prev = * phead;
168}
169 else
170 {
171 newNode-> next = (* phead)-> next;
172 (* phead)-> next = newNode;
173 newNode-> prev = * phead;
174 newNode-> next-> prev = newNode;
175}
176}
177
178
179 size_t RemoveDoubleListNode (DoubleList ** phead, DataType x) // Delete a double linked list node
180 {
181 DoubleList * tmp = * phead;
182 while (* phead)
183 {
184 if (tmp-> data == x)
185 {
186 tmp-> prev-> next = tmp-> next;
187 tmp-> next-> prev = tmp-> prev;
188 if (tmp-> data == (* phead)-> data)
189 * phead = tmp-> next;
190 if ((* phead)-> next == * phead)
191 {
192 free (* phead);
193 * phead = NULL;
194}
195 free (tmp);
196 tmp = NULL;
197 return 1;
198}
199 if (* phead == tmp-> next)
200 break;
201 tmp = tmp-> next;
202}
203 return 0;
204}

 


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.