The description of the sequential structure requires 3 attributes:
? Starting position of storage space: array
? Array length of the maximum storage capacity of a linear table
? Current length of linear table
LOC (AI) = loc (AI) + (i-l) *c
#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20/* Storage space Initial allocation */
typedef int STATUS; /* Status is the type of function, whose value is the function result status code, such as OK, etc. */
typedef int ELEMTYPE; /* The Elemtype type is based on the actual situation and is assumed to be int */
Status visit (elemtype c)
{
printf ("%d", c);
return OK;
}
typedef struct
{
Elemtype Data[maxsize]; /* array, storing data elements */
int length; /* Current length of linear table */
}sqlist;
/* Initialize sequential linear table */
Status initlist (sqlist *l)
{
l->length=0;
return OK;
}
/* Initial conditions: Sequential linear table L already exists. Operation Result: Returns True if L is an empty table, otherwise false */
Status listempty (sqlist L)
{
if (l.length==0)
return TRUE;
Else
return FALSE;
}
/* Initial conditions: Sequential linear table L already exists. Operation Result: Reset L to Empty table */
Status clearlist (sqlist *l)
{
l->length=0;
return OK;
}
/* Initial conditions: Sequential linear table L already exists. Operation Result: Returns the number of data elements in L */
int Listlength (sqlist L)
{
return l.length;
}
/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l) */
/* Operation Result: Use E to return the value of the I data element in L, note that I refers to the position, the 1th position of the array is starting from 0 */
Status Getelem (sqlist l,int i,elemtype *e)
{
if (l.length==0 | | i<1 | | i>l.length)
return ERROR;
*E=L.DATA[I-1];
return OK;
}
/* Initial conditions: Sequential linear table L already exists */
/* Operation Result: Returns the bit order of the 1th data element in L that satisfies the relationship with E. */
/* If such a data element does not exist, the return value is 0 */
int Locateelem (sqlist l,elemtype e)
{
int i;
if (l.length==0)
return 0;
for (i=0;i<l.length;i++)
{
if (l.data[i]==e)
Break
}
if (i>=l.length)
return 0;
return i+1;
}
/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l), */
/* Operation result: Insert a new data element before the I position in L e,l length plus 1 */
Status Listinsert (sqlist *l,int i,elemtype e)
{
int k;
if (l->length==maxsize)/* Sequential linear table is full */
return ERROR;
if (i<1 | | i>l->length+1)//* When I is smaller than the first position or larger than the last position, */
return ERROR;
if (i<=l->length)/* If the insertion data location is not at the end of the table */
{
for (k=l->length-1;k>=i-1;k--)/* The data element after the insertion position is moved backward by one */
l->data[k+1]=l->data[k];
}
l->data[i-1]=e; /* Insert new element */
l->length++;
return OK;
}
/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l) */
/* Operation Result: Delete the I data element of L and return its value with E, the length of L minus 1 */
Status listdelete (sqlist *l,int i,elemtype *e)
{
int k;
if (l->length==0)/* Linear table is empty */
return ERROR;
if (i<1 | | i>l->length)/* Delete location Incorrect */
return ERROR;
*e=l->data[i-1];
if (i<l->length)/* If Delete is not last location */
{
for (k=i;k<l->length;k++)/* Removes the trailing element of the position forward */
l->data[k-1]=l->data[k];
}
l->length--;
return OK;
}
/* Initial conditions: Sequential linear table L already exists */
/* Operation result: output per data element of L, in turn */
Status Listtraverse (sqlist L)
{
int i;
for (i=0;i<l.length;i++)
Visit (L.data[i]);
printf ("\ n");
return OK;
}
void Unionl (SqList *la,sqlist Lb)
{
int la_len,lb_len,i;
Elemtype e;
La_len=listlength (*la);
Lb_len=listlength (LB);
for (i=1;i<=lb_len;i++)
{
Getelem (lb,i,&e);
if (! Locateelem (*la,e))
Listinsert (la,++la_len,e);
}
}
int main ()
{
SqList L;
SqList Lb;
Elemtype e;
Status i;
int j,k;
I=initlist (&L);
printf ("Initialize L: l.length=%d\n", l.length);
for (j=1;j<=5;j++)
I=listinsert (&L,1,J);
printf ("The table head in L in turn inserted after the l.data=:");
Listtraverse (L);
printf ("l.length=%d \ n", l.length);
I=listempty (L);
printf ("L is empty: i=%d (1: Yes 0: NO) \ n", i);
I=clearlist (&L);
printf ("Empty L after: l.length=%d\n", l.length);
I=listempty (L);
printf ("L is empty: i=%d (1: Yes 0: NO) \ n", i);
for (j=1;j<=10;j++)
Listinsert (&L,J,J);
printf ("After the footer of L is inserted in 1~10: L.data=");
Listtraverse (L);
printf ("l.length=%d \ n", l.length);
Listinsert (&l,1,0);
printf ("The table header in L is inserted after 0: L.data=");
Listtraverse (L);
printf ("l.length=%d \ n", l.length);
Getelem (l,5,&e);
printf ("The value of the 5th element is:%d\n", e);
for (j=3;j<=4;j++)
{
K=locateelem (L,J);
if (k)
printf ("The value of%d element is%d\n", k,j);
Else
printf ("no element with a value of%d \ n", j);
}
K=listlength (L); /* k for table length */
for (j=k+1;j>=k;j--)
{
I=listdelete (&l,j,&e); /* Delete the first J data */
if (i==error)
printf ("Delete%d data failed \ n", j);
Else
printf ("Delete element%d value is:%d\n", j,e);
}
printf ("Output the elements of L sequentially:");
Listtraverse (L);
j=5;
Listdelete (&l,j,&e); /* Delete 5th Data */
printf ("Delete element%d value is:%d\n", j,e);
printf ("Output the elements of L sequentially:");
Listtraverse (L);
Constructs a 10-digit lb
I=initlist (&LB);
for (j=6;j<=15;j++)
I=listinsert (&LB,1,J);
Unionl (&L,LB);
printf ("Output the elements of the L with LB in turn:");
Listtraverse (L);
return 0;
}
Implementation of sequential storage structure C language for linear tables