C / C ++ Note 1: Introduction & application list

1. Why to list

     Array as a collection of similar data storage, has brought us a lot of convenience in a program designed to increase flexibility. However, the array also has some shortcomings. Such as the size of the array when you define to be specified in advance and can not be adjusted in the program, so that, in the programming sometimes requires an array of 30 sizes for different problems, sometimes the size 50 array, it is difficult unity. We are only able to define an array according to the maximum possible demand, often resulting in waste some storage space.

     We wish to construct a dynamic array, you can always adjust the size of the array, in order to meet the needs of different issues. The list is dynamic array we need. It is during the execution of the program requires the data storage system to the storage space requirements of the application, in no way constitutes a waste storage area. FIG.

Is a list of complex data structures, the relationship between the data which make the list into three types: a single linked list, a circular linked list, a doubly linked list, one by one will be described.

2. singly linked list

A head node has a single linked list head, pointing to the list in the first memory address. Data type of the list structure for each node type, a node has two members: a member integral (actual data needs to be saved), and a pointer to the structure type of the next node, i.e. a node address (in fact, this single list is a dynamic array for storing integer data). Click to find from the list structure for an access to each node from the head of the list, the address is given by the subsequent node the current node. Whether that access a node in the table, we need to start from the head of the list, sequential search backwards. Tail node list since no successor node, which pointer field is empty, writing to NULL.

as the picture shows

The figure also shows the meaning of such a layer, each node in the linked list of memory storage address is not continuous, each node address of its application is assigned to the system when necessary, the system according to the current situation of the memory, either continuously or allocated address, you can jump to assign addresses.

3. implement a one-way linked list program

Data structure defines (1) a list node

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


In the definition of the linked list node, except for an integer member, p is a pointer to a member of the same type of node pointers.

List node in the data structure, the data point is a very special type of pointer field structures in the body using data type defined success. This is the only after a predetermined data structure definition may be first used in C.

 

(2) create a linked list output step

The creation of a single list has the following steps:

        1- linked list data structure definitions;

        2- Create an empty table;

        3- using malloc () function for the allocation of a node to the system;

        4 - A member of the new node pointer assignment is empty. If empty, the new node is connected to the header; if non-empty, a new node to the end of the table;

        5- determines whether there are subsequent to the access node list, if go 3-, or ends;

Single linked list output process has the following steps

       1- find the header;

       If the value of the non-members of 2- empty, the output node, the table is empty then exit;

       3- track chain growth, i.e., find the address of the next node;

       4- to 2-

(3) Examples of program code

Create a single list stored positive integer, input number 0 or less than 0, the end of the list to create and print out the value in the list, the procedure is as follows:

#include <stdlib.h> /*含ma l l o c ( ) 的头文件*/  
#include <stdio.h>  
 //①定义链表数据结构  
struct node  
{  
    int num;  
    struct node *next;  
};  
//函数声明  
struct node *creat();   
void print();  
main( )  
{  
  
    struct node *head;  
    head=NULL;    //②建一个空表  
    head=creat(head);/*创建单链表*/  
    print(head);/*打印单链表*/  
}  
/******************************************/   
struct node*creat(struct node *head)/*返回的是与节点相同类型的指针*/  
{  
    struct node*p1,*p2;  
    int i=1;  
//③利用malloc ( )函数向系统申请分配一个节点  
    p1=p2=(struct node*)malloc(sizeof(struct node));/*新节点*/   
    printf("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %d\n",p1);  
    scanf("%d",&p1->num);/*输入节点的值*/  
    p1->next=NULL;/*将新节点的指针置为空*/  
    while(p1->num>0)/*输入节点的数值大于0*/  
    {  
//④将新节点的指针成员赋值为空。若是空表,将新节点连接到表头;若是非空表,将新节点接到表尾;   
        if(head==NULL)  
            head=p1;/*空表,接入表头*/  
        else   
            p2->next=p1;/*非空表,接到表尾*/  
        p2=p1;  
  
        p1=(struct node*)malloc(sizeof(struct node));/*下一个新节点*/  
        i=i+1;  
        printf("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR= %d\n",i,p2);  
        scanf("%d",&p1->num);/*输入节点的值*/  
//⑤判断一下是否有后续节点要接入链表,若有转到3 ),否则结束;   
    }  
//==============原来程序更正部分:(多谢@daling_datou提醒)================================  
    free(p1);  //申请到的没录入,所以释放掉    
    p1=NULL;   //使指向空    
    p2->next = NULL; //到表尾了,指向空    
    printf("链表输入结束(END)\n");    
//==============================================  
    return head;/*返回链表的头指针*/  
}  
/*******************************************/  
void print(struct node*head)/*出以head为头的链表各节点的值*/  
{  
    struct node *temp;  
    temp=head;/*取得链表的头指针*/  
  
    printf("\n\n\n链表存入的值为:\n");  
    while(temp!=NULL)/*只要是非空表*/  
    {  
        printf("%6d\n",temp->num);/*输出链表节点的值*/  
        temp=temp->next;/*跟踪链表增长*/  
    }  
    printf("链表打印结束!!");  
}  

In the process of creating the list, the head pointer list is a very important parameter. Because must start from the head of the list of output and find the list, so the list is created, the return address to a list head node, that is, the head pointer.



 

In addition, it also opened a public personal number: JiandaoStudio, regularly publishes industry information in a public number, and a variety of free code books, master classes resources.

 

                                            

I am concerned about the scan code micro-channel public number, Austria surprise! No public regularly send exquisite articles every day! Keywords kinds of programming available to respond to the massive development of learning materials!

For example: Python want to get entry to the proficient learning materials, please reply to Python keyword.

 

Guess you like

Origin blog.csdn.net/weixin_41213648/article/details/93383977