Example of circular single linked list functionality implemented by C language

  • 2020-06-03 08:00:05
  • OfStack

This article illustrates the circular single linked list function implemented by C language. To share for your reference, specific as follows:

SClist.h


#ifndef __SCLIST_H__
#define __SCLIST_H__
#include<cstdio>
#include<malloc.h>
#include<assert.h>
typedef int ElemType;
typedef struct Node {
  ElemType data;
  struct Node *next;
}Node,*PNode;
typedef struct List {
  PNode first;
  PNode last;
  size_t size;
}List;
void InitSClist(List *list);// Initialize cyclic single linked list 
void push_back(List *list, ElemType x);// Inserts elements at the end of the loop single linked list 
void push_front(List *list, ElemType x);// Inserts elements into the header of a circular single linked list 
void show_list(List *list);// Print circular single linked list 
void pop_back(List *list);// Deletes the end of the loop single linked list 1 An element 
void pop_front(List *list);// Removes the control of a loop single linked list 1 An element 
void insert_val(List *list, ElemType val);// Insert the data elements into the circular single linked list (the data elements in the circular single linked list are ordered at this time) 
Node* find(List *list, ElemType x);// The data value in the lookup loop single linked list is x The node 
int length(List *list);// Find the length of the circular single linked list 
void delete_val(List *list, ElemType x);// Deletes a data element from a circular single linked list by value 
void sort(List *list);// Sort a circular single linked list 
void reverse(List *list);// Inverse cyclic single linked list 
void clear(List *list);// Clear circular single linked lists 
void destroy(List *list);// Destroy the circular single linked list 
// To optimize the 
Node* _buynode(ElemType x);// Create a node 
#endif

SClist.cpp


#include"SClist.h"
Node* _buynode(ElemType x) {
  Node *s = (Node*)malloc(sizeof(Node));
  assert(s != NULL);
  s->data = x;
  s->next = NULL;
  return s;
}
void InitSClist(List *list) {
  Node *s = (Node*)malloc(sizeof(Node));
  assert(s != NULL);
  list->first = list->last = s;
  list->last->next = list->first;// Let the list end 1 The pointer field of the node points to the header node, thus the time list loop 
  list->size = 0;
}
void push_back(List *list, ElemType x) {
  Node *s = _buynode(x);
  list->last->next = s;
  list->last = s;
  list->last->next = list->first;
  list->size++;
}
void push_front(List *list, ElemType x) {
  Node *s = _buynode(x);
  s->next = list->first->next;
  list->first->next = s;
  if (list->last == list->first)
    list->last = s;
  list->size++;
}
void show_list(List *list) {
  Node *p = list->first->next;
  while (p != list->first) {
    printf("%d->", p->data);
    p = p->next;
  }
  printf("Nul.\n");
}
void pop_back(List *list) {
  if (list->size == 0) return;
  Node *p = list->first;
  while (p->next != list->last)
    p = p->next;
  free(list->last);
  list->last = p;
  list->last->next = list->first;
  list->size--;
}
void pop_front(List *list) {
  if (list->size == 0) return;
  Node *p = list->first->next;
  list->first->next = p->next;
  if (list->size == 1)
    list->last = list->first;
  free(p);
  list->size--;
}
void insert_val(List *list, ElemType x) {
  Node *p = list->first;
  while (p->next != list->last && p->next->data < x)
    p = p->next;
  if (p->next == list->last && p->next->data < x)
    push_back(list, x);
  else {
    Node *s = _buynode(x);
    s->next = p->next;
    p->next = s;
    list->size++;
  }
}
Node* find(List *list, ElemType key) {
  if (list->size == 0) return NULL;
  Node *p = list->first->next;
  while(p != list->first && p->data != key)
    p = p->next;
  if (p == list->first)
    return NULL;
  return p;
}
int length(List *list) {
  return list->size;
}
void delete_val(List *list, ElemType x) {
  if (list->size == 0) return;
  Node *p = find(list, x);
  if (p == NULL) {
    printf(" There is no data to delete !\n");
    return;
  }
  if (p == list->last)
    pop_back(list);
  else {
    Node *q = p->next;
    p->data = q->data;
    p->next = q->next;
    free(q);
    list->size--;
  }
}
void sort(List *list) {
  if (list->size == 0 || list->size == 1) return;
  Node *s = list->first->next;
  Node *q = s->next;
  list->last->next = NULL;
  list->last = s;
  list->last->next = list->first;
  while (q != NULL) {
    s = q;
    q = q->next;
    Node *p = list->first;
    while (p->next != list->last && p->next->data < s->data)
      p = p->next;
    if (p->next == list->last &&p->next->data < s->data) {
      list->last->next = s;
      list->last = s;
      list->last->next = list->first;
    }
    else {
      s->next = p->next;
      p->next = s;
    }
  }
}
void reverse(List *list) {
  if (list->size == 0 || list->size == 1) return;
  Node *p = list->first->next;
  Node *q = p->next;
  list->last->next = NULL;
  list->last = p;
  list->last->next = list->first;
  while (q != NULL) {
    p = q;
    q = q->next;
    p->next = list->first->next;
    list->first->next = p;
  }
}
void clear(List *list) {
  Node *p = list->first->next;
  while (p != list->first) {
    list->first->next = p->next;
    free(p);
    p = list->first->next;
  }
  list->last = list->first;
  list->last->next = list->first;
  list->size = 0;
}
void destroy(List *list) {
  clear(list);
  free(list->first);
  list->first = list->last = NULL;
}

main.cpp


#include"SClist.h"
void main() {
  List mylist;
  InitSClist(&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);
}

I hope that this article has been helpful in C programming.


Related articles: