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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制