[Data Structure] C language implementation single-link table

Implement a basic singly linked list in C language

Structure

First, we define a structure Node

typedef int DataType;typedef struct Node{ int data; struct Node* next;}Node,*PNode;

In this structure of Node, it contains A data element and a pointer to the next node

Initialize the linked list

void InitList (PNode* pHead){ *pHead = NULL;}

Apply for a new node

PNode BuyNewNode(DataType data){ PNode New = NULL; New = (PNode)malloc(sizeof(Node)); New->data = data; New->next = NULL; return New;}

Print linked list

void PrintList(PNode pHead){ PNode Cur = pHead; while (Cur) {printf(" %d ", Cur->data); Cur = Cur->next;} printf("
");}

Insert a node at the end h2> < pre code_snippet_id="1936576" snippet_file_name="blog_20161019_5_4971077" class="cpp" name="code">void PushBack(PNode* pHead,DataType data){ assert(pHead!=NULL); PNode Cur = NULL; PNode NewNode = BuyNewNode (data); Cur = *pHead; if (*pHead == NULL) {*pHead = NewNode;} else {while (Cur->next) {Cur = Cur->next;} Cur->next = NewNode;} }

Insert a node before

void PushFront(PNode* pHead, DataType data){ assert(pHead != NULL); PNode Cur = *pHead; PNode NewNode = BuyNewNode(data); if (*pHead == NULL) {*pHead = NewNode;} else {NewNode ->next = *pHead; *pHead = NewNode; }}

Remove a node from the tail

void PopBack(PNode* pHead){ assert(pHead!=NULL); PNode Cur = *pHead; PNode DelNode = NULL; if (*pHead == NULL) {return ;} if (Cur->next == NULL) {free(Cur); *pHead = NULL;} while (Cur->next->next != NULL) {Cur = Cur->next;} //Cur->next = NULL; DelNode = Cur->next; free(DelNode); //free(*(Cur->next)); DelNode = NULL; Cur->next = NULL;}

Remove a node from the front

void PopFront(PNode* pHead){ assert(pHead! =NULL); if ((*pHead) == NULL)//No node {return;} else//There are multiple nodes {PNode Del = (*pHead)->next; //(*pHead) = (* pHead)->next; free((*pHead)->next); Del = NULL; }}

Find a node

PNode Find(PNode pHead, DataType data){ assert(pHead != NULL); PNode Cur = pHead; while (Cur != NULL) {if (Cur->data == data) {return Cur;} Cur = Cur->next;} return NULL;}

Insert according to a node One node

void Insert(PNode pos, DataType data){ assert(pos != NULL); PNode NewNode = BuyNewNode(data); if (pos->next == NULL) {pos->next = NewNode;} else {PNode Cur = pos->next; pos->next = NewNode; NewNode->next = Cur; }}// Insert(Find(&pHead, 4),5) ;

Destroy the singly linked list

void Destroy (PNode* pHead){ PNode Node = (*pHead); PNode NodeNext = Node->next; while (NodeNext != NULL) {free(Node); Node = NodeNext; NodeNext = NodeNext->next;} free(Node ); *pHead = NULL; Node = NULL;}

Return to the tail node

PNode Back(PNode pHead){ //assert(pHead != NULL); PNode Cur = pHead; if (Cur == NULL) {return Cur;} while (Cur->next! = NULL) {Cur = Cur->next;} return Cur;}

Complete code

Header file SeqList.h

#ifndef __SEQLIST_H__#define __SEQLIST_H__#include#include #include#includetypedef int DataType;typedef struct Node{ int data; struct Node* next;}Node,*PNode;void InitList(PNode* pHead);PNode BuyNewNode(DataType data) ;void PrintList(PNode pHead);void PushBack(PNode* pHead, DataType data);void PushFront(PNode* pHead, DataType data);void PopBack(PNode* pHead);void PopFront(PNode* pHead);PNode Find(PNode pHead, DataType data);void Insert(PNode pos, DataType data);void Destroy(PNode* pHead);PNode Back(PNode pHead);#endif __SEQLIST_H__

Function implementation SeqList.c

#include "SeqList.h"void InitList(PNode* pHead){ *pHead = NULL;}PNode BuyNewNode(DataType data) {PNode New = NULL; New = (PNode)malloc(sizeof(Node)); New->data = data; New->next = NULL; return New;}void PrintList(PNode pHead){ PNode Cur = pHead; while (Cur) {printf("%d ", Cur->data); Cur = Cur->next;} printf("
" );}void PushBack(PNode* pHead,DataType data){ assert(pHead!=NULL); PNode Cur = NULL; PNode NewNode = BuyNewNode(data); Cur = *pHead; if (*pHead == NULL) {* pHead = NewNode;} else {while (Cur->next) {Cur = Cur->next;} Cur->next = NewNode; }}void PushFront(PNode* pHead, DataType data){ assert(pHead != NULL) ; PNode Cur = *pHead; PNode NewNode = BuyNewNode(data); if (*pHead == NULL) {*pHead = NewNode;} else {NewNode->next = *pHead; *pHead = NewNode; }}void PopBack( PNode* pHead){ assert(pHead!=NULL); PNode Cur = *pHead; PNode DelNode = NULL; if (*pHead == NULL) {return;} if (Cur->next == NULL) {free(Cur ); *pHead = NULL;} while (Cur->next->next != NULL) {Cur = Cur->next;} //Cur->next = NULL; DelNode = Cur->next; free(DelNode) ; //free(*(Cur->next)); DelNode = NULL; Cur->next = NULL;}void PopFront(PNode* pHead){ assert(pHead!=NULL); if ((*pHead) == NULL)//No node {return;} else//There are multiple nodes {PNode Del = (*pHead)->next; //(*pHead ) = (*pHead)->next; free((*pHead)->next); Del = NULL; ))PNode Find(PNode pHead, DataType data){ assert(pHead != NULL); PNode Cur = pHead; while (Cur != NULL) {if (Cur->data == data) {return Cur;} Cur = Cur->next;} return NULL;}void Insert(PNode pos, DataType data){ assert(pos != NULL); PNode NewNode = BuyNewNode(data); if (pos->next == NULL) {pos->next = NewNode;} else {PNode Cur = pos->next; pos->next = NewNode; NewNode-> next = Cur; })// Insert(Find(&pHead, 4),5);void Destroy(PNode* pHead){ PNode Node = (*pHead); PNode NodeNext = Node->next; while (NodeNext != NULL ) {free(Node); Node = NodeNext; NodeNext = NodeNext->next;} free(Node); *pHead = NULL; Node = NULL;}PNode Back(PNode pHead){ //assert(pHead != NULL) ; PNode Cur = pHead; if (Cur == NULL) {return Cur;} while (Cur->next != NULL) {Cur = Cur->next;} return Cur;}

< p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5014 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.