C language data structure of the linked list to implement code _c language

Source: Internet
Author: User

Objective

Recently in the review of data structure of the relevant knowledge, feeling in the beginner's time or there are a lot of things do not grasp, but now finally be made more have a clue, so just write out and share with everyone!

What is a linked list

Simply put, the linked list is separated by multiple nodes, and each node is connected by a pointer, with only one precursor node and a subsequent node. The first node has no precursor nodes and is a storage structure with no subsequent nodes.

The structure of a linked list


Head node: The node in front of the first valid node of the linked list, the header node does not hold valid data, that is, the data field is empty, and the main purpose of the header node is to facilitate the operation of the linked list.

Initial node: The first valid node of a linked list that contains data fields and pointer fields.

Tail node: The pointer field of the tail node is empty.

Head pointer: A pointer variable that points to a head node that holds the address of the head node (note here that the pointer variable holds the address, which means that the head pointer holds the

The address of the head node, generally through the head pointer to the linked list operation.

Concrete implementation

#include <stdio.h> #include <malloc.h> #include <stdlib.h>//define the list node typedef struct Node {int data; Data domain struct Node * pnext;  Pointer field}node, * pnode;    node is equivalent to struct node, Pnode is equivalent to struct node *//function declaration Pnode createlinklist (void);   Create a list of functions void Traverselinklist (Pnode phead);    The function of traversing the list is bool IsEmpty (Pnode phead);    function int GetLength (Pnode phead) to determine whether the list is empty; Gets the function of the chain list length bool Insertelement (pnode phead, int pos, int val); A function that inserts elements into a list of three parameters, in turn, the Chain header node, the position to insert the element, and the value to insert the element bool Deleteelement (pnode phead, int pos, int * pVal);     A function that deletes an element from a list of three parameters, in turn, the Chain header node, the position of the element to be deleted, and the value of the deleted element, void sort (pnode phead);   The function that sorts the elements in a linked list (based on bubble sort) int main (void) {int val;  The element used to save the deletion pnode phead = NULL; Pnode equivalent to struct Node * Phead = Createlinklist (); Creating a Phead single linked list and assigning the head node address of the linked list to the traverselinklist (Phead); Call to traverse the list of functions if (IsEmpty (phead)) printf ("Linked list is empty!")
 \ n "); else printf ("The list is not empty!")
 \ n ");
 printf ("The length of the list is:%d\n", GetLength (Phead));
 Call Bubble sort function sort (phead);
 Re-traverse Traverselinklist (Phead); ToInserts an element at the specified position in the list if (Insertelement (Phead, 4), printf ("Insert succeeded! The elements inserted are:%d\n", 30);
 else printf ("Insert failed!\n");
 Re-traverse the linked list traverselinklist (phead);
 Delete element Test if (Deleteelement (Phead, 3, &val)) printf ("element deletion succeeded!) deleted elements are:%d\n", Val);
 else printf ("element deletion failed!\n");
 Traverselinklist (Phead);
 System ("pause");
return 0;
 } pnode createlinklist (void) {int length;//length of valid node int i; int value;
 Used to hold the value of the node entered by the user//creates a header node that does not hold valid data pnode Phead = (pnode) malloc (sizeof (NODE)); if (NULL = = Phead) {printf ("Memory allocation failed, program exits!")
 \ n ");
 Exit (-1); } pnode ptail = Phead; Ptail always point to the tail node ptail->pnext = NULL;
 Clear the Pointer field printf ("Please enter the number of links you want to create: len =");
 scanf ("%d", &length);
 for (i=0;i<length;i++) {printf ("Please enter the value of%d node:", i+1);
 scanf ("%d", &value);
 Pnode pnew = (pnode) malloc (sizeof (NODE)); if (NULL = = Phead) {printf ("Memory allocation failed, program exits!")
  \ n ");
 Exit (-1); } pnew->data = value; Put the value Ptail->pnext = Pnew to the new node; Pointing the tail node to the new node Pnew->pnext = NULL;  Clears the pointer field of the new node ptail = pnew; The new node is assigned to Ptail, so that the ptail begins.End point for Tail node} return phead;
 } void Traverselinklist (Pnode phead) {Pnode p = phead->pnext;
 while (NULL!= p) {printf ("%d", p->data);
 p = p->pnext;
 printf ("\ n");
Return
 BOOL IsEmpty (Pnode phead) {if (NULL = = Phead->pnext) return true;
else return false;  int GetLength (Pnode phead) {Pnode p = phead->pnext;   Point to first node int len = 0;
 A variable while (NULL!= p) {len++) that records the length of a list;  p = p->pnext;
P points to the next node} return len;   } void Sort (Pnode phead) {int len = getlength (phead);//Get list length int i, j, T;   Intermediate variable for exchanging element value Pnode p, q; Two intermediate pointer variables for comparison (i=0,p=phead->pnext; i<len-1 i++,p=p->pnext) {for (j=i+1,q=p->pnext;j<len;j++,q=
  Q->pnext) {if (P->data > Q->data) {t = p->data;
  P->data = q->data;
  Q->data = t;
}} return;
 BOOL Insertelement (pnode phead, int pos, int val) {int i = 0;
 Pnode p = phead;
 Node while (null!=p && i<pos-1) {p = p->pnext; that determines whether p is empty and causes p to eventually point to POS position
i++; } if (Null==p | | i>pos-1) return FALSE;
 Create a new node Pnode pnew = (pnode) malloc (sizeof (NODE));
 if (NULL = = pnew) {printf ("Memory allocation failure, program exit!\n");
 Exit (-1);
 } pnew->data = val;
 Defines a temporary node that points to the next node of the current p pnode q = p->pnext;
 Point p to new node p->pnext = pnew;
 Point Q to the node pnew->pnext = q before P points;
return true;
 BOOL Deleteelement (pnode phead, int pos, int * pVal) {int i = 0;
 Pnode p = phead;
 Determines if p is empty and causes p to eventually point to POS nodes while (Null!=p->pnext && i<pos-1) {p = p->pnext;
 i++;
 } if (Null==p->pnext | | i>pos-1) return FALSE;
 Save the node you want to delete * PVal = p->pnext->data;
 Delete the node after p pnode q = p->pnext;
 P->pnext = p->pnext->pnext;
 Free (q);
 Q = NULL;
return true;

 }

End language

The main implementation of the above is a single linked list, in addition to a double linked list, circular chain list, non-circular linked list and other several other common linked lists. The particularity of the double linked list has two pointer fields in each basic node; the characteristics of the cyclic list are mainly shown in the circular chain list, in which all other nodes can be found through any node.

Thank you for your reading, I hope to help you, thank you for your support for this site!

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.