Three elements of linked list
1) Head pointer (head): It is used to show that the linked list has started , The header pointer represents the linked list itself
So you need to access the linked list in the future , To access the header pointer
2) node (node): Each structure variable in the linked list
3) Tail pointer : Used to describe the end of the linked list ( It is a null pointer ,NULL)
#include<stdio.h> #include<string.h> typedef struct stud // Defines a structure { char Id[
10];// Job number char Name[10];// full name int pay;// wages int Age;// Age struct stud *Next; }STU;
// The type of this pointer is a structure type , In the future, this pointer can only point to struct stud such STU *CreatLink(STU a[], int n)
// To return the linked list is actually to return the header pointer { STU *head; int i; head = &a[0];
// Represents an array a The first element in is assigned to the header pointer , Let the header pointer point to the first node // Pass the previous node and the next node through a loop next Linked to a pointer for (i = 0; i < n
- 1; i++) { a[i].Next = &a[i + 1]; } a[n - 1].Next = NULL;// After exiting the cycle , Take the end pointer as NULL
return head;// Return header pointer , It actually represents the linked list itself } // Search of linked list void FindById(STU *head, char Id[])
// Find the specified job number in a linked list { // To access all nodes in the linked list STU *p = head;// Assign the head pointer of the linked list to p while (p!=NULL) {
//p It represents each node in the linked list , If p Point to that node id With what we're looking for id Same words // Then exit the loop if (strcmp(p->Id, Id) == 0) {
break;// Description found , There's no need to cycle } p = p->Next; } // After exiting the cycle , Then judge whether you found it if (p==NULL)// Can't find {
printf(" The bird man could not be found "); } else// Description found { // Output the value of the node pointed to by the pointer printf("%s\t%s\t%d\t%d\n", p
->Id, p->Name,p->pay,p->Age); } } // Delete Linked List //1: Find the node to delete //2: delete
//3: This function returns the new linked list after deletion , Actually return header pointer STU *DelById(STU *head, char Id[])// Delete a node in the corresponding linked list according to the job number
{ // The node must be found STU *p = head, *front; while (p!=NULL) { if (strcmp(p->Id,Id)==0)
// If p Of the node pointed to id Equal to the to be deleted id Words { break;// Just exit the loop } // stay p Before moving , To keep the previous position front = p; p = p->
Next;//p Move next node } if (p != NULL)// Description: find the node to delete { front->Next = p->Next; } return
head; } // Insert Linked List //1: Find the location to insert //2: Perform insert operation // Insert a node , And return the linked list after insertion STU *Insert(STU *
head, STU *pNewNode) { STU *p, *front; p = head; while (p!=NULL&&p->Age<pNewNode
->Age)// If cash back p If the age pointed to is younger than the age of the new node , Just keep cycling { front = p; p = p->Next; } // After exiting the cycle , Check p State of
if (p==head)// Indicates that the new node should be inserted at the beginning { // New node next To point to the previous head pNewNode->Next = head;
// new head To point to the inserted node head = pNewNode; } else { if (p == NULL)// Indicates that the new node should be inserted into the tail { front
->Next = pNewNode;// hold front of next Point to new node pNewNode->Next = NULL;// Then put the new node next Fu Wei NULL
} else // Insert in the middle ( stay p Insert in front of ) { // hold front of next Point to new node front->Next = pNewNode;
// Put the new node next point p pNewNode->Next = p; } } return head;// Return pointer , Is to return the linked list } // Output all node values in the linked list
void OutputLink(STU *head)// Parameter requires a linked list , A linked list actually needs a header pointer { STU *p = head;
// First give the head pointer of the linked list to the pointer p printf(" Job number \t full name \t wages \t Age \n"); while (p != NULL)// When the accessed node is less than the end {
printf("%s\t%s\t%d\t%d\n", p->Id, p->Name,p->pay,p->Age); p = p->Next;
// Put the pointer p Move to next node } printf("**************************\n"); } void main() { STU a[5]
= { {"001"," millet ",6000,20}, {"002"," Xiao Ming ",5000,30}, {"003"," trumpet ",3000,40}, {"004",
" Little fool ",30000,50}, {"005"," waiter ",20000,60} }, *head, NewNode = {"006"," Sample ",1900,29},*
pNewNode=&NewNode; char id[100]; // It's still an array , It's not a linked list yet , So call CreatLink To create a linked list head =
CreatLink(a, 5); OutputLink(head); //printf(" Please enter a job number :\n");
//gets_s(id,60);//gets(id); //FindById(head, id); //head = DelById(head,
id);// call DelById To delete a node , But I can't see the effect // In order to see the effect, you should output it again head = Insert(head, pNewNode);
OutputLink(head); }

©2019-2020 Toolsou All rights reserved,
( Super detail )Eclipse Using tutorials —— use Eclipse Create first HelloWorld! Database operation 5 code implementation mysql Addition, deletion, modification and query of database What can MCU do , Do you have any interesting works made by MCU or open source hardware Go to the interview after reading this , Steady over ~~ Single linked list of primary data structure (C Language implementation )SQL Comprehensive questions Employee unit comprehensive questions Python Implementation of Hanoi Tower code VHDL——JK trigger It's over , Starting salary 30k