C language single linked list implementation method details

  • 2020-06-03 07:59:27
  • OfStack

An example of C language single linked list is presented in this paper. To share for your reference, specific as follows:

slist.h


#ifndef __SLIST_H__
#define __SLIST_H__
#include<cstdio>
#include<malloc.h>
#include<assert.h>
typedef int ElemType;
typedef struct Node { // Defines node information in a single linked list 
  ElemType data; // The data field of a node 
  struct Node *next; // The pointer field of a node 
}Node,*PNode;
typedef struct List { // Defines the list information for a single linked list 
  PNode first; //first Points to a number in a single linked list 1 A node 
  PNode last; //last To the end of a single linked list 1 A node 
  size_t size; // Record the number of nodes in a single linked list 
}List;
void InitList(List *list);// Initializes a single linked list 
void push_back(List *list, ElemType x);// Inserts elements at the end of a single linked list 
void push_front(List *list, ElemType x);// Inserts elements into the header of a single linked list 
void show_list(List *list);// Print a single linked list 
void pop_back(List *list);// Delete the end of a single linked list 1 An element 
void pop_front(List *list);// Deletes a single linked list 1 An element 
void insert_val(List *list, ElemType val);// Insert data elements into a single linked list (the data elements in the single linked list are ordered at this point) 
Node* find(List *list, ElemType x);// Find the data value in the single linked list x The node 
int length(List *list);// Find the length of a single list 
void delete_val(List *list, ElemType x);// Deletes a data element from a single linked list by value 
void sort(List *list);// Sort a single linked list 
void reverse(List *list);// Invert a single linked list 
void clear(List *list);// Clear the single linked list 
void destroy(List *list);// Destroy the single linked list 
#endif //__SLIST_H__

slist.cpp


