2.1 Linear List-Single Linked List

1 Definition of single linked list

  • Single linked list is a kind of data structure with chain access. It uses a set of storage units with arbitrary addresses to store data elements in a linear table.
  • The data in the linked list is represented by nodes (as shown in the figure). Each node is composed of elements (mapping of data elements) and pointers (indicating the storage location of subsequent elements). The element is the storage unit of data, and the pointer is the address data connecting each node.
  • If each node of a linked list contains only one pointer field, it is called a single linked list.
    The pointer to the last node of the single list points to NULL
typedef struct node{
	datatype Data;
	struct node * Next;
}listnode, *linklist; // Node structure

2 Specific structure of single linked list

2.1 Head Node

  • Header node is a header node appended to the list.
  • The data domain of the header node generally does not store any information, but can also store some additional information about the length of the linear table.
  • The pointer field of the head node stores the pointer to the first node (that is, the storage location of the first node a1).
  • Header nodes are not necessarily necessary elements of a list.

2.2 Head Pointer

  • Header pointer refers to the pointer that the list points to the first node. If the list has a header node, it is the pointer to the header node.
  • Head pointer has the function of identification, so it is often crowned with the name of the list (the name of the pointer variable).
  • No matter whether the list is empty or not, the header pointer is not empty.
  • Header pointer is a necessary element of linked list.

2.3 Head Nodes and Head Pointer

  • If there is no header node, the insertion and deletion of the linked list may change the header pointer
  • If the header node exists, the insertion and deletion header pointers of the linked list remain unchanged

3. Operation of single linked list

3.1 Initialization

linklist init_list() {
	// Apply for a Head Node 
	linklist head = calloc(1, sizeof(listnode));
	if ( head != NULL){
		head->Next = NULL;
	}
	return head;
}

3.2 Create a new node

linklist new_node( int data ) {
	linklist new = calloc(1, sizeof(listnode));
	if( new != NULL){
		new->Data = data;
		new->Next = NULL;
	}
	return new;
}

3.3 Insert new nodes at the end of the list

void link_add_tail(linklist new, linklist head) {
	linklist tail = head;
	while ( tail->Next != NULL ){
		tail = tail->Next;
	}
	new->Next = tail->Next; // new's Next points to the empty
	tail->Next = new; 
}

3.4 Display

void show_list(linklist head) {
	linklist tmp;
	for (tmp=head->Next; tmp!=NULL; tmp=tmp->Next){
		printf("%d ", tmp->Data);
	}
	printf("\n");
}

4 Exercises

Establish a one-way list of integers (such as 1, 2, 3, 4, 5) and flip the data (such as 5, 4, 3, 2, 1) through some algorithms.

#include "common.h"

#ifndef LL_NODE_TYPE
#define LL_NODE_TYPE int // The default data type in the linked list node is int
#endif 

typedef LL_NODE_TYPE datatype; 

typedef struct node{
	datatype Data;
	struct node * Next;
}listnode, *linklist; // Node structure

// 1. Initialize an empty list
linklist init_list() {
	// Apply for a Head Node 
	linklist head = calloc(1, sizeof(listnode));
	if ( head != NULL){
		head->Next = NULL;
	}
	return head;
}

// 2.1 Create a new node
linklist new_node( int data ) {
	linklist new = calloc(1, sizeof(listnode));
	if( new != NULL){
		new->Data = data;
		new->Next = NULL;
	}
	return new;
}

// 2.2 Insert new nodes at the end of the list
void link_add_tail(linklist new, linklist head) {
	linklist tail = head;
	while ( tail->Next != NULL ){
		tail = tail->Next;
	}
	new->Next = tail->Next; // new's Next points to the empty
	tail->Next = new; 
}

// display
void show_list(linklist head) {
	linklist tmp;
	for (tmp=head->Next; tmp!=NULL; tmp=tmp->Next){
		printf("%d ", tmp->Data);
	}
	printf("\n");
}

// 3.1 After inserting the first irreversible node into the head node of the list
void link_add(linklist tmp, linklist head){
	tmp->Next = head->Next;
	head->Next = tmp;
}
 
// Flip
void revert(linklist head){
	linklist p = head->Next; // Unreversed list
	head->Next = NULL;
	
	while (p!=NULL){
		linklist tmp = p;
		p = p->Next;
		link_add(tmp, head);
	}
}


int main(void){
	// 1. Initialize an empty list
	linklist head = init_list();
	
	// 2. Insert several natural numbers in turn
	int n;
	scanf("%d", &n) ;
	int i;
	for ( i=1;i<=n;i++){
		// Create a new node
		linklist new = new_node(i);
		// Insert the new node at the end of the list
		link_add_tail(new, head);
	}
	
	// Display the node condition before flipping
	show_list(head);
	// 3. Reverse these receptions.
	revert(head);
	// Display the status of the flipped node
	show_list(head);
	
	return 0;
}

Code refers to a common header file. If an error is reported, refer to another article. Common header file - common.h

5 Advantages and disadvantages of single linked list

5.1 Advantages

  • The logical and physical order of nodes in a linked list is not necessarily the same.
  • Insertion and deletion do not require moving data elements, just modifying the "chain"

5.2 Disadvantages

  • Each node stores only backward pointers and stops the backward chain operation when the tail identifier is reached. That is to say, in this way, only successor nodes can be indexed, but not precursor nodes.
  • The problem with single-linked lists: If you don't start from scratch, you can't access all the nodes.
    Solution: Simply change the pointer of the terminal node in the single linked list from the null pointer to the pointer of the head node (single-loop linked list)

Posted by PHP Man on Wed, 28 Aug 2019 00:05:14 -0700