SoFunction
Updated on 2025-03-04

Detailed explanation of the examples of C language implementing static order tables

Detailed explanation of the examples of C language implementing static order tables

Linear Table

Defining a sequential table means opening up a continuous storage space in memory and identifying it with a name. Only when a sequence table is defined can the order table be used to store data elements and can various operations be performed on the order table.

Next, take a look at the static order table and directly follow the code:

#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include <>
#include <>
#include <>
#include <>

#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__

#define _CRT_SECURE_NO_WARNINGS 1

#include ""

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

#define _CRT_SECURE_NO_WARNINGS 1
#include ""

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", [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", [pos]);
}
int main()
{
 test4();
 system("pause");
 return 0;
}

Implementation of dynamic order tables:https:///article/

If you have any questions about the above dynamic sequence table using C language, please leave a message or go to the community of this website to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this website!