#include"slist.h"
void InitList(List *list) {
  list->first = list->last = (Node*)malloc(sizeof(Node)); // The first node 
  assert(list->first != NULL);
  list->first->next = NULL;
  list->size = 0;
}
void push_back(List *list, ElemType x) {
  //step 1 : create 1 A new node 
  Node *s = (Node*)malloc(sizeof(Node));
  assert(s != NULL);
  s->data = x;
  s->next = NULL;
  //step 2 : Inserts a new node into the end of a single linked list 
  list->last->next = s;
  list->last = s;
  //step 3 : Updates the length of a single linked list 
  list->size++;
}
void push_front(List *list, ElemType x) {
  //step 1 : create 1 A new node 
  Node *s = (Node*)malloc(sizeof(Node));
  assert(s != NULL);
  s->data = x;
  s->next = NULL;
  //step 2 : Inserts a new node into the header of a single linked list 
  s->next = list->first->next;
  list->first->next = s;
  //step 3 : Determine whether the inserted node is the first in a single linked list 1 If update the end pointer of the linked list 
  if (list->size == 0)
    list->last = s;
  //step 4 : Updates the length of a single linked list 
  list->size++;
}
void show_list(List *list) {
  //step 1 Pointer to: p Points to the end of a single linked list 1 A node 
  Node *p = list->first->next;
  //step 2 : Circulates the information of the print node 
  while (p != NULL) {
    printf("%d->", p->data);
    p = p->next;
  }
  printf("Nul.\n");
}
void pop_back(List *list) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Define pointer p Make it point to the front of the target node 1 A node 
  Node *p = list->first;// From scratch 
  while (p->next != list->last)
    p = p->next;
  //step 3 : Delete the target node 
  free(list->last);
  list->last = p;
  list->last->next = NULL;
  //step 4 : Updates the length of a single linked list 
  list->size--;
}
void pop_front(List *list) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Define pointer p Make it point to the front of the target node 1 A node 
  Node *p = list->first->next;
  //step 3 : Delete the target node 
  list->first->next = p->next;
  free(p);
  //step 4 : Determine whether the deleted node is the last in a single linked list 1 If so, update the tail pointer of the single linked list 
  if (list->size == 1)
    list->last = list->first;
  //step 4 : Updates the length of a single linked list 
  list->size--;
}
void insert_val(List *list, ElemType x) {
  //step 1 : create 1 A new node 
  Node *s = (Node*)malloc(sizeof(Node));
  assert(s != NULL);
  s->data = x;
  s->next = NULL;
  //step 2 : Define pointer p Make it point ahead of the position to be inserted 1 A node 
  Node *p = list->first;// From scratch 
  while (p->next != NULL && p->next->data < s->data)
    p = p->next;
  //step 3 : Determine whether the node to be inserted at the end of the table, if so, update the end pointer of the single linked list 
  if (p->next == NULL)
    list->last = s;
  //step 4 : Insert node 
  s->next = p->next;
  p->next = s;
  //step 5 : Updates the length of a single list 
  list->size++;
}
Node* find(List *list, ElemType x) {
  //step 1 Pointer to: p Points to the end of a single linked list 1 A node 
  Node *p = list->first->next;
  //step 2 : Search linked list nodes in circular order 
  while (p != NULL && p->data != x)
    p = p->next;
  return p;
}
int length(List *list) {
  return list->size;
}
void delete_val(List *list, ElemType x) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Determine the position of the node in the single linked list, and judge whether it exists in the single linked list 
  Node *p = find(list, x);
  if (p == NULL) {
    printf(" The data to be deleted does not exist! \n");
    return;
  }
  //step 3 : Determine whether the node position is the end of the table 
  if (p == list->last)// Is the footer 
    pop_back(list);
  else {// Not a footer 
    Node *q = p->next;
    p->data = q->data;
    p->next = q->next;
    free(q);
    list->size--;
  }
}
void sort(List *list) {
  //step 1 : Determine whether the number of nodes in a single linked list is 0 or 1
  if (list->size == 0 || list->size == 1) return;
  //step 2 : Will be a single linked list of the number 1 After the node of the linked list section cut out, easy to re-insert the list in order 
  Node *s = list->first->next; //  Pointer to the s Points to the end of a single linked list 1 A node 
  Node *p = s->next;//q Point to the s Subsequent nodes 
  list->last = s;// The tail pointer of a singly linked list points to the beginning of the singly linked list 1 A node 
  list->last->next = NULL;// Truncated list 
  //step 3 : Insert the node in the truncated list back into the original list according to its data field size 
  while (p != NULL) {
    s = p;
    p = p->next;
    Node *q = list->first;
    while (q->next != NULL && q->next->data < s->data)
      q = q->next;
    if (q->next == NULL)// judge q Is it pointing to the end of a single list 1 If so, update the end pointer of the linked list 
      list->last = s;
    // Insert the node back into the list 
    s->next = q->next;
    q->next = s;
  }
}
void reverse(List *list) {
  //step 1 : Determine whether the number of nodes in a single linked list is 0 or 1
  if (list->size == 0 || list->size == 1) return;
  //step 2 : Will be a single linked list of the number 1 The section of the linked list after the node is truncated, and then the node in the truncated linked list is re-inserted into the original linked list by the method of header insertion 
  Node *p = list->first->next;
  Node *q = p->next;
  list->last = p;
  list->last->next = NULL;
  while (q != NULL) {
    p = q;
    q = q->next;
    p->next = list->first->next;
    list->first->next = p;
  }
}
void clear(List *list) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Releases each in a single linked list 1 A node 
  Node *p = list->first->next;
  while (p != NULL) {
    list->first->next = p->next;
    free(p);
    p = list->first->next;
  }
  //step 3 : Both the head pointer and the tail pointer point to the head again 
  list->last = list->first;
  //step 4 : Updates the list length 
  list->size = 0;
}
void destroy(List *list) {
  //step 1 : Clear a single linked list 
  clear(list);
  //step 2 : Release the head 
  free(list->first);
  //step 3 : Both the head pointer and tail pointer are null 
  list->first = list->last = NULL;
}

main.cpp


#include"slist.h"
void main() {
  List mylist;
  InitList(&mylist);
  ElemType item;
  Node *p = NULL;
  int select = 1;
  while (select) {
    printf("*******************************************\n");
    printf("*[1] push_back    [2] push_front  *\n");
    printf("*[3] show_list    [4] pop_back   *\n");
    printf("*[5] pop_front    [6] insert_val  *\n");
    printf("*[7] find       [8] length    *\n");
    printf("*[9] delete_val    [10] sort     *\n");
    printf("*[11] reverse     [12] clear     *\n");
    printf("*[13*] destroy     [0] quit_system  *\n");
    printf("*******************************************\n");
    printf(" Please select: >>");
    scanf("%d", &select);
    if (select == 0) break;
    switch (select) {
    case 1:
      printf(" Please enter the data you want to insert ( -1 The end) :>");
      while (scanf("%d", &item), item != -1) {
        push_back(&mylist, item);
      }
      break;
    case 2:
      printf(" Please enter the data you want to insert ( -1 The end) :>");
      while (scanf("%d", &item), item != -1) {
        push_front(&mylist, item);
      }
      break;
    case 3:
      show_list(&mylist);
      break;
    case 4:
      pop_back(&mylist);
      break;
    case 5:
      pop_front(&mylist);
      break;
    case 6:
      printf(" Please enter the data to insert :>");
      scanf("%d", &item);
      insert_val(&mylist, item);
      break;
    case 7:
      printf(" Enter the data you are looking for :>");
      scanf("%d", &item);
      p = find(&mylist, item);
      if (p == NULL)
        printf(" The data you are looking for does not exist in a single linked list! \n");
      break;
    case 8:
      printf(" The length of a single linked list is %d\n", length(&mylist));
      break;
    case 9:
      printf(" Please enter the value to delete :>");
      scanf("%d", &item);
      delete_val(&mylist, item);
      break;
    case 10:
      sort(&mylist);
      break;
    case 11:
      reverse(&mylist);
      break;
    case 12:
      clear(&mylist);
      break;
      //case 13:
      //destroy(&mylist);
      //break;
    default:
      printf(" The wrong choice , Please choose again! \n");
      break;
    }
  }
  destroy(&mylist); // Program over. Destroy the list 
}

Attached: Single linked list optimized version

slist.h


#ifndef __SLIST_H__
#define __SLIST_H__
#include<cstdio>
#include<malloc.h>
#include<assert.h>
typedef int ElemType;
typedef struct Node { // Defines node information in a single linked list 
  ElemType data; // The data field of a node 
  struct Node *next; // The pointer field of a node 
}Node,*PNode;
typedef struct List { // Defines the list information for a single linked list 
  PNode first; //first Points to a number in a single linked list 1 A node 
  PNode last; //last To the end of a single linked list 1 A node 
  size_t size; // Record the number of nodes in a single linked list 
}List;
void InitList(List *list);// Initializes a single linked list 
void push_back(List *list, ElemType x);// Inserts elements at the end of a single linked list 
void push_front(List *list, ElemType x);// Inserts elements into the header of a single linked list 
void show_list(List *list);// Print a single linked list 
void pop_back(List *list);// Delete the end of a single linked list 1 An element 
void pop_front(List *list);// Deletes a single linked list 1 An element 
void insert_val(List *list, ElemType val);// Insert data elements into a single linked list (the data elements in the single linked list are ordered at this point) 
Node* find(List *list, ElemType x);// Find the data value in the single linked list x The node 
int length(List *list);// Find the length of a single list 
void delete_val(List *list, ElemType x);// Deletes a data element from a single linked list by value 
void sort(List *list);// Sort a single linked list 
void reverse(List *list);// Invert a single linked list 
void clear(List *list);// Clear the single linked list 
void destroy(List *list);// Destroy the single linked list 
// Code optimization 
Node* CreateNode(ElemType x); // create 1 A single list node 
Node* begin(List *list); // Returns the number of a single linked list 1 A node 
Node* end(List *list); // Returns the end of a single linked list 1 The number of nodes 1 A node 
void insert(List *list, Node *pos, ElemType x); // At a particular location in a single linked list ( pos ) Insert a new node 
#endif //__SLIST_H__

slist.cpp


