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

How to realize the operation of adding, deleting, querying and modifying one-way linked list in C language

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to add, delete, query and modify the one-way linked list in the C language, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.

Preface

Linked list is a linked storage structure of linear table, which can be inserted or deleted with time complexity of O (1). At the same time, compared with sequential table, linked list has no space waste. The linked list is divided into lead one-way linked list, no lead one-way linked list, lead circular linked list, lead two-way circular linked list, lead two-way circular linked list, do not lead two-way circular linked list, lead two-way linked list, do not lead two-way linked list, there are a total of eight kinds, among which the simplest structure is not leading one-way linked list, which is also the most error-prone to be realized. And when we carry on the linked list oj on the Internet, the questions are basically one-way linked list without taking the lead, and it is also the easiest to test in the interview of Internet big companies.

First, create the typedef int SLTDadaType;// data type struct SListNode {SLTDadaType _ data;// data struct SListNode* _ next;// pointer to the next node}; typedef struct SListNode SListNode; II, one-way linked list function declaration SListNode* BuyListNode (SLTDadaType x); / / create a node SListNode* SListPushBack (SListNode* head, SLTDadaType x); / / insert SListNode* SListPopBack (SListNode* head) / / insert SListNode* SListPushFornt (SListNode* head, SLTDadaType x); / delete SListNode* SListPopFornt (SListNode* head); / / delete SListNode* SListFind (SListNode* head, SLTDadaType x); / / find a node void SListModify (SListNode* head, SLTDadaType x, SLTDadaType y); / / x modify 3, function implementation 1. Create node SListNode* BuyListNode (SLTDadaType x) {SListNode* newnode = (SListNode*) malloc (sizeof (SListNode)); newnode- > _ data = x; newnode- > _ next = NULL; return newnode;} 2. Insert the tail node SListNode* SListPushBack (SListNode* head, SLTDadaType x) {SListNode* newnode = BuyListNode (x); / / regardless of whether the node is empty or not, create a node if (head = = NULL) / / header node is empty {head = newnode; return head } else / / header node is not empty, traverse directly to the end of the linked list and insert {SListNode* tail = head; while (tail- > _ next! = NULL) {tail = tail- > _ next;} tail- > _ next = newnode Return head;}} 3. Insert SListNode* SListPushFornt (SListNode* head, SLTDadaType x) {SListNode* newnode = BuyListNode (x); newnode- > _ next = head; head = newnode; return head;} 4. Delete SListNode* SListPopBack (SListNode* head) {/ / 1. Empty / / 2. There is only one node / / 3. There are multiple nodes if (head = = NULL) {return head;} else if (head- > _ next== NULL) {free (head); head = NULL; return head;} else {SListNode* prev = NULL SListNode* tail = head; while (tail- > _ next! = NULL) / / use the previous pointer to save the previous node of the node to be deleted {prev = tail; tail = tail- > _ next;} free (tail) If (prev! = NULL) prev- > _ next = NULL; return head;}} 5. Delete SListNode* SListPopFornt (SListNode* head) {if (head = = NULL) {return head;} else {SListNode* cur = head- > _ next; free (head); head = cur; return head;} 6. Find the node SListNode* SListFind (SListNode* head, SLTDadaType x) {SListNode* cur = head; while (cur) {if (cur- > _ data = = x) {return cur;} else {cur = cur- > _ next }} return NULL;} 7. Modify void SListModify (SListNode* head, SLTDadaType x, SLTDadaType y) / / x modify {SListNode* find = SListFind (head, x); if (find) {find- > _ data = y;} else {printf ("Sorry, the value you want to modify does not exist\ n") }} Thank you for reading this article carefully. I hope the article "how to add, delete, query and modify one-way linked list in C language" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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