中国刑警第一部全集:VC++源程序调试问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/04 04:30:18

我用的是VC.net(7)

#include "stdafx.h"

/*链式队的基本操作*/
#include <stdio.h>
#include <memory.h>
typedef char ElemType;
struct qnode
{
ElemType data;
struct qnode *next;
};
typedef struct queue
{
struct qnode *front;
struct qnode *rear;
} linkqueue;
void initqueue(linkqueue *&Q)
{
Q=(struct queue *) malloc (sizeof(struct queue));
Q-> front=Q-> rear=NULL;
}
void enter(linkqueue *Q,ElemType x)
{
qnode *s;
s=(struct qnode *)malloc(sizeof(struct qnode));
s-> data=x;
s-> next=NULL;
if (Q-> rear==NULL) /*若链队为空,则新结点是队首结点又是队尾结点*/
Q-> front=Q-> rear=s;
else
{
Q-> rear-> next=s; /*将s结点链到队尾,rear指向它*/
Q-> rear=s;
}
}
void Delete(linkqueue *Q)
{
qnode *t;
if (Q-> front==NULL)
printf("队列为空!\n");
else if (Q-> front==Q-> rear) /*只有一个结点时*/
{
t=Q-> front;
Q-> front=Q-> rear=NULL;
}
else
{
t=Q-> front;
Q-> front=Q-> front-> next;
}
free(t);
}

ElemType gethead(linkqueue *Q)
{
if (Q-> front==Q-> rear)
printf("队列为空!\n");
else
return(Q-> front-> data);
return (ElemType)NULL;
}
int empty(linkqueue *Q)
{
if (Q-> front==Q-> rear) return(1); /*为空,则返回true*/
else return(0); /*不为空,则返回flase*/
}
void display(linkqueue *Q)
{
qnode *p=Q-> front;
printf("队列元素:");
while (p!=NULL)
{
printf("%c ",p-> data);
p=p-> next;
}
printf("\n");
}
int mainx()
{
linkqueue *qu;
printf("初始化队列qu\n");
initqueue(qu);
printf("队列空:%d\n",empty(qu));
printf("依次入队a,b,c,d元素\n");
enter(qu,'a');
enter(qu,'b');
enter(qu,'c');
enter(qu,'d');
display(qu);
printf("出队一次\n");
Delete(qu);
printf("队首元素:%c\n",gethead(qu));
printf("出队一次\n");
Delete(qu);
display(qu);
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
return mainx();
}

结果:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp

D:\Documents and Settings\******>cd D:\

D:\>b
初始化队列qu
队列空:1
依次入队a,b,c,d元素
队列元素:a b c d
出队一次
队首元素:b
出队一次
队列元素:c d