#include"slist.h"
void InitList(List *list) {
  list->first = list->last = (Node*)malloc(sizeof(Node)); // The first node 
  assert(list->first != NULL);
  list->first->next = NULL;
  list->size = 0;
}
//push_back The optimization of the 
void push_back(List *list, ElemType x) {
  insert(list, end(list), x);
}
//push_front The optimization of the 
void push_front(List *list, ElemType x) {
  insert(list, begin(list), x);
}
void show_list(List *list) {
  //step 1 Pointer to: p Points to the end of a single linked list 1 A node 
  Node *p = list->first->next;
  //step 2 : Circulates the information of the print node 
  while (p != NULL) {
    printf("%d->", p->data);
    p = p->next;
  }
  printf("Nul.\n");
}
void pop_back(List *list) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Define pointer p Make it point to the front of the target node 1 A node 
  Node *p = list->first;// From scratch 
  while (p->next != list->last)
    p = p->next;
  //step 3 : Delete the target node 
  free(list->last);
  list->last = p;
  list->last->next = NULL;
  //step 4 : Updates the length of a single linked list 
  list->size--;
}
void pop_front(List *list) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Define pointer p Make it point to the front of the target node 1 A node 
  Node *p = list->first->next;
  //step 3 : Delete the target node 
  list->first->next = p->next;
  free(p);
  //step 4 : Determine whether the deleted node is the last in a single linked list 1 If so, update the tail pointer of the single linked list 
  if (list->size == 1)
    list->last = list->first;
  //step 4 : Updates the length of a single linked list 
  list->size--;
}
//insert_val The optimization of the 
void insert_val(List *list, ElemType x) {
  //step 1 : create 1 A new node 
  Node *s = CreateNode(x);
  //step 2 : Define pointer p Make it point ahead of the position to be inserted 1 A node 
  Node *p = list->first;// From scratch 
  while (p->next != NULL && p->next->data < s->data)
    p = p->next;
  //step 3 : Determine whether the node to be inserted at the end of the table, if so, update the end pointer of the single linked list 
  if (p->next == NULL)
    list->last = s;
  //step 4 : Insert node 
  s->next = p->next;
  p->next = s;
  //step 5 : Updates the length of a single list 
  list->size++;
}
Node* find(List *list, ElemType x) {
  //step 1 Pointer to: p Points to the end of a single linked list 1 A node 
  Node *p = list->first->next;
  //step 2 : Search linked list nodes in circular order 
  while (p != NULL && p->data != x)
    p = p->next;
  return p;
}
int length(List *list) {
  return list->size;
}
void delete_val(List *list, ElemType x) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Determine the position of the node in the single linked list, and judge whether it exists in the single linked list 
  Node *p = find(list, x);
  if (p == NULL) {
    printf(" The data to be deleted does not exist! \n");
    return;
  }
  //step 3 : Determine whether the node position is the end of the table 
  if (p == list->last)// Is the footer 
    pop_back(list);
  else {// Not a footer 
    Node *q = p->next;
    p->data = q->data;
    p->next = q->next;
    free(q);
    list->size--;
  }
}
void sort(List *list) {
  //step 1 : Determine whether the number of nodes in a single linked list is 0 or 1
  if (list->size == 0 || list->size == 1) return;
  //step 2 : Will be a single linked list of the number 1 After the node of the linked list section cut out, easy to re-insert the list in order 
  Node *s = list->first->next; //  Pointer to the s Points to the end of a single linked list 1 A node 
  Node *p = s->next;//q Point to the s Subsequent nodes 
  list->last = s;// The tail pointer of a singly linked list points to the beginning of the singly linked list 1 A node 
  list->last->next = NULL;// Truncated list 
  //step 3 : Insert the node in the truncated list back into the original list according to its data field size 
  while (p != NULL) {
    s = p;
    p = p->next;
    Node *q = list->first;
    while (q->next != NULL && q->next->data < s->data)
      q = q->next;
    if (q->next == NULL)// judge q Is it pointing to the end of a single list 1 If so, update the end pointer of the linked list 
      list->last = s;
    // Insert the node back into the list 
    s->next = q->next;
    q->next = s;
  }
}
void reverse(List *list) {
  //step 1 : Determine whether the number of nodes in a single linked list is 0 or 1
  if (list->size == 0 || list->size == 1) return;
  //step 2 : Will be a single linked list of the number 1 The section of the linked list after the node is truncated, and then the node in the truncated linked list is re-inserted into the original linked list by the method of header insertion 
  Node *p = list->first->next;
  Node *q = p->next;
  list->last = p;
  list->last->next = NULL;
  while (q != NULL) {
    p = q;
    q = q->next;
    p->next = list->first->next;
    list->first->next = p;
  }
}
void clear(List *list) {
  //step 1 : To determine whether a single linked list is empty 
  if (list->size == 0) return;
  //step 2 : Releases each in a single linked list 1 A node 
  Node *p = list->first->next;
  while (p != NULL) {
    list->first->next = p->next;
    free(p);
    p = list->first->next;
  }
  //step 3 : Both the head pointer and the tail pointer point to the head again 
  list->last = list->first;
  //step 4 : Updates the list length 
  list->size = 0;
}
void destroy(List *list) {
  //step 1 : Clear a single linked list 
  clear(list);
  //step 2 : Release the head 
  free(list->first);
  //step 3 : Both the head pointer and tail pointer are null 
  list->first = list->last = NULL;
}
// To optimize the 
Node* CreateNode(ElemType x) {
  Node *s = (Node*)malloc(sizeof(Node));
  assert(s != NULL);
  s->data = x;
  s->next = NULL;
  return s;
}
Node* begin(List *list) {
  return list->first->next;
}
Node* end(List *list) {
  return list->last->next;
}
void insert(List *list, Node *pos, ElemType x) {
  //step 1 : create 1 A new node 
  Node *s = CreateNode(x);
  //step 2 : Determine the insertion location of the tape 
  Node *p = list->first;
  while (p->next != pos)
    p = p->next;
  //step 3 : Insert node 
  s->next = p->next;
  p->next = s;
  //step 4 : Determines whether a node is inserted into the end of a linked list. If so, update the end pointer of a single linked list 
  if (pos == NULL)
    list->last = s;
  //step 5 : Updates the length of a single list 
  list->size++;
}

