# Data structure - primary sequence table

Today we are entering the early stage of data structure learning. We use C language to realize the data structure and write code in the form of multiple files

catalogue

<1> Definition of sequence table

<2> Structure definition of sequence table

<3> Sequence table creation

<4> Insert a data at the end of the sequence table

<6> The tail of the sequence table is deleted

<7> Header deletion of sequence table

<8> Insert at the specified position of the sequence table

<9> Delete at specified location

## <1> Definition of sequence table

The sequence table is in Computer memory Zhongyi array The sequential storage of the linear table refers to the sequential storage of each element in the linear table with a group of storage units with continuous addresses, so that the logically adjacent data elements in the linear table are stored in the adjacent physical storage units, that is, the logical adjacent relationship between the data elements is reflected through the adjacent relationship of the physical storage of the data elements The linear table of [sequential storage structure] is usually called sequential table. Sequential table stores the nodes in the table in a group of storage units with continuous addresses in computer memory.

## <2> Structure definition of sequence table

The sequence table is a continuous space, which is what we often call an array. In c language, the length of the array cannot be changed, so we use a variable length array to realize the sequence table

```typedef int seldata;//In order to make the sequence table can store any type of data, the alias of the type is used here to facilitate the change of data type
typedef struct seqlit
{
seldata* pldata;
int cursize;
int maxsize;
}Sel,*PLsel;```

## <3> Sequence table creation

Initialization of sequence table with dynamic memory request

```PLsel creatseqlist(int maxsize)
{
PLsel seqlist = (PLsel)malloc(sizeof(Sel));
assert(seqlist);
seqlist->pldata = (seldata*)malloc(sizeof(seldata)*maxsize);
assert(seqlist->pldata);
seqlist->cursize = 0;
seqlist->maxsize = maxsize;
return seqlist;
}```

## <4> Insert a data at the end of the sequence table

The sequence table is actually the same as the array. To insert a data at the end of the sequence table is actually to store a new data for the subscript of the current number of elements in the sequence table. First, create a sequence table,

```#include"seqlist.h" / / write code in the form of multiple files. The function declaration is in this file and the definition is in the corresponding. c file
int main()
{
PLsel seqlist = creatseqlist(7);
return 0;
}```

At this point, there is already space for storing 7 data in our sequence table. Next, let's insert data

```void PushByBack(PLsel seqlist, seldata data)
{
seqlist->pldata[seqlist->cursize] = data;
seqlist->cursize++;
}```

In this way, the data is inserted into the prepared sequence table. Of course, in order to facilitate viewing the data, we need to implement an interface to print the elements in the sequence table.

Printing the sequence table is very simple, which is the same as printing the elements in the array.

```void printsql(PLsel seqlist)
{
for (int i = 0; i < seqlist->cursize; i++)
{
printf("%d ", seqlist->pldata[i]);

}
}```

Program running effect diagram

The sequence table logic diagram is shown in the figure below:

At this time, there is already data in the sequence table. However, only 7 data can be stored in the sequence table, so our program will crash when we store the data.

For example:

```#include"seqlist.h"
int main()
{
PLsel seqlist = creatseqlist(7);
for (int i = 0; i < 7; i++)
{
PushByBack(seqlist, i);
}
PushByBack(seqlist, 100);//When the sequence table is full, insert it again
printsql(seqlist);
return 0;
}```

Will cause the program to crash.

Therefore, it is necessary to optimize the insertion function. When the number of elements in the sequence table is equal to or greater than the maximum storage quantity in the sequence table, the array will be expanded. After optimization, the insertion function is as follows:

```void PushByBack(PLsel seqlist, seldata data)
{
if (seqlist->cursize >= seqlist->maxsize)
{
seqlist->pldata = (seldata*)realloc(seqlist->pldata, sizeof(seldata) * ((seqlist->maxsize) * 2));//The capacity of the array is doubled by the realloc function
assert(seqlist->pldata);
seqlist->pldata[seqlist->cursize++] = data;
seqlist->maxsize *= 2;
}

else
{
seqlist->pldata[seqlist->cursize] = data;
seqlist->cursize++;

}
}```

Insert again at this time, and you can see that the code can run normally

```#include"seqlist.h"
int main()
{
PLsel seqlist = creatseqlist(7);
for (int i = 0; i < 7; i++)
{
PushByBack(seqlist, i);
}
printsql(seqlist);
printf("\n");
PushByBack(seqlist, 100);
printsql(seqlist);
return 0;
}```

## <5> Sequence table header deletion

Deleting the header is more troublesome because the physical addresses of the data storage in the sequence table are continuous. Therefore, we need to move the data in the sequence table backward, and then insert the data into the beginning of the sequence table.

In this way, the last head position is empty and new data is inserted

Of course, like the tail insertion above, in order to store more data, the capacity of the sequence table is expanded when the capacity of the sequence table is insufficient.

The code is as follows:

