#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERR 0
#define MAXSIZE 100
Defining the sequential storage structure
typedef struct list{
int elem[maxsize];
int last;
}seqlist;
Initializing a linear table
Seqlist *initlist ()
{
Seqlist *l;
L = (Seqlist *) malloc (sizeof (seqlist));
L->last = 0;
return L;
}
Find operations, find by content
int Locate (seqlist *l,int e)
{
int i=0;
for (i;i<l->last-1;i++)
{
if (l->elem[i]==e)
{
return i+1;
}
}
return-1;
}
The insert operation. Insert a new element before the first position in the table
int inslist (seqlist *l,int i,int e)
{
int k;
if (i<1| | I>L->LAST+1)
{
printf ("The insertion position is illegal! ");
return ERR;
}
if (l->last>=maxsize)
{
printf ("The table is full and cannot be inserted!") ");
return ERR;
}
for (k=l->last-1;k>=i;k--)
{
l->elem[k+1]=l->elem[k];
}
l->elem[i-1]=e;
l->last++;
return OK;
}
Deletes the first element of the table and returns its value with the pointer E
int dellist (seqlist *l,int i,int *e)
{
int k;
if (i<1| | I>l->last)
{
printf ("Delete location is not legal!") ");
return ERR;
}
*e=l->elem[i-1];
for (k=i-1;k<l->last;k++)
{
l->elem[k]=l->elem[k+1];
}
l->last--;
return OK;
}
Output of linear tables
void Print_seqlist (Seqlist *l)
{
Int J;
for (j=1;j<=l->last;j++)
{
printf ("%d\t", l->elem[j-1]);
if (j%5==0)
{
printf ("\ n");
}
}
}
int main ()
{
Seqlist *l;
L=initlist ();
int a,j,locate,e;
for (j=1;j<10;j++)
{
printf ("Please enter the number to insert: \ n");
scanf ("%d", &a);
Inslist (L,A,J);
}
printf ("\ n");
printf ("————————————————————: \ n");
printf ("————————————————————: \ n");
printf ("The inserted list is: \ n");
Print_seqlist (L);
printf ("\ n");
printf ("————————————————————: \ n");
printf ("————————————————————: \ n");
printf ("Find if element 5 exists, display subscript: \ n");
Locate = locate (l,5);
printf ("%d\n", locate);
printf ("————————————————————: \ n");
printf ("————————————————————: \ n");
printf ("Delete the 7th element, return the deleted element: \ n");
Dellist (l,7,&e);
printf ("The element being deleted is:%d\n", e);
return 0;
}
Definition, initialization, and other operations of the sequential list of linear tables