#include
#include
#define maxsize 100
struct LNode{ //指针变量名称
union {
int num;
char ch;
}data;
struct LNode *next; //结构体类型 ,lnode直接定义结构体变量或者指针
}*mylink; //重定义为mylink
void deletee(struct LNode *aa){//定义删除函数
int pos,i;
struct LNode *p,*q;
if(aa->data.num==0) //判断是否为空 。
printf("EMPTY!!!\n");//空
else{
printf("Please input position:"); //如果为空输出Please input position:
scanf("%d",&pos);
if (pos>aa->data.num || pos
printf("THE POSITION IS ERROR!!!\n");// 这个位置是错误的
else{
i=1;
p=aa;
while(i
p=p->next;
i++;
}
q=p->next;
p->next=q->next;
aa->data.num--;
free(q);
}
p=aa->next;
while(p!=NULL){
printf("%c",p->data.ch );
p=p->next;
}
printf("\n");
}
return;
}
void insert(struct LNode aa){ //定义插入函数
int pos,i;
char x;
struct LNode *p,*q;//申请内存,构建链式储存结构堆栈。
q=(struct LNode)malloc(sizeof(struct LNode));
if(q==NULL) //判断是否满了
printf("FULL!!!\n"); //满
else{
printf("Please input position:");
scanf("%d",&pos);
if (pos>aa->data.num+1 || pos
printf("THE POSITION IS ERROR!!!\n");
else{
getchar();
printf("Please input the char:");
scanf("%c",&x);
i=1;
p=aa;
while(i
p=p->next;
i++;
}
q->data.ch=x;
q->next=p->next;
p->next=q;
aa->data.num++;
}
p=aa->next;
while(p!=NULL){
printf("%c",p->data.ch);
p=p->next;
}
printf("\n");
}
return;
}
int main(int n){
mylink=(struct LNode*)malloc(sizeof(struct LNode));
mylink->data.num=0;
mylink->next=NULL;
while(1){
printf("1.INSERT\n2.DELETE\n3.EXIT\nPlease input the number:");
scanf("%d",&n);
switch (n){
case 1: insert(mylink);
break;
case 2: deletee(mylink);
break;
case 3: return(0);
default: break;
}
}
}