C language to create and operate a single linked list of data structure Examples Tutorial _c language

Source: Internet
Author: User
Tags arrays

1, why use the linked list

As a collection of similar data, the array gives us a lot of convenience in the program design and increases the flexibility. But the array also has some drawbacks. The size of an array is defined in advance and cannot be adjusted in the program, so it is sometimes necessary to have 3 0 size arrays for different problems in programming, sometimes 5 to 0 arrays, and difficult to unify. We can only define arrays based on the maximum possible requirements, which often result in a waste of some storage space.

We want to construct a dynamic array that can resize the array at any time to meet the needs of different problems. The list is the dynamic array we need. It is in the process of execution of the program in accordance with the need for data storage to the system requirements to request storage space, in no way constitute a waste of storage area.

The linked list is a kind of complex data structure, and the relationship between the data makes the list divided into three kinds: single linked list, circular chain list, bidirectional linked list, and the following will be introduced.

2, one-way linked list

A single linked list has a header node head that points to the first address of the linked list in memory. The data type of each node in the list is a struct type. A node has two members: an integral member (the actual data to be saved) and a pointer to the next struct type node (in fact, the single linked list is a dynamic array for storing integer data). Linked list According to this structure, access to each node needs to be found from the head of the linked list, and the address of subsequent nodes is given by the current node. Regardless of which node is accessed in a table, you need to start from the head of the list and look backwards in order. The tail node of the linked list has no subsequent nodes, its pointer field is NULL, and writing is null.

As shown in the figure:

The above figure also gives such a layer of meaning, the list of nodes in the memory of the storage address is not continuous, the address of each node is in need of the system to apply for allocation, the system according to the current situation of memory, can be continuously allocated addresses, can also be a jump-type distribution address.

3, the realization of one-way linked list program
(1), the data structure definition of the linked list node

struct node 
{ 
  int num; 
  struct node *p; 
} ; 

In the definition of a linked list node, except for members of an integral type, member P is a pointer to exactly the same type of node.

A very special point in the data structure of a linked list node is that the data type of the pointer field in the structure body uses a data type that is not defined successfully. This is the only data structure in C that can be defined in the first use.

(2), the creation of the linked list, output steps
the process of creating a single linked list has the following steps:

1) Define the data structure of the linked list;

2 Create an empty table;

(3) using malloc () function to assign a node to the system application;

4 assigns the pointer member of the new node to null. If the table is empty, connect the new node to the header, or if the table is not empty, the new

node receives the footer of the table;

5 to determine whether there are follow-up nodes to access the linked list, if there is a transfer to 3, or end;

The output process of a single linked list has the following steps

1) Find the table head;

2 If the table is not empty, the value of the output node member, is an empty table exit;

3 Tracking the growth of the list, that is to find the next node address;

4) go to 2).

(3), program code example:
Create a single list of positive integers, enter 0 or less than 0, end the Create list, and print out the values in the list, as follows:

