问题描述:我在VS 2012中进行队列的练习(C Primer plus第6版 P594-P598)时,按照书上的示例将程序敲入电脑后运行,系统提示这三个报错eorr LNK1120, 2001, 2019. 具体报错信息请见截图。 尝试了很多论坛中的方法,但是都不能解决。
报错截图:
代码段如下:
queue.h文件中的代码:
/* P593-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;
/* Operation: Initialize Queue */
/* Precondition: pq ponit to a queue */
/* Postcondition: The queue will be set to NULL */
void InitializeQueue(Queue * pq);
/* Operation: Confirm whether the queue is full */
/* Precondition: pq point to an initialized queue */
/* Postcondition: If the queue is full, return 1, otherwise return 0 */
int QueueIsFull(const Queue * pq);
/* Operation: Confirm whether the queue is empty */
/* Precondition: pq point to an initialized queue */
/* Postcondition: If the queue is empty, return 1, otherwise return 0 */
int QueueIsEmpty(const Queue * pq);
/* Operation: Count the number of the queue */
/* Precondtion: pq point to an initialized queue */
/* Postcondition: Return the number of items in the queue */
int QueueItemCount(const Queue * pq);
/* Operation: Add an item to the end of a queue */
/* Precondition: pq point to an initialized queue */
/* item is to be added to the end of a queue */
/* Postcondition: If the queue is not empty, item will be added to the end of the queue */
/* if success, return 1, otherwise, return 0 with the current queue unchanged. */
int EnQueue(Item item, Queue * pq);
/* Operation: Delete the item from the front of the queue */
/* Precondition: pq point to an initialized queue */
/* Postcondition: If the queue is not empty, the front of the queue will be copied to *pitem */
/* and it will be deleted, meanwhile the function return 1 */
/* If this operation makes queue to be empty, Initialize the queue */
/* If the queue is empty before operation, the function return 0 */
int DeQueue(Item *pitem, Queue * pq);
/* Operation: Clear the queue */
/* Precondition: pq point to an initialized queue */
/* Postcondition: The queue will be cleared */
void EmptyQueue(Queue * pq);
#endif
queue.c中的代码:
/* P596-queue.c */
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
/* regional function */
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 QueIsFull(const Queue * pq)
{
int res;
if (pq->items == MAXQUEUE)
res = 1;
else
res = 0;
return res;
}
int QueueIsEmpty(const Queue * pq)
{
int res;
if (pq->items == 0)
res = 1;
else
res = 0;
return res;
}
int QueueItemCount(const Queue * pq)
{
return pq->items;
}
int EnQueue(Item item, Queue * pq)
{
Node * pnew;
int res;
if (QueueIsFull(pq) == 1)
res = 0;
else
{
res = 1;
pnew = (Node *)malloc(sizeof(Node));
if (pnew == NULL)
{
fprintf(stderr, "Unable to allocate memory!\n");
exit(EXIT_FAILURE);
}
CopyToNode(item, pnew);
pnew->next = NULL;
if (QueueIsEmpty(pq) == 1)
pq->front = pnew;
else
pq->rear->next = pnew;
pq->rear = pnew;
pq->items++;
}
return res;
}
int DeQueue(Item * pitem, Queue * pq)
{
Node * pt;
int res;
if(QueueIsEmpty(pq) ==1 )
res = 0;
else
{
res = 1;
CopyToItem(pq->front, pitem);
pt = pq->front;
pq->front = pq->front->next;
free(pt);
pq->items--;
if (pq->items == 0)
pq->rear = NULL;
}
return res;
}
void EmptyQueue(Queue * pq)
{
Item dummy;
while (!QueueIsEmpty(pq))
DeQueue(&dummy, pq);
}
static void CopyToNode(Item item, Node * pn)
{
pn->item = item;
}
static void CopyToItem(Node * pn, Item * pi)
{
*pi = pn->item;
}
use_q.c中的代码:
/* P598 use_q.c */
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int main (void)
{
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) == 0)
{
printf("Putting %d into queue.\n", temp);
EnQueue(temp, &line);
}
else
{
puts("Queue is full!");
}
}
else
{
if (QueueIsEmpty(&line) == 1)
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:");
}
EmptyQueue(&line);
puts("Bye!");
return 0;
}