List learning (written confused you (yourself))

List

Simple configuration list : a head pointer (Header), a plurality of nodes (node comprises a pointer field and a data field), to the last node to null (NULL).
The principle : the head pointer to the first node in the list, then the first node pointer field points to the next node, and sequentially refers to the last node, this constitutes a chain.
Linked list data structure:

struct list_node
{
    int data;//数据域,用于存储数据
    struct list_node *next;//指针,可以访问节点数据,也可以遍历,指向下一个节点
};

Use the list: I do not know how to declare?
1. The first statement head pointers: list_single node*=NULL;
2 and then to open up space for: node=(list_single*)malloc(sizeof(list_single));
3. Use memset library functions to quickly emptied structure:
void *memset(void *s,int c,size_t n)-> it means that:
the memory space has been opened up to a value of the first value s n bytes c.
That is: memset(node,0,sizeof(list_single));//清理一下
4. Assignment list:

node->data=100;
node->next=NULL;

5. Use the list: printf("%d\n",node->data);
6. freed space, the program ends.free(node);

Create a node:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list_node
{
    int data;
    struct list_node *next;
};
typedef struct list_node list_single;
int main(void)
{
    list_single *node=NULL;
    node=(list_single*)malloc(sizeof(list_single));
    if(node==NULL){//?
        printf("malloc fair!");
    }
    memset(node,0,sizeof(list_single));
    node->data=100;
    node->next=NULL;
    printf("%d,%s",node->data,node->next);
    return 0;
}

It just creates a linked list of nodes, the process we can create a package to function, you can call when you create!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list_node
{
    int data;
    struct list_node *next;
};
typedef struct list_node list_single;
list_single *creat_list_node(int data);//返回一个指针,即地址!
int main(void)
{
    int data=100;
    list_single *node=creat_list_node(data);
    printf("%d,%s",node->data,node->next);
    return 0;
}
list_single *creat_list_node(int data)
{
    list_single *node=NULL;
    node=(list_single*)malloc(sizeof(list_single));
    memset(node,0,sizeof(list_single));
    node->data=data;
    node->next=NULL;
    return node;
}

We are now just create a list node, then this single-node linked list in memory looks like this: define a pointer to the head (data, next) this data field, the data field next pointer field points to a NULL.

* node)->(data,next)->NULL

Let's create a single list multiple nodes to achieve CRUD! ! !
1. First, a single top linked list data structure!

Same as above, just put the above crate_list_node function can be created.
2. End plug :( singly linked list chain connection)
that is implemented: header-> next = new
Step: acquiring position of the current node, i.e. the first access node

struct list * p=header;

Step two: determining whether a node is the last, if not, moves to the next node, if so, the data is inserted in a tail.

while(NULL!=p->next) p=p->next;
p->next=new; 

3. Single head of the list inserted list :( insertion)
head insertion of the new node is inserted between the next node node original node and the original node.
The first step: Get the position of the current node, which is the first access node

struct list * p=header;

The second step: a new next node to the next node of the head node of the original (let points to the node to be inserted later)

new ->next=p->next;

The third step: the next node of the head node of the original set as the head node newly inserted.
4. singly linked list traversal:
We know that the head is a singly linked list node and a plurality of nodes, the last node to NULL.
Reflection:
1. We need to print head node do? (Without print head node, because we are a node for ease of operation and setting)
2. This article lists how many nodes how I know? (By judging whether the node is the end node, the end node point because NULL)
then we can now traverse our list:
The first step: Get the position of the current node, which is the first access node
struct list * p = header;
second step Three: As the head node we do not need to print it, this time, the node initialize printing needs to start from the first node.
p = p-> next;
The third step: determining whether a node is the last, if not, the first print data to the first node (1), and then moves to the next node (2), repeating these two steps.
1. while(NULL!=p-next){printf("node:%d\n",p->data);p=p->next;}
the printf ( "Node: D% \ n-", p-> Data); // print the last data field null points;
2. step may be printed: while(NULL!=p>next){p=p->next;printf("node:%d\n",p->data);}
5, a single list Delete
Delete prototype can be defined as:

int * detele_list_node(struct list* pH,int data);//指针、位置、数据

To delete a single list to consider two cases:
one deleted common node (of course, the head node can not be considered) namely: change point, release the memory!
There is also a case before a node end node removed, note that deleting the node also completely free up memory space corresponding node. Namely: point to NULL, free up space.
Design process:
first step: define two pointers, one for the current node, and the other represents the next current node.

struct list *p=header;//当前节点
struct list *prev=NULL;//当前节点的上一个节点

Step two: traverse the entire list, while preserving the previous current node

while(NULL!=p->next)
{
        //保存了当前节点的前一个节点
        prev=p;
        //保存当前偏移的节点
       p=p->next;
       return 0;
}

The third step: to find the data to be deleted in the process of traversal

while(NULL != p->next)
        { 
          //保存了当前的节点的前一个节点
          prev = p ;  
          //保存当前偏移的节点
          p = p->next ; 
          //查找到了数据
          if(p->id == data)
          {
          
          }
          return 0 ;
        }

