//linkqueue.h
#include "stdio.h" #include "stdlib.h" #include <string.h> #define MAXSZIE 30 struct qNode { char name[MAXSZIE]; char author[MAXSZIE]; int Pages; double Price; char IsForeign; struct qNode * next; }; typedef struct qNode QNode,*pNode; typedef struct { pNode front; pNode rear; int size; }LinkQueue; void InitQueue(LinkQueue *LQ); //1.Init queue int QueueEmpty(LinkQueue LQ); //1.if queue is nut empty return 1; //2.if queue is empty return 0; int enqueue( LinkQueue *LQ,QNode item) ; //1.Check that the queue and the item are not empty. Return -1 if they are. //2.Create a new Record to save the data received from item. This Record will be added to the queue. //3.Find the end of the queue. Be mindful that the queue may be empty. //4.Return the value of a counter with the new size of the queue. int dequeue(LinkQueue *LQ, QNode *item); //1.Check if the queue is empty, if so the program will print “Nothing to dequeue!” and return -1. //2.If the queue is not empty, copy the data of the front element into the Record item and remove the front element from the queue. //3. free its memory. Return 1 when finished. int GetHead(LinkQueue LQ,QNode item); //1.If the queue is not empty,return the head item name; void destroyQueue(LinkQueue * LQ) ; //1.The method will dequeue and free the memory of all the elements in the queue. void DislayQueue(LinkQueue LQ); //1.printf all the elements in the queue.
//linkqueue.c
#include "QueueADT.h" void InitQueue(LinkQueue *LQ) { LQ->front=(pNode)malloc(sizeof(QNode)); if(LQ->front==NULL) exit(-1); LQ->front->next=NULL; LQ->rear=LQ->front; LQ->size=0; } int QueueEmpty( LinkQueue LQ ) { if(LQ.rear->next==NULL) return 1; else return 0; } int enqueue( LinkQueue *LQ,QNode item ) { QNode *s=(QNode *)malloc(sizeof(QNode)); if(!s) exit(-1); strcpy(s->author,item.author); s->IsForeign=item.IsForeign; strcpy(s->name,item.name); s->Pages=item.Pages; s->Price=item.Price; s->next=NULL; LQ->rear->next=s; LQ->rear=s; LQ->size++; return LQ->size; } int dequeue( LinkQueue *LQ, QNode *item ) { QNode *s; if(LQ->front==LQ->rear) return -1; else{ s=LQ->front->next; strcpy(item->author,s->author); item->IsForeign=s->IsForeign; strcpy(item->name,s->name); item->Pages=s->Pages; item->Price=s->Price; LQ->front->next=s->next; if(LQ->rear==s)LQ->rear=LQ->front; free(s); return 1; } } int GetHead( LinkQueue LQ,QNode item ) { QNode *s; if(LQ.rear==LQ.front) return 0; else { s=LQ.front->next; strcpy(item.author,s->author); item.IsForeign=s->IsForeign; strcpy(item.name,s->name); item.Pages=s->Pages; item.Price=s->Price; return 1; } } void destroyQueue( LinkQueue * LQ ) { QNode *s; if(LQ->front!=LQ->rear) { s=LQ->front->next; LQ->front->next=s->next; if(LQ->rear==s)LQ->rear=LQ->front; free(s); destroyQueue(LQ); }else { LQ->rear->next=LQ->front; } } void DislayQueue( LinkQueue LQ ) { QNode * p; int i=1; printf(" ID NAME AUTHOR PAGES PRICE ISFOREIGN\n"); p=LQ.front->next; while(p&&QueueEmpty(LQ)==1) { printf("[%2d]:%30s %25s%5d %10.2f %10c\n", i, p->name,p->author ,p->Pages, p->Price,p->IsForeign); i++; p=p->next; } printf("\nSIZE = %d\n",i-1); printf("************************************************************************************************\n"); }
main.c
//gt Final Grade 100/100 //gt Program compiles and runs #include "QueueADT.h" /* author:zhaoke date:2014.12.4 project:QueueADT. */ void main() { int i=0; QNode p; LinkQueue LQ; /////////////////////////////////////////////////////////////////////// InitQueue(&LQ); // add item to Qrere strcpy(p.name,"Romance of the Three Kingdoms"); strcpy(p.author,"LuoGuanZhong"); p.Price=50.5; p.Pages=365; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"Water Margin"); strcpy(p.author,"ShiNaiAn"); p.Price=80.5; p.Pages=465; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"A Dream of Red Mansions"); strcpy(p.author,"CaoXueQin"); p.Price=46.5; p.Pages=532; p.IsForeign='N'; enqueue(&LQ,p);; strcpy(p.name,"Jane Eyre"); strcpy(p.author,"Charlotte Bronte"); p.Price=66.5; p.Pages=432; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"the making of steel"); strcpy(p.author,"Pavel Korchagin"); p.Price=85.5; p.Pages=432; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"Robinson Crusoe"); strcpy(p.author,"Daniel Defoe"); p.Price=85.5; p.Pages=672; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"The Merchant of Venice"); strcpy(p.author,"WilliamShakesbeare"); p.Price=95.5; p.Pages=572; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"The Tragedy of Hamlet"); strcpy(p.author,"WilliamShakesbeare"); p.Price=95.5; p.Pages=1072; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"Othello"); strcpy(p.author,"WilliamShakesbeare"); p.Price=95.5; p.Pages=882; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"The life of King Henry Ⅷ"); strcpy(p.author,"WilliamShakesbeare"); p.Price=105.5; p.Pages=682; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name," Les Misrables "); strcpy(p.author,"Victor Hugo"); p.Price=85.5; p.Pages=452; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"Notre-Dame de Paris"); strcpy(p.author,"Victor Hugo"); p.Price=85.5; p.Pages=752; p.IsForeign='Y'; enqueue(&LQ,p); strcpy(p.name,"Red Sorghum"); strcpy(p.author,"MoYan"); p.Price=85.5; p.Pages=642; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"Wa"); strcpy(p.author,"MoYan"); p.Price=23.5; p.Pages=452; p.IsForeign='N'; enqueue(&LQ,p);; strcpy(p.name,"Sadness into river upstream"); strcpy(p.author,"Jing M.Guo"); p.Price=52.5; p.Pages=552; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"Tiny Times"); strcpy(p.author,"Jing M.Guo"); p.Price=58.5; p.Pages=512; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name," Let the wind cutting dusts"); strcpy(p.author,"Jing M.Guo"); p.Price=28.5; p.Pages=532; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"Triple gate"); strcpy(p.author,"HanHan"); p.Price=58.5; p.Pages=132; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"Ordinary world"); strcpy(p.author,"LuYao"); p.Price=16.5; p.Pages=1324; p.IsForeign='N'; enqueue(&LQ,p); strcpy(p.name,"If you well is sunny"); strcpy(p.author,"BaiLuoMei"); p.Price=58.5; p.Pages=263; p.IsForeign='N'; enqueue(&LQ,p); //////////////////////////////////////////////////////////////////// printf("Creating data and pushing it to the Queue:\n"); DislayQueue(LQ); //////////////////////////////////////////////////////////////////// strcpy(p.name,"DateStruct"); strcpy(p.author,"YanWeiMin"); p.Price=60.3; p.Pages=280; p.IsForeign='N'; enqueue(&LQ,p); printf("Pushing new value on Queue:\n"); DislayQueue(LQ); //////////////////////////////////////////////////////////////////// printf("Delete 5 elements from the Queue:\n\n"); for(i=0;i<5;i++) { dequeue(&LQ, &p ); printf("ElementName[%d]:%s\n",i+1,p.name); } DislayQueue(LQ); ////////////////////////////////////////////////////////////////// if(QueueEmpty(LQ)!=1) { printf("Queue is empty\n"); } else { printf("Queue is NOT empty\n"); } DislayQueue(LQ); ////////////////////////////////////////////////////////////////// printf("Destroying the Queue\n"); destroyQueue(&LQ); if(QueueEmpty(LQ)!=1) { printf("Queue is empty\n"); } else { printf("Queue is NOT empty\n"); } DislayQueue(LQ); }
时间: 2024-10-28 21:22:15