C language sequence table implementation code

  • 2020-05-30 20:35:43
  • OfStack

The example of this article for you to share the C language implementation sequence table of the specific code, for your reference, the specific content is as follows

seqlist.h


#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include<cstdio>
#include<malloc.h>
#include<assert.h>
#define SEQLIST_INIT_SIZE 8
#define INC_SIZE 3 // The size of the space increment 
typedef int ElemType;
typedef struct Seqlist {
 ElemType *base;
 int capacity; // Sequential table capacity 
 int size; // The size of the table 
}Seqlist;

bool Inc(Seqlist *list);// Increase the capacity of the sequence table 
void InitSeqlist(Seqlist *list); // Initializes the sequence table 
void push_back(Seqlist *list, ElemType x); // Insert elements at the end of the sequence table 
void push_front(Seqlist *list, ElemType x); // Insert elements at the head of the sequence table 
void show_list(Seqlist *list); // Displays the elements in the order table 
void pop_back(Seqlist *list); // Delete the order table last 1 An element 
void pop_front(Seqlist *list); // Delete order table no 1 An element 
void insert_pos(Seqlist *list, int pos, ElemType x);// Inserts data at the selected location in the sequence table 
int find(Seqlist *list, ElemType key); // Look up the elements in the order table key The subscript 
int length(Seqlist *list);// Find the length of the sequence table 
void delete_pos(Seqlist *list, int pos); // Deletes data elements at specific locations in the sequence table 
void delete_val(Seqlist *list, int key);// Delete the order table value of key Data elements 
void sort(Seqlist *list);// Bubble sort 
void reverse(Seqlist *list);// Reverse the order list 
void clear(Seqlist *list);// Clear all elements in the order table 
void destroy(Seqlist *list);// Destroy sequence table 
void merge(Seqlist *lt, Seqlist *la, Seqlist *lb);// Merge two sequential lists 

#endif //__SEQLIST_H__ 

seqlist.cpp


#include"seqlist.h"

bool Inc(Seqlist *list) {
 ElemType *newbase = (ElemType*)realloc(list, sizeof(ElemType)*(list->capacity + INC_SIZE)); // Reallocate memory space 
 if (newbase == NULL) {
  printf(" Memory space is full, no more memory space can be allocated! \n");
  return false;
 }
 list->base = newbase;
 list->capacity += INC_SIZE;
 return true;
}

void InitSeqlist(Seqlist *list) {
 list->base = (ElemType*)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE);
 assert(list->base != NULL);
 list->capacity = SEQLIST_INIT_SIZE;
 list->size = 0;
}

void push_back(Seqlist *list, ElemType x) {
 if (list->size >= list->capacity && !Inc(list)) { //Inc(list) To determine whether the increase in the sequential table capacity was successful , Enter only in case of failure if In the statement 
  printf(" Order table capacity is full, can not continue to insert new elements at the end of the table! \n");
  return;
 }
 list->base[list->size] = x;
 list->size++;
}

void push_front(Seqlist *list, ElemType x) {
 if (list->size >= list->capacity && !Inc(list)) {
  printf(" Order table capacity is full, can no longer insert new elements in the header! \n");
  return;
 }
 for (int i = list->size;i > 0;i--) {
  list->base[i] = list->base[i - 1];
 }
 list->base[0] = x;
 list->size++;
}

void show_list(Seqlist *list) {
 for (int i = 0;i < list->size;i++) {
  printf("%d ", list->base[i]);
 }
 printf("\n");
}

void pop_back(Seqlist *list) {
 if (list->size == 0) {
  printf(" The order table is empty, you can no longer delete elements at the end of the table! \n");
  return;
 }
 list->size--;
}

void pop_front(Seqlist *list) {
 if (list->size == 0) {
  printf(" The order table is empty, you can no longer delete elements in the header! \n");
  return;
 }
 for (int i = 0;i < list->size - 1;i++) {
  list->base[i] = list->base[i + 1];
 }
 list->size--;
}

void insert_pos(Seqlist *list, int pos, ElemType x) {
 if (pos<0 || pos>list->size) {
  printf(" Insert position is not valid, cannot insert element! \n");
  return;
 }
 if (list->size >= list->capacity && !Inc(list)) {
  printf(" Order table capacity is full, cannot insert new element! \n");
  return;
 }
 for (int i = list->size;i > pos;i--) {
  list->base[i] = list->base[i - 1];
 }
 list->base[pos] = x;
 list->size++;
}

int find(Seqlist *list, ElemType key) {
 for (int i = 0;i < list->size;i++) {
  if (list->base[i] == key)
   return i;
 }
 return -1;
}

int length(Seqlist *list) {
 return list->size;
}

void delete_pos(Seqlist *list, int pos) {
 if (pos < 0 || pos >= list->size) {
  printf(" Delete location is illegal, cannot delete element! \n");
  return;
 }
 for (int i = pos;i < list->size - 1;i++) {
  list->base[i] = list->base[i + 1];
 }
 list->size--;
}

