C++ implements a sequence table based on static arrays

  • 2020-09-16 07:40:18
  • OfStack

In this paper, we share C++ sequence table based on static array for your reference. The specific content is as follows

The basic operations implemented include:

1. Initialization
2. End plug
3. Delete
4. His head
5. Delete
6. Find any element
7. Read any element
8. Modify any location element
9. Finds the subscript of the specified element value
10. Insert the element anywhere
11. Deletes the element in the specified location
12. Delete any element
13. Print data

The header file seqlist. h:


#pragma once 

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

#define maxsize 1000// Maximum number of elements in a linked list 

typedef int seqType;
typedef struct seqlist
{
  seqType arr[maxsize];// Data field, where data is stored 
  size_t size;//size_t It's an unsigned long integer, yes 1 type ,size Represents the number of elements in an array 
}seqlist ;

void PrintSeqList(seqlist *seq); // Print data 
void print_seqlist(char *s); // Print the title 
void seqlistInit(seqlist *seq); // List initialization 

void seqlistPushBack(seqlist *seq, seqType value); // Tail element, value Is the value of the inserted element 
void seqlistPopBack(seqlist *seq); // Tail deleted elements, value Is the value of the inserted element 

void seqlistPushHead(seqlist*seq, seqType value); // His head element 
void seqlistPopHead(seqlist*seq); // To delete the first element 

seqType seqlistFind(seqlist *seq, seqType data); // Find any element 
seqType seqlistRead_pos(seqlist*seq, size_t pos); // Reads an element at any location 
size_t seqlistFind_pos(seqlist*seq, seqType value); // Finds the subscript of the specified element 

seqType seqlistModify(seqlist*seq, size_t pos, seqType data); // Modify an element in any location 
void seqlistInsert_pos(seqlist *seq, size_t pos, seqType data); // Insert the element anywhere 

void seqlistErase_pos(seqlist *seq, size_t pos); // Deletes an element anywhere 
void seqlistRemove(seqlist *seq, seqType data); // Remove elements 

The implementation file seqlist.c


#include"seqlist.h"
void PrintSeqList(seqlist *seq)// Print data 
{
  size_t i = 0;
  if (seq->size == 0)
  {
    printf(" The linear table is empty , End of the print \n");
    return;
  }
  for (i = 0; i < seq->size; i++)
  {
    printf(" The subscript for %d Elements of are: %d\n",i, seq->arr[i]);
  }
  printf("\n");
}

void print_seqlist(char *s)// Print the title 
{
  int i = 0;
  printf("%s\n",s);
  printf("\n");
}

void seqlistInit(seqlist *seq)// List initialization 
{
  assert(seq);
  seq->size = 0;// The effective element is assigned to 0 ; 
}

void seqlistPushBack(seqlist *seq, seqType value)// Tail element, value Is the value of the inserted element 
{
  assert(seq);// Determines if the pointer is null 
  if (seq->size == maxsize)
  {
    printf(" The element is full and cannot be inserted \n");
    return;
  }
  else
  seq->arr[seq->size++] = value;
}

void seqlistPopBack(seqlist *seq)// Tail deleted elements, value Is the value of the inserted element 
{
  assert(seq);// Determines if the pointer is null 
  if (seq->size ==0)
  {
    printf(" The content is empty and cannot be deleted \n");
    return;
  }
  else
    seq->size--;
}

void seqlistPushHead(seqlist *seq, seqType value)// His head element 
{
  assert(seq);// Determines if the pointer is null 
  if (seq->size == maxsize)
  {
    printf(" The element is full and cannot be inserted \n");
    return;
  }
  else
  {
    int i = seq->size - 1;
    for (; i >= 0; i--)
    {
      seq->arr[i + 1] = seq->arr[i];
    }
    seq->arr[0]=value;
    seq->size++;
  }
}

void seqlistPopHead(seqlist *seq)// To delete the first element 
{
  assert(seq);// Determines if the pointer is null 
  if (seq->size == 0)
  {
    printf(" The content is empty and cannot be deleted \n");
    return;
  }
  else
  {
    size_t i =1;
    for (; i < seq->size; i++)
    {
      seq->arr[i - 1] = seq->arr[i];
    }
    seq->size--;
  }
}

seqType seqlistFind(seqlist *seq, seqType data)// Find any element 
{
  size_t i = 0;
  assert(seq);// Determines if the pointer is null 
  if (seq->size == 0)
  {
    printf(" The linear table is empty \n");
    return -1;
  }
  for (i = 0; i < seq->size - 1; i++)
  {
    if (seq->arr[i] == data)
    {
      return seq->arr[i];
    }
  }
  return -1;
}



seqType seqlistRead_pos(seqlist *seq, size_t pos)// Reads an element at any location 
{
  assert(seq);// Determines if the pointer is null 
  if (seq->size == 0)
  {
    printf(" The content is empty and cannot be read \n");
    return -1;
  }
  else if (pos> seq->size)
  {
    printf(" Read position error \n");
  }
  else
      return seq->arr[pos];
}

size_t seqlistFind_pos(seqlist *seq, seqType value)// Finds the subscript of the specified element 
{
  assert(seq);// Determines if the pointer is null 
  size_t i= 0;
  for (; i < seq->size; i++)
    {
      if (seq->arr[i] == value)
        return i;
    }
  return -1;
}

seqType seqlistModify(seqlist *seq, size_t pos, seqType data)// Modify any location element 
{
  assert(seq);// Determines if the pointer is null 
  if (seq->size == 0)
  {
    printf(" The content is empty and cannot be modified \n");
    return -1;
  }
  for (; pos < seq->size; pos++)
  {
    seq->arr[pos] = data;
    return seq->arr[pos];
  }
}

