|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问这个程序为什么有问题呢?
Q.rear和Q.front开始都指向同一个地方,为什么好像值并不相同?
- //第30集课后练习:功能:用户输入什么,就打印出什么!
- //注意区别队列与线性表的区别,此处应该使用队列进行操作
- //对头front出队,队尾rear出队
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Qnode
- {
- char data;
- struct Qnode *next;
- }Qnode, *Qnodep;
- typedef struct
- {
- Qnodep front;
- Qnodep rear;
- }LinkQueue;
- LinkQueue Q;
- void InitQueue(LinkQueue &Q)//创建一个空队列!
- {
- Q.front = Q.rear = (Qnodep)malloc(sizeof(Qnode));
- if(!Q.front) exit(-1);//注意程序的严谨性!
- Q.front->next = Q.rear->next = NULL;
- }
- void GetHead(LinkQueue Q, char &ch)//返回队头元素(不为空时)
- {
- if(Q.front == Q.rear)
- {
- printf("队列为空!");
- exit(-1);
- }
- else
- {
- ch = Q.front->data;
- }
- }
- void EnQueue(LinkQueue &Q, char ch)//插入元素,生成新的队尾
- {
- Q.rear->next = (Qnodep)malloc(sizeof(Qnode));
- if(!Q.rear->next) exit(-1);//分配空间记得检查!
- Q.rear = Q.rear->next;
- Q.rear->data = ch;
- Q.rear->next = NULL;
- }
- void DeQueue(LinkQueue &Q, char &ch)//删除一个对头的元素,生成新的对头
- {
- if(Q.front == Q.rear)
- {
- printf("队列为空!");
- exit(-1);
- }
- //注意队列的头指针是空的
- ch = Q.front->next->data;
- if(Q.front->next == Q.rear)
- {
- Q.rear = NULL;//队列为空!
- }
- Q.front = Q.front->next = NULL;
- }
- int isempty(LinkQueue Q)
- {
- if(Q.front == Q.rear) return 1;
- else return 0;
- }
-
- int main()
- {
- InitQueue(Q);
- char ch;
- printf("请输入一串字符,以#结尾\n");
- scanf("%c", &ch);
- while(ch != '#')
- {
- EnQueue(Q, ch);
- scanf("%c", &ch);
- }
- while(!isempty(Q))
- {
- DeQueue(Q, ch);
- printf("%c", ch);
- }
- return 0;
- }
复制代码
本帖最后由 xieglt 于 2021-1-6 20:43 编辑
|
|