void delete_val(Seqlist *list, int key) {
 int pos = find(list, key);
 if (pos == -1) {
  printf(" This element is not in the order table! \n");
  return;
 }
 delete_pos(list, pos);
}

void sort(Seqlist *list) {
 for (int i = 0;i < list->size - 1;i++) {// The number of times a sort occurs (e.g 5 Data need to be compared 4 Trip) 
  for (int j = 0;j < list->size - 1 - i;j++) {// every 1 The number of comparisons in a pass comparison (e.g 5 The number is number one 0 Need to compare 4 Times) 
   if (list->base[j] > list->base[j + 1]) {
    ElemType temp = list->base[j];
    list->base[j] = list->base[j + 1];
    list->base[j + 1] = temp;
   }
  }
 }
}

void reverse(Seqlist *list) {
 if (list->size == 0 || list->size == 1) return;
 int low = 0, high = list->size - 1;
 while (low < high) {
  ElemType temp = list->base[low];
  list->base[low] = list->base[high];
  list->base[high] = temp;
  low++;
  high--;
 }
}

void clear(Seqlist *list) {
 list->size = 0;
}

void destroy(Seqlist *list) {
 free(list->base);
 list->base = NULL;
 list->capacity = 0;
 list->size = 0;
}

void merge(Seqlist *lt, Seqlist *la, Seqlist *lb) {
 lt->capacity = la->size + lb->size;
 lt->base = (ElemType*)malloc(sizeof(ElemType)*lt->capacity);
 assert(lt->base != NULL);

 int ia = 0, ib = 0, ic = 0;
 while (ia < la->size&&ib < lb->size) {
  if (la->base[ia] < lb->base[ib]) {
   lt->base[ic++] = la->base[ia++];
  }
  else {
   lt->base[ic++] = lb->base[ib++];
  }
 }
 while (ia < la->size) {
  lt->base[ic++] = la->base[ia++];
 }
 while (ib < lb->size) {
  lt->base[ic++] = lb->base[ib++];
 }
 lt->size = la->size + lb->size;
 show_list(lt);
}

main.cpp


#include"seqlist.h"

void main() {
 Seqlist list;
 InitSeqlist(&list);

 ElemType item;
 int pos;
 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_pos *\n");
  printf("*[7] find    [8] length  *\n");
  printf("*[9] delete_pos  [10] delete_value *\n");
  printf("*[11] sort    [12] reverse  *\n");
  printf("*[13] clear   [14] merge   *\n");
  printf("*[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 to be inserted ( -1 The end) :>");
   while (scanf("%d", &item), item != -1) {// First the input item As long as item Is not equal to -1 And then we go through the loop 
    push_back(&list, item);
   }
   break;
  case 2:
   printf(" Please enter the data to be inserted ( -1 The end) :>");
   while (scanf("%d", &item), item != -1) {
    push_front(&list, item);
   }
   break;
  case 3:
   show_list(&list);
   break;
  case 4:
   pop_back(&list);
   break;
  case 5:
   pop_front(&list);
   break;
  case 6:
   printf(" Please enter the data to insert :>");
   scanf("%d", &item);
   printf(" Please enter the location to insert :>");
   scanf("%d", &pos);
   insert_pos(&list, pos, item);
   break;
  case 7:
   printf(" Please enter the data you are looking for :>");
   scanf("%d", &item);
   pos = find(&list, item);
   if (pos == -1)
    printf(" The data element you are looking for is not in the order table! \n");
   else
    printf(" The subscript position of the searched data element in the sequential table is %d\n", pos);
   break;
  case 8:
   printf(" The length of the sequence table is %d\n", length(&list));
   break;
  case 9:
   printf(" Please enter the subscript position in the sequence table where you want to delete data :>");
   scanf("%d", &pos);
   delete_pos(&list, pos);
   break;
  case 10:
   printf(" Please enter a value to delete the data :>");
   scanf("%d", &item);
   delete_val(&list, item);
   break;
  case 11:
   sort(&list);
   break;
  case 12:
   reverse(&list);
   break;
  case 13:
   clear(&list);
   break;
  case 14:
   Seqlist mylist, yourlist;
   ElemType item1, item2;
   InitSeqlist(&mylist);
   InitSeqlist(&yourlist);
   printf(" Please enter the sequence table 1 Element values ( -1 The end) :>");
   while (scanf("%d", &item1), item1 != -1) {
    push_back(&mylist, item1);
   }
   printf(" Please enter the sequence table 2 Element values ( -1 The end) :>");
   while (scanf("%d", &item2), item2 != -1) {
    push_back(&yourlist, item2);
   }
   merge(&list, &mylist, &yourlist);
   destroy(&mylist);
   destroy(&yourlist);
   break;
  default:
   printf(" Wrong choice of input! Please enter again! \n");
   break;
  }
 }
 destroy(&list);
}

Related articles: