#include<stdio.h>
#include<stdlib.h>
typedef struct QNode{
char data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
LinkQueue *q;
//初始化
initqueue (LinkQueue &q){
q.front=q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!q.front) return 0;
q.front->next=NULL;
return 1;
}
//入队
enQueue(LinkQueue &q,char e){
QNode *p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return 0;
p->data=e;
p->next=NULL;
q.rear->next=p;
q.rear=p;
return 1;
}
//出队
deQueue(LinkQueue &q,char &e){
if(q.front==q.rear)
return 0;
QNode *p=q.front->next;
e=p->data;
q.front->next=p->next;
if(q.rear==p)
q.rear=q.front;
return 1;
}
void clearQueue(LinkQueue *q){
QueuePtr p,temp;
if(q!=NULL){
if(q->front!=NULL){
p=q->front->next;
while(p!=NULL){//释放队列空间
temp=p->next;
free(p);
p=temp;
}
q->front->next=NULL;
q->rear=q->front;
}
}
}
main(){
initqueue(*q);
#include<stdlib.h>
typedef struct QNode{
char data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
LinkQueue *q;
//初始化
initqueue (LinkQueue &q){
q.front=q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!q.front) return 0;
q.front->next=NULL;
return 1;
}
//入队
enQueue(LinkQueue &q,char e){
QNode *p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return 0;
p->data=e;
p->next=NULL;
q.rear->next=p;
q.rear=p;
return 1;
}
//出队
deQueue(LinkQueue &q,char &e){
if(q.front==q.rear)
return 0;
QNode *p=q.front->next;
e=p->data;
q.front->next=p->next;
if(q.rear==p)
q.rear=q.front;
return 1;
}
void clearQueue(LinkQueue *q){
QueuePtr p,temp;
if(q!=NULL){
if(q->front!=NULL){
p=q->front->next;
while(p!=NULL){//释放队列空间
temp=p->next;
free(p);
p=temp;
}
q->front->next=NULL;
q->rear=q->front;
}
}
}
main(){
initqueue(*q);
enQueue(*q,”I”);
enQueue(*q,”l”);
enQueue(*q,”o”);
enQueue(*q,”v”);
enQueue(*q,”e”);
enQueue(*q,”y”);
enQueue(*q,”o”);
enQueue(*q,”u”);
deQueue(*q,”I”);
deQueue(*q,”l”);
deQueue(*q,”o”);
void clearQueue(LinkQueue *q);
return 0;
}
解决方案
30
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct QNode{
char data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue,*LinkQueueP; //定义了结构体指针
//初始化
int initqueue(LinkQueueP &q){
q = (LinkQueueP)malloc(sizeof(QNode)); //分配内存空间
q->front = (QueuePtr)malloc(sizeof(QNode));
q->rear = q->front;
if (!q->front) return 0;
q->front->next = NULL;
return 1;
}
//入队
int enQueue(LinkQueueP q, char e){
QNode *p = (QueuePtr)malloc(sizeof(QNode));
if (!p) return 0;
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
return 1;
}
//出队
int deQueue(LinkQueueP q, char &e){
if (q->front == q->rear)
return 0;
QNode *p = q->front->next;
e = p->data;
q->front->next = p->next;
if (q->rear == p)
q->rear = q->front; //队列已空
return 1;
}
int clearQueue(LinkQueueP q){ //清空队列
QueuePtr p, temp;
if (q != NULL){
if (q->front != NULL){
p = q->front->next;
while (p != NULL){//释放队列空间
temp = p->next;
free(p);
p = temp;
}
q->front->next = NULL;
q->rear = q->front;
}
}
return 0;
}
LinkQueueP q;
int main(){
initqueue(q);
enQueue(q, “I”);
enQueue(q, “l”);
enQueue(q, “o”);
enQueue(q, “v”);
enQueue(q, “e”);
enQueue(q, “y”);
enQueue(q, “o”);
enQueue(q, “u”);
char ch;
deQueue(q, ch); //假如是引用的话,一定要是变量
printf(“%c\n”, ch);
deQueue(q, ch);
printf(“%c\n”, ch);
deQueue(q, ch);
printf(“%c\n”, ch);
clearQueue(q);
return 0;
}
#include<stdlib.h>
#include<malloc.h>
typedef struct QNode{
char data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue,*LinkQueueP; //定义了结构体指针
//初始化
int initqueue(LinkQueueP &q){
q = (LinkQueueP)malloc(sizeof(QNode)); //分配内存空间
q->front = (QueuePtr)malloc(sizeof(QNode));
q->rear = q->front;
if (!q->front) return 0;
q->front->next = NULL;
return 1;
}
//入队
int enQueue(LinkQueueP q, char e){
QNode *p = (QueuePtr)malloc(sizeof(QNode));
if (!p) return 0;
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
return 1;
}
//出队
int deQueue(LinkQueueP q, char &e){
if (q->front == q->rear)
return 0;
QNode *p = q->front->next;
e = p->data;
q->front->next = p->next;
if (q->rear == p)
q->rear = q->front; //队列已空
return 1;
}
int clearQueue(LinkQueueP q){ //清空队列
QueuePtr p, temp;
if (q != NULL){
if (q->front != NULL){
p = q->front->next;
while (p != NULL){//释放队列空间
temp = p->next;
free(p);
p = temp;
}
q->front->next = NULL;
q->rear = q->front;
}
}
return 0;
}
LinkQueueP q;
int main(){
initqueue(q);
enQueue(q, “I”);
enQueue(q, “l”);
enQueue(q, “o”);
enQueue(q, “v”);
enQueue(q, “e”);
enQueue(q, “y”);
enQueue(q, “o”);
enQueue(q, “u”);
char ch;
deQueue(q, ch); //假如是引用的话,一定要是变量
printf(“%c\n”, ch);
deQueue(q, ch);
printf(“%c\n”, ch);
deQueue(q, ch);
printf(“%c\n”, ch);
clearQueue(q);
return 0;
}
20
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。