#include
#include
#define MAXSIZE 100
struct List
{
int element[MAXSIZE]; //鏈表中最大節點數
int len; //當前鏈表的長度
}*t;
void print(List *t);
int Isemtty(List *t) //判斷鏈表是否為空
{
if(t->len==0)
return 0;
else
return -1;
}
void getElement(List *t,int i) //獲取表中第i個元素(下標從0開始)
{
if(i<0||i>t->len)
printf("fail!\n");
else
printf("%d\n",t->element[i]);
}
void add(List *t) //向鏈表中添加節點
{
int i,j;
printf("input the count of new numner:");
scanf("%d",&j);
printf("input new numbers:\n");
for(i=0;ielement[i]));
t->len++;
}
printf("添加成功!\n");
}
void insert(List *t)
{
int i,j,x;
printf("input the value of i and x :"); //在表中第i個節點之前插入一個節點
scanf("%d%d",&i,&x);
if(i<=0||i>t->len||t->len==MAXSIZE) //可以插入的條件
printf("can not insret!\n");
else
{
for(j=t->len;j>=i;j--)
{
t->element[j]=t->element[j-1]; //從插入位置開始所有節點後移
}
t->element[i]=x;
t->len++;
}
}
void Delete(List *t,int i) //刪除表中第i節點
{
int j;
if(i<0||i>=t->len)
printf("can not delete!\n");
else
{
for(j=i;jlen;j++)
t->element[j]=t->element[j+1]; //從被刪除位置下一個節點開始所有節點前移
t->len--;
}
}
void clear(List *t) //清空鏈表
{
t->len=0;
}
void modify(List *t,int i) //修改鏈表中第i個節點
{ int b;
printf("intut a number:");
scanf("%d",&b);
if(i<0||i>t->len)
printf("can not modify!\n");
else
t->element[i]=b;
}
void show(List *t) //顯示鏈表中所有節點的數據
{
int i=0;
for(;ilen;i++)
printf("%2d",t->element[i]);
printf("\n");
}
int main()
{
struct List *p;
p=new List;
p->len=0;
int ch,i,x;
add(p);
while(1)
{
printf("******************************************************\n");
printf(" 1.judge 2.Getelement 3.add 4.delete \n 5.insert 6.show 7.modify 8.clear 0.quit\n");
printf("******************************************************\n");
printf("please input your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("%d\n",Isemtty(p));
break;
case 2:
{
printf("input a number:");
scanf("%d",&i);
getElement(p,i);
break;
}
case 3: add(p);
break;
case 4: {
printf("input value of i:");
scanf("%d",&i);
Delete(p,i);
break;
}
case 5: insert(p);
break;
case 6: show(p);
break;
case 7: {
printf("input value of i:");
scanf("%d",&i);
modify(p,i);
break;
}
case 8: clear(p);
break;
case 0: exit(0);
default:printf("你的輸入有誤!請重新輸入");
break;
}
}
return 0;
}