#include <stdlib.h>//* with MA l o C () header file/#include <stdio.h>//① defines the linked list data structure struct node {int num; 
struct node *next; 
};  
function declaration struct node *creat (); 
void print (); 
  Main () {struct node *head;  Head=null; ② Build an empty table head=creat (head);/* Create a single linked list/print (head);/* Printed single link/}/******************************************/struct 
  Node*creat (struct node *head)/* Returns a pointer of the same type as the node/{struct NODE*P1,*P2; 
int i=1; ③ uses the malloc () function to assign a node to the system request p1=p2= (struct node*) malloc (sizeof (struct node));//printf ("Please enter a value, the value is less than or equal to 0, and the value is stored as: P 
  1_addr=%d\n ", p1); scanf ("%d", &p1->num);/* Input node value/* p1->next=null;/* the pointer of the new node to the null/while (P1-&GT;NUM&GT;0)/* Input node value greater than 0*/{// ④ assigns the pointer member of the new node to null.  
    If the table is empty, connect the new node to the header, or the new node to the footer if the table is not empty. 
 
    if (head==null) head=p1;/* empty table, Access table head/Else p2->next=p1;/* not empty table, received the table tail * * P2=P1; 
    p1= (struct node*) malloc (sizeof node); * * Next new node/struct; printf ("Please enter a value, the value is less than or equal to 0 ends, the value is stored at the address: p%d_addr=%d\n", I,P2);  
  scanf ("%d", &p1->num);/* Input node Value * *//⑤ determine if there are subsequent nodes to access the linked list, if there is a transfer to 3, or end; //============== Original Program Correction section: (thank @daling_datou Reminder) ================================ free (p1);  The application is not entered, so release the P1=null; Make point null p2->next = null;  
To the end of the table, point to empty printf ("End of list input"); ============================================== return head;/* Returns the head pointer of the list/}/************************************* 
  /void print (struct node*head)/* The value of each node of the list with head heads/{struct node *temp; 
  temp=head;/* gets the head pointer of the list/printf ("The value of the \n\n\n linked list is: \ n"); 
  while (Temp!=null)//As long as the Non-empty table/{printf ("%6d\n", temp->num);/* Output list node value * * temp=temp->next;/* tracking List Growth * * 
printf ("Linked list printing end!!"); 
 }

The head pointer of a linked list is a very important parameter during the creation of a linked list. Because the output of the list and lookup are to start from the head of the list, so the linked list after the successful creation, to return a linked table header node address, that is, the head pointer.

Program execution Flow:

4, the single linked list Operation Foundation example

#include <stdio.h> #include <malloc.h> #define LEN sizeof (node) typedef struct _NODE//node Declaration {int Val 
  ; 
struct _node* next; 
 
NODE, *pnode; 
    void print (Pnode head) {//Print all nodes while (head) {printf ("%3d", head->val); 
  Head = head->next; 
printf ("\ n"); 
  } void Inserthead (Pnode *phead, int val) {//head interpolation pnode n = (pnode) malloc (LEN); 
  N->val = val; 
  N->next = *phead; 
*phead = n; 
  } void Inserttail (Pnode *phead, int val) {//trailing interpolation pnode t = *phead; 
  Pnode n = (pnode) malloc (LEN); 
  N->val = val; 
  N->next = NULL; 
    if (*phead = = NULL) {n->next = *phead; 
  *phead = n; 
    }else{while (t->next) {t = t->next; 
  } T->next = n; 
  } void Deletehead (Pnode *phead) {//delete header if (*phead = = NULL) {return; 
    else {Pnode T = *phead; 
    *phead = (*phead)->next; 
  Free (t); 
} void Deletetail (Pnode *phead) {//delete tail Pnode t = *phead;  if (t = = NULL) {return; 
    else if (T->next = NULL) {free (t); 
  *phead = NULL; 
    } else{while (T->next->next!= NULL) {t = t->next; 
    Free (t->next); 
  T->next = NULL;  
    } pnode Findbyval (pnode head, int val) {//The first node that satisfies the condition is found by value (head!= NULL && head->val!= val) { 
  Head = head->next; 
return head; 
  } pnode Findbyindex (pnode Head, int index) {/////Index to find node if (index = = 1) {return head; 
    else {int c = 1; 
      while (head!= NULL && index!= c) {head = head->next; 
    C + +; 
} return head; 
  } void Insertbyindex (Pnode *phead, int index, int val) {////PER Index Insert node if (index = = 1) {Inserthead (Phead, Val); 
    else {Pnode T = findbyindex (*phead,index-1); 
    if (t = = NULL) {return; 
      else {Pnode n = t->next; T->next = (pnode) malloc(LEN); 
      T->next->next = n; 
    T->next->val = val; 
  }} void Deletebyindex (Pnode *phead, int index)//To delete node {if (index = = 1) {deletehead (Phead) based on node index; 
    else {pnode t= findbyindex (*phead, index-1); 
    if (t = = NULL | | t->next = NULL) {return; 
      }else{Pnode n = t->next->next; 
      Free (t->next); 
    T->next = n; 
  } void Deletebyval (Pnode *phead, int val)//Delete the first {if (*phead = = NULL) that satisfies the condition according to the value if the empty table exits {return; 
    else {if ((*phead)->val = = val)//If the first is, delete the header {deletehead (phead); 
      else {Pnode T = *phead; while (T->next!= NULL && t->next->val!= val)/traversal, exit {t = T->n if T's next is empty or next is the node to be found 
      Ext } if (T->next)//If T points to the last node of the point to be found {pnode n = t->next->next;//Delete the node free (t->n 
        EXT); 
      T->next = n; 
  }  "} Void Clear (Pnode *phead)/Purge list {while (*phead)!= NULL) {deletehead (phead);//Remove from the beginning}} 
 
  void Main () {Pnode head = NULL; 
  Inserttail (&head,1); 
  Deletehead (&head); 
  Inserttail (&head,2); 
  Inserttail (&head,3); 
  Inserttail (&head,4); 
  Inserttail (&head,5); 
 
  Inserttail (&head,6); 
  Print (head); 
  Insertbyindex (&head, 6, 9); 
  Print (head); 
  Deletebyindex (&head,3); 
  Deletebyval (&head, 2); 
  Print (head); 
  Clear (&head); 
  Print (head); 
  Insertbyindex (&head,1,12); 
Print (head); 
 }

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.