首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

运行时总出现#exe文件出现异常,求大神解决程序有关问题

2012-11-05 
运行时总出现###.exe文件出现错误,求大神解决程序问题头文件queue.h#ifndef _QUEUE_H_#define _QUEUE_H_ty

运行时总出现###.exe文件出现错误,求大神解决程序问题
头文件queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
typedef int Item;
#define MAXQUEUE 10
typedef struct node
{
Item item;
struct node *next;
}Node;

typedef struct queue
{
Node *front;
Node *rear;
int items;
}Queue;
void InitializeQueue(Queue *pq);
int QueueIsFull(const Queue *pq);
int QueueIsEmpty(const Queue *pq);
int QueueItemCount(const Queue *pq);
int EnQueue(Item item,Queue *pq);
int DeQueue(Item *pitem,Queue *pq);
void EmptyTheQueue(Queue *pq);
#endif
接口实现:

#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
static void CopyToNode(Item item,Node *pn);
static void CopyToItem(Node *pn,Item *pi);
void InitializeQueue(Queue *pq)
{
pq->front=pq->rear=NULL;
pq->items=0;
}
int QueueIsFull(const Queue *pq)
{
return pq->items==MAXQUEUE;
}
int QueueIsEmpty(const Queue *pq)
{
return pq->items==0;
}
int QueueItemCount(const Queue *pq)
{
return pq->items;
}
int EnQueue(Item item,Queue *pq)
{
Node *pnew;
if(QueueIsFull(pq))
return 0;
pnew=(Node *)malloc(sizeof(Node));
if(pnew=NULL)
{
fprintf(stderr,"Unable to allocate memory!\n");
exit(1);
}
else
{

CopyToNode(item,pnew);
pnew->next=NULL;
if(QueueIsEmpty(pq))
pq->front=pnew;
else
pq->rear->next=pnew;
pq->rear=pnew;
pq->items++;
return 1;
}
}
int DeQueue(Item *pitem,Queue *pq)
{
Node *pt;
if(QueueIsEmpty(pq))
return 0;
CopyToItem(pq->front,pitem);
pt=pq->front;
pq->front=pq->front->next;
free(pt);
pq->items--;
if(pq->items==0)
pq->rear=NULL;
return 1;
}
void EmptyTheQueue(Queue *pq)
{
Item dummy;
while(!QueueIsEmpty)
DeQueue(&dummy,pq);
}
static void CopyToNode(Item item,Node *pn)
{
pn->item=item;
}
static void CopyToItem(Node *pn,Item *pi)
{
*pi=pn->item;
}
测试程序:
#include <stdio.h>
#include "queue.h"
int main()
{
Queue line;
Item temp;
char ch;
InitializeQueue(&line);
puts("Testing the Queue interface.Type a to add a value,");
puts("type d to delete a value,and type q to quit. ");
while((ch=getchar())!='q')
{
if(ch!='a'&&ch!='d')
continue;
if(ch=='a')
{
printf("Integer to add: ");
scanf("%d",&temp);
if(!QueueIsFull(&line))
{
printf("putting %d into queue.\n",temp);
EnQueue(temp,&line);
}
else
puts("Queue is full.");
}
else
{
if(QueueIsEmpty(&line))
puts("Nothing to delete!");
else
{
DeQueue(&temp,&line);
printf("Removing %d from queue.\n",temp);
}
}
printf("%d items in queue\n", QueueItemCount(&line));
puts("Type a to add,d to delete,q to quit: ");
}
EmptyTheQueue(&line);
puts("Bye!");
return 0;
}

[解决办法]

探讨

int EnQueue(Item item,Queue *pq)
{
Node *pnew;
if(QueueIsFull(pq))
return 0;
pnew=(Node *)malloc(sizeof(Node));
if(pnew=NULL)
{
fprintf(stderr,"Unable to allocate memory!\n");
exit(1);
}
e……

热点排行