#include "stdio.h"
#define max 80
#define Error 0
#define Ok 1
typedef int ElemType;
typedef struct
{
ElemType elem[max];
int last;
}SeqList;
SeqList *NewSeq()
{
SeqList *L;
L=(SeqList *)malloc(sizeof(SeqList));
L->last=0;
return L;
}
int AddSeq(SeqList *L,int i,ElemType e)
{
int k;
if(L->last>=max)
{
printf("The List is full!\n");
return(Error);
}
i--;
if(i>L->last)
{
printf("Error Add Point!\n");
return(Error);
}
for(k=L->last;k>=i;k--)
L->elem[k]=L->elem[k-1];
L->elem[i]=e;
L->last++;
return(Ok);
}
int MovSeq(SeqList *L,int i)
{
if(L->last==0)
{
printf("The List is empty!\n");
return(Error);
}
i--;
if(i>=L->last)
{printf("Error Mov Point!\n");
return(Error);
}
for(;i<=L->last-1;i++)
L->elem[i]=L->elem[i+1];
L->last--;
return(Ok);
}
int PtSeq(SeqList *L)
{
int i;
if(L->last==0)
{
printf("The List is Empty!\n");
return(Error);
}
for(i=0;i<L->last;i++)
printf("%d ",L->elem[i]);
return(Ok);
}
main()
{
int i;
int o=1;
ElemType e;
char c;
SeqList *L;
L=NewSeq();
do
{printf("\n********************************************************************************");
printf("\nPlease press your key to choose your order:");
printf("\n press 'a' to Add Element;");
printf("\n press 'm' to Mov Element;");
printf("\n press 'p' to Print List;");
printf("\n press 'q' to Quite the Program.\n-");
loop:
c=getch();
switch(c)
{case 'a': printf("%c\n",c);
printf("Please Enter the Add Point:");
scanf("%d",&i);
getchar();
printf("Please Enter the Add Elem :");
scanf("%d",&e);
getchar();
AddSeq(L,i,e);
break;
case 'm' :printf("%c\n",c);
printf("Please Enter the Mov Point:");
scanf("%d",&i);
getchar();
MovSeq(L,i);
break;
case 'p' :printf("%c\n",c);
printf("The List is:\n");
PtSeq(L);
getchar();
break;
case 'q': printf("%c\n",c);
printf("Are you sure to quit the program?(y/n)-");
while(!(c=='y'||c=='n'))
c=getch();
if(c=='y')
o=0;
else
printf("%c\n",c);
break;
default: goto loop;
break;
}
}while(o);
}
文章来源:
http://blog.coders.com.cn/jnn10/archive/2007/01/16/891.html