Step Four: Find data points to two cases:
deleted common node

    if(p->id == data)
        {
            prev->next = p->next ;
            free(p);
        }
考虑尾节点的下一个节点为NULL的节点删除
        if(p->id == data)
        {
            if(p->next == NULL)
            {
                prev->next = NULL ;
                free(p);
            }
         }

The single chain reverse:
reverse steps:
Step 1: Set two pointers, a first storage position of the current node, a node represents a linked list of valid
// save the current position of the first node

struct list *p=pH->next;

// represents a valid node linked list

struct list *pBack;

Step Two: When no valid node or nodes have a valid, no operation.

if(p==NULL||p->next==NULL)
return ;

The third step: list traversal
1. Save the first address of the next node node

pBock=p->next;

2. The special treatment for the first valid node, which is the first valid node into the end to go.

if(p==pH->next)p->next=NULL;

3. If it is not a valid first point, the first interpolation operation is performed, the current node as a new node:

p->next=pH->next;//尾部连接
pH->next=p//头部连接

4. Continue down a node

p=pBack;

Step Four: manually inserting the last node

top_insret(pH,p);

The basic flow of a single list to get to know, here to write a program! ! !

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct slist
{
	int id ;
	struct slist *next ;
}L;

//创建一个节点
L *create_node(int data)
{
	//给每个节点分配结构体一样的空间大小
	L *p = malloc(sizeof(L));
	if(NULL == p)
	{
		printf("malloc error!\n");
		return NULL ;
	}
	//由于结构体在未初始化的时候一样是脏数据,所以要清
	memset(p,0,sizeof(L));
	//初始化第一个节点
	p->id = data ;
	//将节点的后继指针设置为NULL
	p->next = NULL ;
}

//链表的尾插
void tail_insert(L *pH , L *new)
{
	//获取当前的位置
	L *p = pH ;
	//如果当前位置的下一个节点不为空
	while(NULL != p->next)
	{
		//移动到下一个节点
		p = p->next ;
	}
	//如果跳出以上循环,所以已经到了NULL的这个位置
	//此时直接把新插入的节点赋值给NULL这个位置
	p->next = new ;
}

//链表的头插
void top_insert(L *pH , L *new)
{
	L *p = pH ;
	new->next = p->next ;
	p->next = new ;
}

//链表的遍历
void Print_node(L *pH)
{
	//获取当前的位置
	L *p = pH ;
	//获取第一个节点的位置
	p = p->next ;
	//如果当前位置的下一个节点不为空
	while(NULL != p->next)
	{
		//(1)打印节点的数据
		printf("id:%d\n",p->id);
		//(2)移动到下一个节点,如果条件仍为真,则重复(1),再(2)
		p = p->next ;
	}
	//如果当前位置的下一个节点为空,则打印数据
	//说明只有一个节点
	printf("id:%d\n",p->id);
}

//删除链表中的节点
int detele_list_node(L * pH , int data)
{
	//获取当前头节点的位置
	L *p = pH ;
	L *prev = NULL;
	while(NULL != p->next)
	{
		//保存当前节点的前一个节点的指针
		prev = p ;
		//然后让当前的指针继续往后移动
		p = p->next ;
		//判断,找到了要删除的数据
		if(p->id == data)
		{
			//两种情况,一种是普通节点,还有一种是尾节点
			if(p->next != NULL)  //普通节点的情况
			{
				prev->next = p->next ;
				free(p);
			}
			else //尾节点的情况
			{
				prev->next = NULL ; //将这个尾节点的上一个节点的指针域指向空
				free(p);
			}
			return 0  ;
		}
	}
	printf("没有要删除的节点\n");
	return -1 ;
}

void trave_list(L * pH)
{
	//保存第一个节点的位置
	L *p = pH->next;
	L *pBack;
	int i = 0 ;
	if(p->next == NULL || p == NULL)
		return ;

	while(NULL != p->next) //遍历链表
	{
		//保存第一个节点的下一个节点
		pBack = p->next ;
		//找到第一个有效节点,其实就是头指针的下一个节点
		if(p == pH->next)
		{
			//第一个有效节点就是最后一个节点,所以要指向NULL
			p->next = NULL ;
		}
		else
		{
			/*
			new->next = p->next ;
			p->next = new ;
			*/
			p->next = pH->next ; //尾部连接
		}
		pH->next = p ; //头部连接
		p = pBack ; //走下一个节点
	}
	top_insert(pH,p); //插入最后一个节点
}

int main(int argc , char **argv)
{
	//创建第一个节点
	int i ;
	L *header = create_node(0);
	for(i = 1 ; i < 10 ; i++)
	{
		tail_insert(header,create_node(i));
	}
	Print_node(header);
	detele_list_node(header,5);
	putchar('\n');
	Print_node(header);
	putchar('\n');
	trave_list(header);
	Print_node(header);
	return 0 ;
}

Related reference connection:

https://blog.csdn.net/morixinguan/article/details/68951912

Published 30 original articles · won praise 21 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44292472/article/details/104806606