Copy Code code as follows:
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 1024
typedef CHAR datatype;
typedef struct
{
DataType data[maxsize];
int last;
}sequenlist;
/* Inserts data x before the first element, counting the elements starting at 0 */
int Insert (sequenlist *l,datatype x,int i)
{
int J;
if (l->last==maxsize-1)
{
printf ("Overflow");
return 0;
}
else if (i<0) | | (I>l->last))
{
printf ("Error,please input" right ' I ');
return 0;
}
else
{
for (j=l->last;j>=i;j--)
{
l->data[j+1]=l->data[j];
L->data[i]=x;
l->last=l->last +1;
}
return (1);
}
/* Delete the first element, the element starts counting from 0 * *
int dellist (sequenlist *l,int i)
{
if ((i<0) | | (I>l->last))
{printf ("Error,please input" right ' I ');
return 0;}
Else
{
for (; i<l->last; i++)
l->data[i]=l->data[i+1];
L->last =l->last-1;
return (1);
}
}
/* Create a sequential table with an element of a single character * *
void CreateList (Sequenlist *l)
{
int n,i;
char tmp;
printf ("Please enter the number of elements: \ n");
scanf ("%d", &n);
for (i=0;i<n;i++)
{
printf ("data[%d]=", I);
Fflush (stdin);
scanf ("%c", &tmp);
L->data[i] =tmp;
}
l->last=n-1;
printf ("n");
}
/* Print ORDER form * *
void Printflist (Sequenlist *l)
{
int i;
for (i=0;i<l->last; i++)
{
printf ("data[%d]=", I);
printf ("%c\n", L->data [i]);
}
}
Main ()
{
Sequenlist *l;
Char cmd,x;
int i;
L= (Sequenlist *) malloc (sizeof (sequenlist)); /* pointers are initialized before use.
CreateList (L);
Printflist (L);
Todo
{
printf ("I,i ...") Insert \ n ");
printf ("D,d ...") Delete \ n ");
printf ("Q,q ...") Exit \ n ");
Todo
{
Fflush (stdin);
scanf ("%c", &cmd);
}while (cmd!= ' d ') && (cmd!= ' d ') && (cmd!= ' Q ') && (cmd!= ' Q ') && (cmd!= ' i ') && ( cmd!= ' I '));
Switch (CMD)
{
Case ' I ':
Case ' I ':
printf ("Please enter the data you want to insert:");
Fflush (stdin);
scanf ("%c", &x);
printf ("Please enter the location you want to insert:");
scanf ("%d", &i);
Insert (l,x,i);
Printflist (L);
Break
Case ' d ':
Case ' D ':
printf ("Please enter the location of the element you want to delete:");
Fflush (stdin);
scanf ("%d", &i);
Dellist (L,i);
Printflist (L);
Break
}
}while (cmd!= ' Q ') && (cmd!= ' Q '));
}