Basic operation of single-linked list (C language)

Source: Internet
Author: User

The following is for the time being only a few basic operations to be continued.

First, Introduction

A linked list is a chained storage structure that dynamically allocates storage space.

The list includes a "head pointer" variable, where the No. 0 node is called the head node of the entire list, and the head node holds an address that points to an element where the head node generally does not hold specific data, only the address of the first node. Each element in a list is called a node, and each node consists of two parts: the data field that holds the data element, and the pointer field that stores the direct successor storage location. The pointer field stores the next node of the linked list where it is stored and is a pointer. Multiple nodes are linked into a linked list. The pointer field of the last node is set to null (NULL) as the end flag of the linked list, indicating that it has no successor nodes.

  

Second, the structure of the linked list
intstruct  node{    DataType data;     struct Node **pnode;
View CodeThird, the creation of linked list
///Create a nodepnode CreateNode (DataType value) {pnode node= (Pnode)malloc(sizeof(Node)); Node->data =value; Node->next =NULL; returnnode;}///Create a single linked listPnode createlist () {intlen, value; Pnode Phead= (Pnode)malloc(sizeof(Node));///head NodePhead->next =NULL; Pnode Ptail= Phead;///Moving NodesPuts"Please enter the number of nodes for the single linked list:"); scanf ("%d", &Len);  for(inti =0; i < Len; ++i) {scanf ("%d", &value); Pnode Pnode=CreateNode (value); Ptail->next =Pnode; Ptail=Pnode; }    returnPhead;}
View CodeIv. length of the linked list
/// Get the chain table length int getlistlength (Pnode phead) {    int0;      while (Phead->next! = NULL)  /// It actually calculates the number of head nodes to the second-to-last node, but is still the number of nodes     {        = phead->Next;        Len++    ;    } return Len;}
View CodeFive, whether the linked list is empty
/// determine if the linked list is empty BOOL isEmpty (Pnode phead) {    if (phead->next = = NULL        )returnfalse ;     Else        return true ;}
View CodeVI. node insertion of a linked list
///single-linked list insertion nodeBOOLInsertnode (Pnode phead, DataType value,intPOS) {Pnode PPOs=Phead; if(Pos <1|| Getlistlength (PPOs) <POS)return false;  for(inti =0; I < pos-1; ++i)///All you need is a pointer to the pos-1.{PPOs= ppos->Next; } pnode Pnode=CreateNode (value); Pnode->next = ppos->next;///when inserting a node, simply modify the next of the node to be inserted and the previous node of the insertion position .Ppos->next =Pnode; return true;}
View CodeVii. node deletion of linked list
///single-linked list delete nodeBOOLDeletenode (Pnode Phead,intPOS) {Pnode PPOs=Phead; if(Pos <1|| Getlistlength (PPOs) <POS)return false;  for(inti =0; I < pos-1; ++i) {PPOs= ppos->Next; }    if(pos = = Getlistlength (phead))///if the POS is exactly the node location of a single-node list, the node is disposed directly and the head node next is null    {         Free(ppos->next); PPOs->next =NULL; return true; } Pnode P= ppos->next->next;///Copy the pointer you want to release so that you can use it later     Free(Ppos->next);///releasing the pointer memoryPpos->next = p;///use a pointer that was previously copied    return true;}
View CodeViii. Integration
#include <stdio.h>#include<malloc.h>typedefintDatatype;typedefstructnode{DataType data; structNode *Next;} Node,*Pnode;///Create a nodepnode CreateNode (DataType value) {pnode node= (Pnode)malloc(sizeof(Node)); Node->data =value; Node->next =NULL; returnnode;}///Create a single linked listPnode createlist () {intlen, value; Pnode Phead= (Pnode)malloc(sizeof(Node));///head NodePhead->next =NULL; Pnode Ptail= Phead;///Moving NodesPuts"Please enter the number of nodes for the single linked list:"); scanf ("%d", &Len);  for(inti =0; i < Len; ++i) {scanf ("%d", &value); Pnode Pnode=CreateNode (value); Ptail->next =Pnode; Ptail=Pnode; }    returnPhead;}///traversing a single linked listvoidtraverselist (Pnode phead) {puts ("single-linked list traversal:"); Pnode Ptail=Phead;  while(Ptail->next! = NULL)///when traversing a node, the node returned at the end of each traversal is the next node{printf ("%d", ptail->next->data); Ptail= ptail->Next; } puts ();}///determine if the linked list is emptyBOOLisEmpty (Pnode phead) {if(Phead->next = =NULL)return false; Else        return true;}///Get the chain table lengthintgetlistlength (Pnode phead) {intLen =0;  while(Phead->next! = NULL)///It actually calculates the number of head nodes to the second-to-last node, but is still the number of nodes{Phead= phead->Next; Len++; }    returnLen;}///single-linked list insertion nodeBOOLInsertnode (Pnode phead, DataType value,intPOS) {Pnode PPOs=Phead; if(Pos <1|| Getlistlength (PPOs) <POS)return false;  for(inti =0; I < pos-1; ++i)///All you need is a pointer to the pos-1.{PPOs= ppos->Next; } pnode Pnode=CreateNode (value); Pnode->next = ppos->next;///when inserting a node, simply modify the next of the node to be inserted and the previous node of the insertion position .Ppos->next =Pnode; return true;}///single-linked list delete nodeBOOLDeletenode (Pnode Phead,intPOS) {Pnode PPOs=Phead; if(Pos <1|| Getlistlength (PPOs) <POS)return false;  for(inti =0; I < pos-1; ++i) {PPOs= ppos->Next; }    if(pos = = Getlistlength (phead))///if the POS is exactly the node location of a single-node list, the node is disposed directly and the head node next is null    {         Free(ppos->next); PPOs->next =NULL; return true; } Pnode P= ppos->next->next;///Copy the pointer you want to release so that you can use it later     Free(Ppos->next);///releasing the pointer memoryPpos->next = p;///use a pointer that was previously copied    return true;}intMain () {Pnode LinkedList=createlist ();    Traverselist (LinkedList); if(Insertnode (LinkedList,-2,3) {puts ("Insert Successful"); }    Else{puts ("Insert Failed");    } traverselist (LinkedList); if(Deletenode (LinkedList,3) {puts ("Delete succeeded"); }    Else{puts ("Delete Failed"); } traverselist (LinkedList);}
View Code

Basic operation of single-linked list (C language)

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.