C language implementation of static sequence table of the case details

  • 2020-05-26 09:54:46
  • OfStack

The C language implements an example of a static sequence table

The linear table

To define a sequence table is to carve out a contiguous segment of memory and identify it with a name. Only when a sequence table is defined can data elements be stored in the sequence table and various operations be performed on the sequence table.

Next, take a look at the static sequence table and go directly to the code:

SeqList.h


#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

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

#define MAX 10

typedef int DataType;

typedef struct SeqList
{
 DataType data[MAX];
 int sz;
}SeqList,*pSeqList;

void InitSeqList(pSeqList ps);
void PushBack(pSeqList ps, DataType d);
void PopBack(pSeqList ps);
void PushFront(pSeqList ps, DataType d);
void PopFront(pSeqList ps);
void Display(const pSeqList ps);
int Find(pSeqList ps, DataType d);
void Insert(pSeqList ps, DataType d, int pos);
void Remove(pSeqList ps, DataType d);
void RemoveAll(pSeqList ps, DataType d);
void Reverse(pSeqList ps);
void Sort(pSeqList ps);
int BinarySearch(pSeqList ps, DataType d);

#endif//__SEQLIST_H__

SeqList.c


#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

void InitSeqList(pSeqList ps)
{
 assert(ps);
 ps->sz = 0;
 memset(ps->data, 0, sizeof(DataType)*MAX);
}
void PushBack(pSeqList ps, DataType d)
{
 assert(ps);
 if (ps->sz == MAX)
 {
  return;
 }
 ps->data[ps->sz] = d;
 ps->sz++;
}
void PopBack(pSeqList ps)
{
 assert(ps);
 if (ps->sz == 0)
 {
  return;
 }
 ps->sz--;
}
void PushFront(pSeqList ps, DataType d)
{
 int i = 0;
 assert(ps);
 for (i = ps->sz; i >= 1; i--)
 {
  ps->data[i] = ps->data[i - 1];
 }
 ps->data[0] = d;
 ps->sz++;
}
void PopFront(pSeqList ps)
{
 int i = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  ps->data[i] = ps->data[i + 1];
 }
 ps->sz--;
}
void Display(const pSeqList ps)
{
 int i = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  printf("%d ", ps->data[i]);
 }
 printf("\n");
}
int Find(pSeqList ps, DataType d)
{
 int i = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  if (ps->data[i] == d)
  {
   return i;
  }
 }
 return -1;
}
void Insert(pSeqList ps, DataType d, int pos)
{
 int i = 0;
 assert(ps);
 if (ps->sz == MAX)
 {
  return;
 }
 // way 1
 //for (i = ps->sz - 1; i >= pos; i--)
 //{
 // ps->data[i + 1] = ps->data[i];
 //}
 // way 2
 memmove(ps->data + pos + 1, ps->data + pos, sizeof(DataType)*(ps->sz - pos));
 ps->data[pos] = d;
 ps->sz++;
}
void Remove(pSeqList ps, DataType d)
{
 int i = 0;
 int pos = 0;
 assert(ps);
 pos = Find(ps, d);
 if (pos != -1)
 {
  for (i = pos; i < ps->sz; i++)
  {
   ps->data[i] = ps->data[i + 1];
  }
  ps->sz--;
 }
}
void RemoveAll(pSeqList ps, DataType d)
{
 int i = 0;
 int pos = 0;
 assert(ps);
 pos = Find(ps, d);
 while ((pos = Find(ps, d)) != -1)
 {
  for (i = pos; i < ps->sz; i++)
  {
   ps->data[i] = ps->data[i + 1];
  }
  ps->sz--;
 }
}
void Reverse(pSeqList ps)
{
 int left = 0;
 int right = ps->sz - 1;
 assert(ps);
 while (left < right)
 {
  DataType tmp = ps->data[right];
  ps->data[right] = ps->data[left];
  ps->data[left] = tmp;
  left++;
  right--;
 }
}
void Sort(pSeqList ps)
{
 int i = 0;
 int j = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  for (j = 0; j < ps->sz - i - 1; j++)
  {
   if (ps->data[j]>ps->data[j + 1])
   {
    DataType tmp = ps->data[j];
    ps->data[j] = ps->data[j + 1];
    ps->data[j + 1] = tmp;
   }
  }
 }
}
int BinarySearch(pSeqList ps, DataType d)
{
 int left = 0;
 int right = ps->sz - 1;
 while (left <= right)
 {
  int mid = left - ((left - right) >> 1);
  if (d > ps->data[mid])
  {
   left = mid + 1;
  }
  else if (d < ps->data[mid])
  {
   right = mid - 1;
  }
  else
  {
   return mid;
  }
 }
 return -1;
}

test.c


#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

void test1()
{
 SeqList list;
 InitSeqList(&list);
 PushBack(&list, 1);
 PushBack(&list, 2);
 PushBack(&list, 3);
 PushBack(&list, 4);
 Display(&list);
 PopBack(&list);
 Display(&list);
 PopBack(&list);
 Display(&list);
 PopBack(&list);
 Display(&list);
 PopBack(&list);
 Display(&list);
}

void test2()
{
 int pos = 0;
 SeqList list;
 InitSeqList(&list);
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 2);
 PushFront(&list, 4);
 PushFront(&list, 2);
 Display(&list);
 pos = Find(&list, 3);
 printf("%d\n", list.data[pos]);
 PopFront(&list);
 Display(&list);
 PopFront(&list);
 Display(&list);
 PopFront(&list);
 Display(&list);
 PopFront(&list);
 Display(&list);
}
void test3()
{
 int pos = 0;
 SeqList list;
 InitSeqList(&list);
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 2);
 PushFront(&list, 4);
 PushFront(&list, 2);
 Display(&list);
 Insert(&list, 6, 2);
 Display(&list);
 Remove(&list, 1);
 Display(&list);
 RemoveAll(&list, 2);
 Display(&list);
}
void test4()
{
 int pos = 0;
 SeqList list;
 InitSeqList(&list);
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 2);
 PushFront(&list, 4);
 Display(&list);
 Reverse(&list);
 Display(&list);
 Sort(&list);
 Display(&list);
 pos = BinarySearch(&list, 3);
 printf("%d\n", list.data[pos]);
}
int main()
{
 test4();
 system("pause");
 return 0;
}

The realization of the dynamic sequence table: https: / / www ofstack. com article / 120876. htm

If you have any questions about the implementation of the above dynamic order table using C language, please leave a message or go to the community of this site to communicate and discuss, thank you for reading, hope to help you, thank you for your support to this site!


Related articles: