Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of single linked list in C language

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of single linked list in C language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, train of thought step 1. Define the structure

a. Data fields: used to store data

b. Pointer field: the location where the next data is stored.

two。 Initialization

Apply for the header node and initialize it to empty

3. Find the number of current data elements

a. Set a pointer variable p to the header node and the count variable size equals 0

b. Loop to determine whether p-> next is empty, if not, let the pointer p point to its immediate successor node, and let size augment itself.

c. Return to size

4. insert

a. Set two pointers, one to the header node and the other to dynamically apply for memory space to store the number to be inserted

b. Find the previous bit of the position to be inserted and determine whether the insertion position is correct

c. Generate a new node, assign a value to the data field of the new node, execute step ①, and perform step ②

5. Delete

a. Set two pointers, p and Q, to point to the header node and Q to the node to be deleted.

b. Find the previous bit of the location to be deleted, and determine whether the deletion location is correct and exists.

C. Q points to the deleted node, assigns the data field of the deleted node to the next node of the deleted node p to the deleted node, freeing up the memory space of Q

6. Free up memory space

Finally, remember to leave the head node empty! Otherwise, wild pointers are easy to appear.

Code # include # include typedef int DataType;// gives int an individual name to facilitate future modification of typedef struct Node {DataType data;// data field struct Node * next;// pointer domain} SLNode;// initialization void ListInit (SLNode * * head) {* head = (SLNode *) malloc (sizeof (SLNode)); / / Application header node (* head)-> next = NULL } / / find the number of current data elements int ListLength (SLNode * head) {SLNode * p = head; int size = 0; while (p-> next! = NULL) {p = p-> next; size++;} return size;} / / insert int ListInsert (SLNode * head, int I, DataType x) {SLNode * p, * q; int j P = head; j =-1; while (p-> next! = NULL & & j

< i - 1) { p = p->

Next; jackers;} if (j! = I-1) {printf ("insert parameter is in the wrong position! \ n "); return 0;} Q = (SLNode *) malloc (sizeof (SLNode)); / / generate new nodes Q-> data = x; Q-> next = p-> next; p-> next = Q; return 1;} / delete int ListDelete (SLNode * head, int I, DataType * x) {SLNode * p, * q; int j; p = head J =-1; while (p-> next! = NULL & & p-> next- > next! = NULL & & j

< i - 1) { p = p->

Next; jackers;} if (j! = I-1) {printf ("delete position parameter error! \ n "); return 0;} Q = p-> next; * x = Q-> data; p-> next = p-> next- > next; free (Q); / / release the memory space return 1 of the deleted node;} / / take int ListGet (SLNode * head, int I, DataType * x) {SLNode * p; int j; p = head J =-1; while (p-> next! = NULL & & j

< i) { p = p->

Next; jackers;} if (j! = I) {printf ("wrong position parameter taken out! \ n "); return 0;} * x = p-> data; return 1;} / / release void ListDestroy (SLNode * * head) {SLNode * p, * Q; p = * head; while (p! = NULL) {Q = p; p = p-> next; free (Q) } * head = NULL;} int main () {SLNode * head; int i, x; ListInit (& head); for (I = 0; I < 10; I +) ListInsert (head, I, I + 10); ListDelete (head, 9, & x); for (I = 0; I < ListLength (head)) ListGet +) {ListGet (head, I, & x); printf ("% d", x);} ListDestroy (& head); system ("pause"); return 0 } this is the end of the article on "sample Analysis of single linked list in C language". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report