void seqlistInsert_pos(seqlist *seq, size_t pos, seqType data)// Insert the element anywhere 
{
  assert(seq);
  if (seq->size == maxsize)
  {
    printf(" Unable to continue inserting content because the content is full \n");
    return;
  }
  else if (pos>seq->size)
  {
    printf(" Illegal position, no insertion allowed \n");
    return;
  }
  else
  {
    size_t m= seq->size ;
    for (m = seq->size; m > pos; m--)
    {
      seq->arr[m] = seq->arr[m-1];
    }
    seq->arr[pos] = data;
    seq->size++;
  }
}

void seqlistErase_pos(seqlist *seq, size_t pos)// Deletes an element anywhere 
{
  assert(seq);
  if (seq == NULL)
  {
    printf(" The content is empty !\n");
    return;
  }
  else if (pos > seq->size)
  {
    printf(" The location could not be deleted !\n");
    return;
  }
  else
  {
    size_t i ;
    for (i = pos; i < seq->size-1; i++)
    {
      seq->arr[i] = seq->arr[i+1];
    }seq->size--;
  }
}

void seqlistRemove(seqlist *seq, seqType data)// Remove elements 
{
  assert(seq);// Determines if the pointer is null 
  size_t i = 0;
  i = seqlistFind_pos(seq,data);
  if (i >= 0)
  {
    while (i <seq->size)
    {
      seq->arr[i] = seq->arr[i + 1];
      i++;
    }
    seq->size--;
    return;
  }
  else
  {
    printf(" The element was not found ");
    return;
  }
}

In the insert, delete and other operations, if the location of the element is not clear with the help of drawing can be better understood.

Test function test.c


#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"

void Test_PushBack()// Test the sequence table tail element 
{
  print_seqlist("***** The tail plug 3 Number of elements to the sequence table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
}

void Test_PopBack()// Test to delete elements from the end of the sequence table 
{
  print_seqlist("***** In the tail delete order table 1 An element *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
  seqlistPopBack(&seq);
  PrintSeqList(&seq);

}

void Test_PushHead()// His head element 
{
  print_seqlist("***** In the header sequence table 3 An element *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushHead(&seq, 2);
  seqlistPushHead(&seq, 4);
  seqlistPushHead(&seq, 6);
  PrintSeqList(&seq);

}

void Test_PopHead()// To delete the first element 
{
  print_seqlist("***** In the header delete order table 1 An element *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushHead(&seq, 2);
  seqlistPushHead(&seq, 4);
  seqlistPushHead(&seq, 6);
  PrintSeqList(&seq);
  seqlistPopHead(&seq);
  PrintSeqList(&seq);

}

void Test_Find()// Find any element 
{
  print_seqlist("***** Find any element in the order table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
  int temp = 0;
  temp = seqlistFind(&seq,2);
  printf(" The element found is %d\n", temp);
  printf("\n");
}

void Test_Read_pos()// Reads an element at any location 
{
  print_seqlist("***** Find an element anywhere in the order table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
  int temp = 0;
  temp=seqlistRead_pos(&seq, 1);
  printf(" The subscript for 1 The element is %d\n", temp);
  printf(" The subscript for 4 The element is :");
  seqlistRead_pos(&seq, 4);
  printf("\n");
}

void Test_seqlistFind_pos()// The test finds the subscript of the specified element 
{
  print_seqlist("***** Finds the subscript of the specified element in the order table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 6);
  seqlistPushBack(&seq, 7);
  PrintSeqList(&seq);
  size_t pos = seqlistFind_pos(&seq, 4);
  size_t pos1 = seqlistFind_pos(&seq, 9);
  printf(" The element 4 The subscript for :%d\n", pos);
  printf(" The element 9 The subscript for :%d, Element does not exist \n", pos1);
  printf("\n");
}

void Test_seqlistModify()// Modify any location element 
{
  print_seqlist("***** Modify any element in the sequence table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 6);
  seqlistPushBack(&seq, 7);
  PrintSeqList(&seq);
  int temp = seqlistModify(&seq, 1, 3);
  int temp1 = seqlistModify(&seq, 2, 8);
  PrintSeqList(&seq);
  printf(" Change the subscript to 1 Is: %d\n", temp);
  printf(" Change the subscript to 2 Is: %d\n", temp1);

}

void Test_seqlistInsert()// Insert the element anywhere 
{
  print_seqlist("***** Inserts elements anywhere in the sequence table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 5);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
  seqlistInsert_pos(&seq, 2, 3);
  PrintSeqList(&seq);
  seqlistInsert_pos(&seq, 8, 9); 
}

void Test_seqlistErase_pos()// Delete any location element 
{
  print_seqlist("***** Deletes any location element in the sequence table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 5);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
  seqlistErase_pos(&seq, 1);
  seqlistErase_pos(&seq, 4);
  PrintSeqList(&seq);
}

void Test_Remove()// Delete any element 
{
  print_seqlist("***** Deletes any element in the sequence table *****");
  seqlist seq;
  seqlistInit(&seq);
  seqlistPushBack(&seq, 2);
  seqlistPushBack(&seq, 4);
  seqlistPushBack(&seq, 5);
  seqlistPushBack(&seq, 6);
  PrintSeqList(&seq);
  seqlistRemove(&seq, 4);
  PrintSeqList(&seq);
}

int main()
{
  Test_PushBack();
  Test_PopBack();
  Test_PushHead();
  Test_PopHead();
  Test_Find();
  Test_Read_pos();
  Test_seqlistFind_pos();
  Test_seqlistModify();
  Test_seqlistInsert();
  Test_seqlistErase_pos();
  Test_Remove();
  return 0;
}

Related articles: