The following is the algorithm of the Operation list, which is a dynamic single linked list.
The list is indexed by the head pointer, the head pointer points to the head node, the head node points to the first node, and so on, until the tail node.
No data is stored in the head node, only pointers to the first node are stored.
The purpose of the header node is to facilitate the operation of the linked list, if the head node is not set, but directly from the pointer to the first node,
This makes the insertion of the delete operation on the node after the right pointer different from that of the other nodes, and is analyzed as a special case
Operating system: Ubuntu
Compiling software: GCC
Results screenshot:
Source:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct NODE {int da
Ta
struct Node *pnext;
}node,*pnode;
Pnode create_list ();
void Traverse_list (Pnode);
BOOL Is_empty (Pnode);
int length_list (pnode);
void Sort_list (Pnode);
BOOL Insert_list (pnode,int,int);
BOOL Delete_list (Pnode,int,int *);
void Clear_list (Pnode);
int main (void) {int len;
int Data_del;
Pnode phead = NULL;
Create a linked list and traverse the output Phead = Create_list ();
Traverse_list (Phead);
The length of the chain table is obtained and the output len = length_list (Phead);
if (!is_empty (phead)) printf ("The length of the list is:%d\n", Len);
Inserts data into the list and iterates through the output if (Insert_list (phead,3,78)) printf ("Insert Succeed,");
else printf ("Insert failed,");
Traverse_list (Phead); Delete the data from the list and iterate through the output//column for more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/if (delete_list Phead,3,&data_del)) {printf ("Delete succeed,the deleted data is:%d\n", Data_del);
else printf ("Delete failed,");
Traverse_list (Phead);
Sorting the linked list, iterating the output sort_list (phead);
printf ("After sorted,");
Traverse_list (Phead);
Empty the list, traverse the output (no data output) clear_list (Phead);
printf ("After cleared,");
Traverse_list (Phead);
return 0;
}//Child functions: Create a linked list and return the header pointer Pnode create_list () {int val;
Pnode Phead = (pnode) malloc (sizeof (NODE));
Pnode pcurrent = Phead;
Pcurrent->pnext = NULL;
if (NULL = = Phead) {printf ("Phead malloc failed!");
Exit (-1);
printf ("Input: Q to Quit)";
while (scanf ("%d", &val) ==1) {Pnode pnew = (pnode) malloc (sizeof (NODE));
if (NULL = = pnew) {printf ("pnew malloc failed!");
Exit (-1);
} pnew->data = val;
Pcurrent->pnext = pnew;
Pnew->pnext = NULL;
Pcurrent = pnew;
printf ("Input Next data (Q to Quit):");
return phead; }//Childfunction: Traversal list void traverse_list (Pnode phead) {Pnode pcurrent = phead->pnext;
printf ("Now Dataes in the list are:\n");
while (pcurrent!= NULL) {printf ("%d", pcurrent->data);
Pcurrent = pcurrent->pnext;
printf ("\ n");
return;
}//Child functions: Determine if the list is an empty bool Is_empty (Pnode pnode) {if (NULL = Pnode->pnext) return true;
else return false;
}//Child function: To find the length of the chain (excluding the head node) int length_list (Pnode pnode) {int count = 0;
Pnode pcurrent = pnode->pnext;
while (pcurrent!= NULL) {count++;
Pcurrent = pcurrent->pnext;
return count;
}//Child functions: Bubble to sort the list void Sort_list (Pnode phead) {Pnode p,q;
int temp; for (P=phead->pnext;p!=null;p=p->pnext) for (q=p->pnext;q!=null;q=q->pnext) {if p->data>
; q->data) {temp = p->data;
P->data = q->data;
Q->data = temp;
} return;}//Child function: Inserts a new node after the POS node in which the data in the node is Val bool Insert_list (Pnode phead,int pos,int val) {int i = 0;
Pnode p = phead;
I 0 o'clock, p points to the No. 0 node (this refers to the head node without actual data, excluding the total number of linked list nodes),//i is 1 o'clock, p points to 1th node, I is a few, p points to the first node while (P!=null && i<pos) {
p = p->pnext;
i++;
This happens if the value of the POS is greater than the linked list length or pos<0 (I>pos | | p==null) return FALSE;
Pnode pnew = (pnode) malloc (sizeof (NODE));
if (NULL = = pnew) {printf ("pnew malloc failed!");
Exit (-1);
} pnew->data = val;
Pnew->pnext = p->pnext;
P->pnext = pnew;
return true;
}//Child functions: Deletes the POS node and saves the deleted data at the location pointed to by the PData pointer bool Delete_list (pnode phead,int pos,int *pdata) {int i = 0;
Pnode p = phead; P eventually points to the node in front of the POS node, where the same is the No. 0 node with the head node//If the following two sentences are changed to while (P!=null && i<pos) and if (I>pos-1 | | p==null), Then p eventually points to the POS node, so that because the node in front of the POS node is not reachable, the two nodes before and after the POS cannot be connected while (P->pnext!=null && i<POS) {p = p->pnext;
i++;
This happens when the value of the POS is greater than the length of the list or pos<1, and the No. 0 node, the header, cannot be deleted.
if (i>pos-1 | | p->pnext==null) return FALSE;
Pnode q = p->pnext;
*pdata = q->data;
P->pnext = p->pnext->pnext;
Free (q);
Q = NULL;
return true;
}//Child functions: Empty the list, even if the list is left with only the head node (no data in the head node) void Clear_list (Pnode phead) {Pnode p = phead->pnext;
Pnode r = NULL;
while (P!= NULL) {r = p->pnext;
Free (p);
p = r;
} phead->pnext = NULL;
return; }
.
Author: csdn Blog Lan pavilion Wind and Rain