大佬们 帮忙看看栈和队列的类型定义问题

LANGZHIZHEN 2020-09-12 10:54:19
我栈类型定义是
typedef struct stacknote
{
char data;
struct stacknote *next;
}stacknote,*Linkstackptr;

typedef struct node
{
int count;
Linkstackptr top;
}Linkstack;
在置空栈函数中的动态分配内存格式
void Initstack(Linkstack* s)
{
s = (Linkstack*)malloc(sizeof(Linkstack));
if (*s == NULL)
{
return;
}
else
{
s->top = NULL;
}
}



这是队列的类型定义和动态分配内存格式
typedef struct queuenote
{
int data;
struct queuenote *next;
}queuenote,*Linkqueueptr;

typedef struct
{
Linkqueueptr front,rear;
}Linkqueue;

置空
int Initqueue(Linkqueue *p)
{
p->front=p->rear=(Linkqueueptr)malloc(sizeof(queuenote));
if(!p->front)
{
return 0;
}
p->front->next=p->rear->next=NULL;
return 1;
}
为什么这两个不一样 我在适应了栈的格式后,把他套在队列上 结果不对这是为什么求大佬解析一下。

最后还有一个小问题
void print(Linkqueue q)
{
while(q.front!=q.rear)
{
q.front=q.front->next; //注意front指向头结点非第一个结点 故先指向下一个
printf("%d\n",q.front->data);
}
}在打印队列的时候为啥是q.front而不是q->front
这两个有啥差别
...全文
200 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
壶中日月小 2020-09-15
  • 打赏
  • 举报
回复
栈是先进后出,只需要一个栈顶,都是从栈顶压栈和出栈;队列是先进先出,头部读出,尾部写入
自信男孩 2020-09-14
  • 打赏
  • 举报
回复
楼主看一下Linkqueue这个是结构体类型还是结构体指针类型。typedef的时候Linkqueue前面没有*.
所以Linkqueue q;定义的是结构体变量,因此用.不用->

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