C language linked list and creation of singly linked list

linked list

Definition of linked list

1. The linked list is on the physical storage unitnon-sequential, non-sequentialThe storage structure and the logical order of data elements are realized through the pointer addresses of the linked list, which consists of a series of nodes (addresses), and the nodes can be dynamically generated.

2. The node consists of two parts:
(1) The storage of data elementsdata field(memory space)
(2) store the address pointing to the next nodepointer field

3. Compared with the sequential structure of the linear table, the operation is complicated.

4. The linked list is divided into (1) single linked list
(2) double linked list
(3) one-way circular linked list
(4) two-way circular linked list

Advantages of linked lists

  1. dynamic data structure
    A linked list is a dynamic data structure, so it can grow and shrink at runtime by allocating and deallocating memory. Therefore, there is no need to give the initial size of the linked list .
  2. insert and delete
    Insertion and deletion of nodes is really easy. Unlike arrays, we don't have to move elements after inserting or removing them. In a linked list, we just need to update the address present in the next pointer of the node .
  3. no memory wasted
    Since the size of the linked list can be increased or decreased at runtime, no memory is wasted. In case of arrays, a lot of memory is wasted, for example, if we declare an array of size 10 and store only 6 elements in it, then space for 4 elements is wasted. There is no such problem in linked lists because memory is only allocated when needed.
  4. practice
    Data structures such as stacks and queues are easily implemented using linked lists.

Disadvantages of linked list

  1. memory usage
    Storing elements in a linked list requires more memory than an array. Because in a linked list, each node contains a pointer and itself requires additional memory.
  2. traverse
    It is difficult to traverse elements or nodes in a linked list. We cannot randomly access any element like we can in an array by index. For example, if we want to visit the node at position n, we must traverse all nodes before it. Therefore, the time required to access a node is large.
  3. reverse traversal
    In a linked list, reverse traversal is really difficult. In the case of a doubly linked list, it's easier, but the backward pointer requires extra memory, so memory is wasted.

Single Linked List Creation

Please add a picture description

Singly linked list node structure

typedef struct Node{
    
    
	int data;//数据域(这里以int型为例)
	struct Node* next;//指针域
}Node;
typedef struct Node* LinkList;

Initialization of singly linked list (creation of head node)

(1) The data field of the head node may not be used.
(2) When the pointer field of the head node is empty, it means an empty linked list.

LinkList InitList()
{
    
    
	LinkList head;
	head=(LinkList)malloc(sizeof(Node));
	head->next=NULL;
	return head;
}

Singly linked list creation

The establishment of a singly linked list can be divided into two methods:
one is to insert a new node at the end of the singly linked list ( tail insertion method )
and the other is to insert a new node at the head of the singly linked list ( head insertion method )

//尾插法 (数据读入顺序和链表结点顺序完全相同)
void CreateTail(LinkList head){
    
    
	LinkList p,q;
	p=head;
	int n;
	scanf("%d",&n);
	while(n){
    
    
		q=(LinkList)malloc(sizeof(Node));
		q->data=n;
		p->next=q;
		p=q;
		scanf("%d",&n);
	} 
	p->next=NULL;
} 
//头插法(数据读入顺序和链表结点顺序正好相反)
void CreateHead(LinkList head){
    
    
	LinkList p;
	int n;
	scanf("%d",&n);
	while(n){
    
    
		p=(LinkList)malloc(sizeof(Node));
		p->data=n;
		p->next=head->next;
		head->next=p;
		scanf("%d",&n);
	}
}

Single linked list traversal

void Output(LinkList head){
    
    
	LinkList p;
	p=head->next;
	while(p){
    
    
		printf("%d ",p->data);
		p=p->next;
	}
}

full code

#include<stdio.h>
#include<malloc.h>
typedef struct Node{
    
    
	int data;//数据域(这里以int型为例)
	struct Node *next;//指针域
}Node;
typedef struct Node* LinkList;

LinkList InitList();
void CreateTail(LinkList head);
void CreateHead(LinkList head);
void Output(LinkList head);

int main(){
    
    
	LinkList head1,head2;
	head1=InitList();
	CreateTail(head1);
	Output(head1);
	printf("\n");
	head2=InitList();
	CreateHead(head2);
	Output(head2);
	return 0;
}
//头结点的建立 
LinkList InitList()
{
    
    
	LinkList head;
	head=(LinkList)malloc(sizeof(Node));
	head->next=NULL;
	return head;
}
//尾插法 
void CreateTail(LinkList head){
    
    
	LinkList p,q;
	p=head;
	int n;
	scanf("%d",&n);
	while(n){
    
    
		q=(LinkList)malloc(sizeof(Node));
		q->data=n;
		p->next=q;
		p=q;
		scanf("%d",&n);
	} 
	p->next=NULL;
} 
//头插法
void CreateHead(LinkList head){
    
    
	LinkList p;
	int n;
	scanf("%d",&n);
	while(n){
    
    
		p=(LinkList)malloc(sizeof(Node));
		p->data=n;
		p->next=head->next;
		head->next=p;
		scanf("%d",&n);
	}
} 
void Output(LinkList head){
    
    
	LinkList p;
	p=head->next;
	while(p){
    
    
		printf("%d ",p->data);
		p=p->next;
	}
}

Results of the

1 2 3 0
1 2 3
1 2 3 0
3 2 1

Guess you like

Origin blog.csdn.net/The_onion/article/details/121411876