guaery 2017-11-14 13:02 采纳率: 0%
浏览 1094

数据结构堆栈的链式存储结构实现代码,求大神精确解释该代码的头节点,压入栈堆和删除并返回堆栈的栈顶元素

#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;
}
}

}

  • 写回答

1条回答 默认 最新

  • 小邓在路上 2017-11-14 13:15
    关注

    你这个代码上传的时候能不能对齐 啊 ,我真的是服了

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站