```void PushByHead(PLsel seqlist, seldata data)
{
if (seqlist->cursize >= seqlist->maxsize)
{
seqlist->pldata = (seldata*)realloc(seqlist->pldata, sizeof(seldata) * ((seqlist->maxsize) * 2));
assert(seqlist->pldata);
for (int i = seqlist->cursize; i >0; i--)
{
seqlist->pldata[i - 1] = seqlist->pldata[i];
}
seqlist->pldata[0] = data;
seqlist->cursize++;
seqlist->maxsize *= 2;
}
else
{
for (int i = seqlist->cursize; i > 0; i--)
{
seqlist->pldata[i] = seqlist->pldata[i-1];
}
seqlist->pldata[0] = data;
seqlist->cursize++;
}
}```

Execution code:

```#include"seqlist.h"
int main()
{
PLsel seqlist = creatseqlist(7);
for (int i = 0; i < 7; i++)
{
PushByBack(seqlist, i);
}
printsql(seqlist);
printf("\n");
PushByBack(seqlist, 100);
printsql(seqlist);
printf("\n");
printsql(seqlist);
return 0;
}```

The effects are as follows:

## <6> The tail of the sequence table is deleted

This is very simple. Just let our cursize --.

The code is as follows:

```void PopByBack(PLsel seqlist)
{
if (seqlist->cursize == 0)
{
printf("The sequence table is empty and cannot be deleted\n");
return;
}
else
{
seqlist->cursize--;
}
}
```

Execution code:

```#include"seqlist.h"
int main()
{
PLsel seqlist = creatseqlist(10);
for (int i = 0; i < 10; i++)
{
PushByBack(seqlist, i);
}

printsql(seqlist);
printf("\n");
PushByBack(seqlist, 100);
printsql(seqlist);
printf("\n");
printsql(seqlist);
printf("\n");
PopByBack(seqlist);
printsql(seqlist);
return 0;
}```

## <7> Header deletion of sequence table

Deleting the header of the sequence table also has some trouble. Like header insertion, we need to move the following elements to cover the header elements

The code is as follows:

```void PopByHead(PLsel seqlist)
{
if (seqlist->cursize == 0)
{
printf("The sequence table is empty and cannot be deleted\n");
return;
}
for (int i = 0; i +1< seqlist->cursize; i++)
{
seqlist->pldata[i] = seqlist->pldata[i + 1];
}
seqlist->cursize--;
}```

Execution code:

## <8> Insert at the specified position of the sequence table

To insert the specified location, first find the specified location, then move the data at the specified location and later, and then insert the new data at the specified location. Of course, we also consider the problem of array expansion

The code is as follows:

```void PushByPos(PLsel seqlist, int pos, seldata data)
{
if (pos < 0)//When pos is less than 0, it cannot be inserted
{
return;
}
if (seqlist->cursize >= seqlist->maxsize)
{
seqlist->pldata = (seldata*)realloc(seqlist->pldata, sizeof(seldata) * ((seqlist->maxsize) * 2));
assert(seqlist->pldata);
seqlist->maxsize *= 2;

for (int i = seqlist->cursize; i >pos ; i--)
{
seqlist->pldata[i] = seqlist->pldata[i - 1];
}
seqlist->pldata[pos] = data;
seqlist->cursize++;
}
else
{
for (int i = seqlist->cursize; i > pos; i--)
{
seqlist->pldata[i] = seqlist->pldata[i - 1];
}
seqlist->pldata[pos] = data;
seqlist->cursize++;
}
}
```

Execution code:

```#include"seqlist.h"
int main()
{
PLsel seqlist = creatseqlist(10);
for (int i = 0; i < 10; i++)
{
PushByBack(seqlist, i);
}
printsql(seqlist);
printf("\n");
PushByBack(seqlist, 100);
printsql(seqlist);
printf("\n");
printsql(seqlist);
printf("\n");
PopByBack(seqlist);
printsql(seqlist);
printf("\n");
printsql(seqlist);
PushByPos(seqlist, 3, 300);
printf("\n");
printsql(seqlist);
return 0;
}```

## <9> Delete at specified location

The deletion of a specified location is to find the specified location and move the element after the specified location forward.

The code is as follows:

```void PopByPos(PLsel seqlist, int pos)
{
if (pos < 0||seqlist->cursize==0)
{
return;
}
else
{
for (int i = pos; i-+1 < seqlist->cursize; i++)
{
seqlist->pldata[i] = seqlist->pldata[i + 1];
}
seqlist->cursize--;
}
}
```

Execution code:

```#include"seqlist.h"
int main()
{
PLsel seqlist = creatseqlist(10);
for (int i = 0; i < 10; i++)
{
PushByBack(seqlist, i);
}

printsql(seqlist);
printf("\n");
PushByBack(seqlist, 100);
printsql(seqlist);
printf("\n");
printsql(seqlist);
printf("\n");
PopByBack(seqlist);
printsql(seqlist);
printf("\n");
printsql(seqlist);
PushByPos(seqlist, 3, 300);
printf("\n");
printsql(seqlist);
PopByPos(seqlist,2);
printf("\n");
printsql(seqlist);

return 0;
}```

Tags: C data structure

Posted on Wed, 08 Sep 2021 13:28:08 -0400 by gregor63