#include "stdio.h"
#include "conio.h"
# include "malloc.h"
#define maxsize 1000
#pragma warning(disable:4996);
typedef char datatype;
typedef struct
{
datatype data[maxsize]; /*定义线性表为一个向量*/
int last;
} sequenlist;
/* 插入函数*/
int insert(sequenlist* L, char x, int i) /*将新结点X插入到表L的第I个位置*/
/*L为指向线性表sequenlist类型的指针变量*/
{
int j;
if ((*L).last == maxsize - 1)
{
printf("overflow\n");
return 0;
}
else
if ((i < 1) || (i > (*L).last + 1))
{
printf("error\n"); /* 非法位置*/
return 0;
}
else
{
for (j = (*L).last; j >= i - 1; j--)
;
;
;
}
return (1);
}
/* 删除函数*/
//int delete(sequenlist *L, int i) /*从表中删除第I个结点*/
int dellist(sequenlist* L, int i)
{
int j;
if ((i < 1) || (i > (*L).last + 1)) /* 非法位置*/
{
printf("error\n");
return 0;
}
else
{
for (j = i; j <= (*L).last; j++)
L->data[i ] = L->data[i + 1];
L->last = L->last - 1;
return (1);
}
}
/* 生成线性表*/
void creatlist(sequenlist* L)
{
int i, j, n;
printf(" input the number of the data\n"); /*输入N个数据*/
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("data[%d]=", i);
getchar();
scanf("%c", &(*L).data[i]);
}
(*L).last = n - 1;
printf("\n");
}
/* 输出线性表*/
void printout(sequenlist* L)
{
int i;
for (i = 0; i <= (*L).last; i++)
{
printf(" data[%d]=", i);
printf(" %c ", (*L).data[i]);
}
}
int main()
{
sequenlist* L;
char cmd, x;
int i, s;
L = (sequenlist*)malloc(sizeof(sequenlist));
creatlist(L);
printout(L);
do
{
printf(" i , I -----------insert\n");
printf(" d , D -----------delete\n");
printf(" q , Q -----------exit\n");
do
{
fflush(stdin);
scanf("%c", &cmd);
} while ((cmd != 'd') && (cmd != 'D') && (cmd != 'I') && (cmd != 'i') && (cmd != 'q') && (cmd != 'Q'));
switch (cmd)
{
case 'i':
case 'I': {
printf("please enter the data which you will insert:");
fflush(stdin);
scanf("%c", &x);
printf("please enter the location which you want to insert:");
scanf("%d", &i);
insert(L, x, i);
printout(L);
break;
}
case 'd':
case 'D': {
printf("please enter the location which you want delete:");
getchar();
scanf("%d", &i);
dellist( L,i);
printout(L);
break;
}
}
} while ((cmd != 'q') && (cmd != 'Q'));
getchar();
}