main.cpp


#include"slist.h"
void main() {
  List mylist;
  InitList(&mylist);
  ElemType item;
  Node *p = NULL;
  int select = 1;
  while (select) {
    printf("*******************************************\n");
    printf("*[1] push_back    [2] push_front  *\n");
    printf("*[3] show_list    [4] pop_back   *\n");
    printf("*[5] pop_front    [6] insert_val  *\n");
    printf("*[7] find       [8] length    *\n");
    printf("*[9] delete_val    [10] sort     *\n");
    printf("*[11] reverse     [12] clear     *\n");
    printf("*[13*] destroy     [0] quit_system  *\n");
    printf("*******************************************\n");
    printf(" Please select: >>");
    scanf("%d", &select);
    if (select == 0) break;
    switch (select) {
    case 1:
      printf(" Please enter the data you want to insert ( -1 The end) :>");
      while (scanf("%d", &item), item != -1) {
        push_back(&mylist, item);
      }
      break;
    case 2:
      printf(" Please enter the data you want to insert ( -1 The end) :>");
      while (scanf("%d", &item), item != -1) {
        push_front(&mylist, item);
      }
      break;
    case 3:
      show_list(&mylist);
      break;
    case 4:
      pop_back(&mylist);
      break;
    case 5:
      pop_front(&mylist);
      break;
    case 6:
      printf(" Please enter the data to insert :>");
      scanf("%d", &item);
      insert_val(&mylist, item);
      break;
    case 7:
      printf(" Enter the data you are looking for :>");
      scanf("%d", &item);
      p = find(&mylist, item);
      if (p == NULL)
        printf(" The data you are looking for does not exist in a single linked list! \n");
      break;
    case 8:
      printf(" The length of a single linked list is %d\n", length(&mylist));
      break;
    case 9:
      printf(" Please enter the value to delete :>");
      scanf("%d", &item);
      delete_val(&mylist, item);
      break;
    case 10:
      sort(&mylist);
      break;
    case 11:
      reverse(&mylist);
      break;
    case 12:
      clear(&mylist);
      break;
      //case 13:
      //destroy(&mylist);
      //break;
    default:
      printf(" The wrong choice , Please choose again! \n");
      break;
    }
  }
  destroy(&mylist); // Program over. Destroy the list 
}

I hope this article has been helpful in C programming.


Related articles: