C Language Structure linked list

Source: Internet
Author: User

Original link: http://zhina123.blog.163.com/blog/static/417895782012106036289/

Referring to its own structure, a struct with one or more members in its base type is the struct type, which means that the struct can refer to itself, so it is called a struct that references itself.

For example, the following structure:

struct Char struct link *p} A;

P is a pointer member that can point to a struct link-type variable, so that a.p=&a is a valid expression. So what's the point?

The implication is that we can link the stored data items with a struct member (which, of course, consumes the memory space of that storage pointer), and see the program below.

#include <stdio.h>structnode{intdata;structNode *next;}; Main () {structNode a,b,c,*p;//we assume that the struct variables a, b, and C in this declaration are not in memory.A.data=Ten; B.data= -; C.data= -; A.next=&b; b.next=&c; c.next=' /'; p=&A;//struct variable a address assigned to P  while(p) {printf ("%d",p->data); //after each output value, p automatically points to the link item for this item/*in this way, a member that keeps the link alone can logically store the non-adjacent storage units together.*/P=p->Next;} printf ("\ n");}

Such a chain of data storage form is called linked list! The method that forms the list above is human-defined and does not generate a new storage unit during program execution, so it is also called a "static linked list".

Here is a more method to use the "Dynamic linked list"

The previous log mentions that C dynamic storage allocation provides "memory on demand" Implementation method, there is a problem is, if multiple dynamic allocation of storage units, the address of each storage unit is not necessarily continuous, and the need to process the batch data is often a whole, there is a sequential relationship between the data, so that We can use the list above to connect dynamic storage units logically, rather than physically, to achieve the order relationships we need. At this point, the link list technology and dynamic storage allocation together, so here we gave the list of a new noun called "Dynamic linked list"

Naturally, the list in the example above has only one pointer to the following data item, if there are two? A finger, a finger before a point, so that there will be "two-way list" and "one-way list" difference

Below we mainly look at one-way linked list

In fact, the data (that is, the structure) of each cell in the single list is the same format type (including data members and pointer members)

As follows:

struct abc{int data,...... struct abc *next;};

There are several types of algorithms related to unidirectional lists:

Set up link Table output node data Insert node Data Delete node data.

The following program is an example

#include <stdio.h>#include<stdlib.h>structslist{intDatastructSlist *next;}; typedefstructslist slist; SLIST*creat_slist () {/*The function is to create a dynamic linked list, the return value of the function is a struct pointer variable, that is, the newly created dynamic linked list of the head node, it should be noted that the head node here has no data, only the pointer next point to the first node of the dynamic list*/ intC;/*used to temporarily store data in a struct*/SLIST*h,*s,*R;/*declaring work Pointers*/h= (SLIST *)malloc(sizeof(SLIST)); /*dynamically acquiring a structure's storage space*/R=h;/*The struct pointer r is used to use the H pointer unchanged in the following loop*/scanf ("%d",&c);/*get a struct member int data*/   while(c!=-1){ /*if the resulting int data is not 1, enter the loop*/s= (SLIST *)malloc(sizeof(SLIST)); /*using the structure pointer s in the loop to get the node.*/s->data=C;/*s Member data is C note that the first time you enter the loop, this C is entered on the outside of the loop*/R->next=s; /*The r pointer is the same as H, and the member of R is not pointing before entering the loop and now enters the loop to get a node, and the next point of R points to the new node, so that the H-head node points to the S*/R=s;/*R is the same as the newest node in turn, in order to point to the next obtained node with the latest storage space next*/scanf ("%d",&c); } R->next=' /'; /*after exiting the loop, R points to the last node, and the member of the last node next points to*/ returnh;/*returns the head node pointer of a dynamic linked list*/}voidPrint_slist (Slist *head) {/*the function is to output the node of the dynamic linked list sequentially, and the parameter is the structure pointer variable, that is, the head node of the dynamic linked list .*/SLIST*p;/*declare a working pointer, because head cannot move backwards, so use the pointer p to implement*/P=head->Next;if(p==' /') printf ("linklist is null!\n");/*Empty linked list*/  Else{printf ("Head");/*non-empty linked list*/          Do{printf ("->%d",p->data); P=p->next;} while(p!=' /'); printf ("->end\n"); }}slist*insert_snode (SLIST *head,intXinty) {/*This function implements inserting a node in front of a node with a linked list value of x, with a value of three link header nodes for the y parameter, and a value of x, y*/SLIST*s,*p,*Q;/*Defining work Pointers*/s= (SLIST *)malloc(sizeof(SLIST)); S->data=y;/*The above two sentences first acquires a dynamic storage space for a struct and assigns its member data to Y, but at this point the space does not become a node of the linked list .*/Q=Head; P=head->Next;/*The above two sentences initialize the working pointer, that is, the work pointer q is the same as head, p points to the head of the next*/  while((p!=' /') && (p->data!=x)) {  /*This loop is for finding the location of the node where the value is x, it is important to note that the two conditional locations here cannot be changed, because the data member value is x if the P point is not NULL, otherwise if the point of P is null, The program must first determine if the P-point data is x this will take place an address access error, because P does not point to the node, there is no way to talk about member data, so it is wrong to determine whether the data p points to x*/Q=p;p=p->Next; /*if the loop condition is met, p and Q move backwards until a node with a value of x is found or the end of the list is reached.*/} s->next=p; /*is inserted in front of the node that the P points to, so the next point of the new node points to P*/Q->next=s; /*Q-next, which points to the P-node, now points to the S-node, enabling the insertion*/   returnHead/*the head pointer does not change and returns to*/}slist*insert_bnode (SLIST *head,intXinty) {/*This function implements inserting a node after a node with a linked list value of x, with a value of three link header nodes for the y parameter, and a value of x, y*/SLIST*s,*p,*Q;/*Defining work Pointers*/s= (SLIST *)malloc(sizeof(SLIST)); S->data=y;/*The above two sentences first acquires a dynamic storage space for a struct and assigns its member data to Y, but at this point the space does not become a node of the linked list .*/Q=Head; P=head->Next;/*The above two sentences initialize the working pointer, that is, the work pointer q is the same as head, p points to the head of the next*/  while((p!=' /') && (p->data!=x)) {  /*This loop is for finding the location of the node where the value is x, it is important to note that the two conditional locations here cannot be changed, because the data member value is x if the P point is not NULL, otherwise if the point of P is null, The program must first determine if the P-point data is x this will take place an address access error, because P does not point to the node, there is no way to talk about member data, so it is wrong to determine whether the data p points to x*/Q=p;p=p->Next; /*if the loop condition is met, p and Q move backwards until a node with a value of x is found or the end of the list is reached.*/} s->next=p->Next; /*is inserted after the node that the P points to, so the next point of the new node points to P*/P->next=s; /*p-next the node behind P, which now points to the S node, implements the post-insertion*/   returnHead/*the head pointer does not change and returns to*/}slist*del_node (SLIST *head,intx) {/*This function implements the deletion of a node with a list value of x, with a parameter of two linked header nodes, and a value of x*/SLIST*s,*p,*Q;/*Defining work Pointers*/Q=Head; P=head->Next;/*The above two sentences initialize the working pointer, that is, the work pointer q is the same as head, p points to the head of the next*/  while((p!=' /') && (p->data!=x)) {  /*This loop is for finding the location of the node where the value is x, it is important to note that the two conditional locations here cannot be changed, because the data member value is x if the P point is not NULL, otherwise if the point of P is null, The program must first determine if the P-point data is x this will take place an address access error, because P does not point to the node, there is no way to talk about member data, so it is wrong to determine whether the data p points to x*/Q=p;p=p->Next; /*if the loop condition is met, p and Q move backwards until a node with a value of x is found or the end of the list is reached.*/} q->next=p->Next; /*put Q->next into P->next,*/   Free(P); /*frees the storage space for p to delete*/ returnhead;/*the head pointer does not change and returns to*/}main () {SLIST*head;intx, y, head=creat_slist ();//Create a linked list functionPrint_slist (head); printf ("Please input x\n"); scanf"%d",&x); printf ("Please input y\n"); scanf"%d",&y); Head=insert_snode (Head,x,y);//Insert function before nodePrint_slist (head); printf ("Please input x\n"); scanf"%d",&x); printf ("Please input y\n"); scanf"%d",&y); Head=insert_bnode (Head,x,y);//Insert Function after nodePrint_slist (head); printf ("Please input x\n"); scanf"%d",&x); Head=del_node (HEAD,X);//Delete node functionPrint_slist (head);}

C Language Structure linked list

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.