English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of C Language Single-Linked List and Example Code

1Simplicity of single linked list.

The single linked list (single linked list) is a type of linked list, characterized by the unidirectional link direction of the list, which requires sequential reading from the head to access the list; The list is constructed using pointers; It is also called a node list because the list is composed of individual nodes; Each node has a pointer member variable pointing to the next node in the list; The list is composed of nodes, pointed to by the head pointer to the first node that becomes the head of the table, and terminates with a pointer pointing to NULL;

2Example requirements:

According to the examples in the sample code, complete the insertion, deletion, and search of the single linked list (single linked list) with string data, and support the reversal of the single linked list;

3Code implementation.

#include <stdio.h>
#include <math.h>
#include <cstring>
#include <memory.h>
#include <malloc.h>
//Node definition
typedef struct Node {
 void *data; //Data field //Link field
 struct Node *next;
} NodeStruct, *pNode;
pNode head = NULL;
typedef char (*pCompareFunc)(void *a, void *b);
typedef void* (*pChar)(void *p);
// String judgment
int str_compare(void *a, void *b) {
 char *pa = (char*)a;
 char *pb = (char*)b;
 return strcmp(pa , pb);
}
// Allocate a node
pNode allocate_node(void *data, pChar char_func) {
 pNode node = allocate();
 node->data = char_func(data);
 return node;
}
// Create node
pNode allocate() {
 void *p = malloc(sizeof(NodeStruct));
 pNode node = (pNode)p;
 node->next = NULL;
 node->data = NULL;
 return node;
}
// Add a node
void insertNode(pNode node){
 if (head == null){
  head=node;
  }
  else {
  node->next = head;
  head = node;
  }
}
void* char_char(void *p) {
 char* pa = (char*)malloc(sizeof(char));
 memcpy(pa, p, sizeof(char));
 return pa;
}
// Initialize node
pNode allocate_node(void *data, pChar char_func) {
 pNode node = allocate();
 node->data = char_func(data);
 return node;
}
// Release resources
void free_list(pNode node) {
 pNode next = node;
 while (next != NULL) {
  if (next->data != NULL)
   free(next->data);
  pNode temp = next;
  next = next->next;
  free(temp);
 }
}
// 1.1 Add a node
void insert(pNode node) {
 if (head == NULL)
  head = node;
 else {
  node->next = head;
  head = node;
 }
}
//1.2Search
int str_search(void* data,pCompareFunc compare){
 pNode next = head;
 pNode prev = NULL;
 while (next != NULL ) {
  if (compare(data, next->data) == 0) {
   // If found, exit and return1
   return 1;
   break;
  }
  prev = next;
  next = next->next;
 }
 // If the search cannot be found continuously, return 0
 return 0;
}
// 1.3delete node
void remove(void* data,pCompareFunc compare) {
 pNode next = head;
 pNode prev = NULL;
 while (next != NULL) {
  if (compare(data, next->data) == 0) {
   head = next
    else {->next;
    next->next = NULL;
    free_list(next);
   }
    prev->next = next->next;
    next->next = NULL;
    free_list(next);
   }
   break;
  }
  prev = next;
  next = next->next;
 }
}
//1.4Reverse
void invert_order()
{
  node *This,*prev;
  p=head.next;
  This=NULL;
  while(p) {
    prev=This;
    This=p;
    p=p->next;
    This->next=prev;
  }
  head.next=This;
}
void main(){
 // 1One-way Link List
 char a1[] = 'aaa1';
 char a2[] = 'aaa2';
 char a3[] = 'aaa3';
 // 1.1Add
 insertNode(allocate_node(a1, init_char));
 insertNode(allocate_node(a2, init_char));
 insertNode(allocate_node(a3, init_char));
 // 1.2Search
 int flag = 0;
 flag = str_search(&a2,str_compare);
 // 1.3Delete
 remove(&a2,str_compare);
 // 1.4Reverse
  invert_order();
}

 This is the material sorting of C language one-way link table, and will continue to supplement relevant materials, thank you all for your support to this site!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like