Single-linked list insertion pseudo-algorithm and C language to create a single linked list, and traverse

Source: Internet
Author: User

A pseudo-algorithm for inserting nodes in non-cyclic single-linked list



Q Insert pseudo-algorithm after p:
The first method of presentation:
r = p->pnext; P->pnext represents the pointer field to the node, and the pointer field is the address that points to the next node.
P->pnext = q; Q Saves the address of the node. Q is a pointer variable that stores the address of that node.
Q->pnext = R;

The second method of presentation:
Q->pnext = p->pnext; The pointer field of Q points to a node after p.
P->pnext = q; The pointer field of P points to Q

To delete a non-cyclic single-linked table node pseudo-algorithm:
Remove one of the nodes behind P
r = p->pnext;
P->pnext = p->pnext->pnext;
Free (R); Freeing memory

Demo of linked list creation and linked list traversal algorithms:

Create a single-linked list, and then traverse.
#include <stdio.h>#include<malloc.h>#include<stdlib.h>/*Create a single-linked list, and then traverse. */typedefstructnode{intData//data fields    structNode *pnext;//pointer Field}node, *pnode;//node is equivalent to struct node, pnode equivalent to struct node *//function DeclarationPnode Create_list (void);voidtraverse_list (Pnode phead);intMainvoid) {Pnode phead= NULL;//equivalent to struct Node *phead = NULL;Phead= Create_list ();//Create a non-circular single-linked list and assign the address of the head node of the list to Phead//traversing a single linked listtraverse_list (Phead); return 0;}/*creates a non-circular single-linked list that returns the address of the single-linked header node*/Pnode create_list (void){    intLen//number of valid nodes to store    inti; intVal//the value of the node used to temporarily store user input//Assigning a head node that does not hold valid dataPnode Phead = (pnode)malloc(sizeof(NODE)); if(Phead = =NULL) {printf ("memory allocation failed, program terminated! "); Exit (-1); }    //Ptail represents the tail node, and the pointer field is null .Pnode Ptail =Phead; Ptail->pnext =NULL; printf ("Please enter the number of single-linked table nodes you need to generate: Len ="); scanf ("%d", &Len);  for(i =0; I < len;i++) {printf ("Please enter the value of%d nodes: \ n", i +1); scanf ("%d", &val); Pnode pnew= (Pnode)malloc(sizeof(NODE)); if(Pnew = =NULL) {printf ("memory allocation failed, program terminated! "); Exit (-1); } pnew->data =Val; //to hook the newly generated node to the Pnext pointer field//Ptail represents the last node, and the pointer field is null .Ptail->pnext =pnew; Pnew->pnext =NULL; Ptail=pnew; }    returnPhead;}//traversing a single linked listvoidtraverse_list (Pnode phead) {Pnode P= phead->Pnext; //if p is not empty, it indicates that P already points to a node,//if P is empty, indicates that the single-linked list is empty and points to an empty head node .     while(P! =NULL) {printf ("%d", p->data); P= p->pnext;//p Move Backward    }}

Single-linked list insertion pseudo-algorithm and C language to create a single linked list, and traverse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.