Uploaded by Ahmed Walid

assignment4

advertisement
///////global.h///////////
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
typedef struct customer{
char Name[15];
int Id;
}Customer;
#endif // GLOBAL_H_INCLUDED
///////Queue.c/////////////
#include "Queue.h"
#include <stdlib.h>
void CreateQueue(Queue *pq){
pq->front=NULL;
pq->rear=NULL;
pq->size=0;
}
void Append(Customer c, Queue* pq){
QueueNode*pn=(QueueNode*)malloc(sizeof(QueueNode));
pn->next=NULL;
pn->C=c;
if (!pq->rear)
pq->front=pn;
else
pq->rear->next=pn;//run time error for empty queue
pq->rear=pn;
pq->size++;
}
void Serve(Customer *pc, Queue* pq){
QueueNode *pn=pq->front;
*pc=pn->C;
pq->front=pn->next;
free(pn);
if (!pq->front)
pq->rear=NULL;
pq->size--;
}
void ClearQueue(Queue* pq){
while(pq->front){
pq->rear=pq->front->next;
free(pq->front);
pq->front=pq->rear;
}
pq->size = 0;
}
int QueueEmpty(Queue* pq){
return !pq->front;
}
void TraverseQueue(Queue* pq, void(*pf)(Customer)){
for(QueueNode *pn=pq->front; pn; pn=pn->next)
(*pf)(pn->C);
}
//////////Queue.h///////////
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
#include "global.h"
typedef struct queuenode{
Customer C;
struct queuenode *next;
}QueueNode;
typedef struct queue{
QueueNode *front;
QueueNode *rear;
int size;
}Queue;
void CreateQueue(Queue *);
void Append(Customer , Queue* );
void Serve(Customer *, Queue* );
void ClearQueue(Queue* );
void TraverseQueue(Queue*, void(*)(Customer));
int QueueEmpty(Queue* );
#endif // QUEUE_H_INCLUDED
////////////Stack.c/////////////
#include "Stack.h"
#include <stdlib.h>
void CreateStack(Stack *ps){
ps->top=NULL;
}
void Push(Customer c ,Stack *ps){
StackNode* pn=(StackNode*)malloc(sizeof(StackNode));
pn->C=c;
pn->next=ps->top;
ps->top=pn;
}
void Pop (Customer *pc, Stack *ps ){
StackNode *pn=ps->top;
*pc=pn->C;
ps->top=pn->next;
free(pn);
}
int StackEmpty(Stack *ps){
return ps->top==NULL;
}
///////////Stack.h////////////
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include "global.h"
typedef struct stackNode{
Customer C;
struct stackNode *next;
}StackNode;
typedef struct stack{
StackNode *top;
}Stack;
void CreateStack(Stack *);
void Push(Customer ,Stack *);
void Pop (Customer *, Stack * );
int StackEmpty(Stack *);
#endif // STACK_H_INCLUDED
/////////////main.c/////////////
#include <stdio.h>
#include <stdlib.h>
#include "global.h"
#include "Queue.h"
#include "Stack.h"
#include <stdio.h>
void displayMenu() {
printf("\nMain Menu:\n");
printf("1. Add a New Customer\n");
printf("2. Serve a Customer\n");
printf("3. Display Customers Information\n");
printf("4. Display Customers information in the 'most-recent' Order\n");
printf("5. Exit menu\n");
}
void displayCustomers(Customer c){
printf("\n Name : %s Id : %d",c.Name ,c.Id);
}
Queue reverseQueue(Queue *q, Queue *reversedQ) {
Stack s;
CreateStack(&s);
Customer c;
Queue tempQ;
CreateQueue(&tempQ);
while (!QueueEmpty(q)) {
Serve(&c, q);
Push(c, &s);
Append(c, &tempQ);
}
while (!StackEmpty(&s)) {
Pop(&c, &s);
Append(c, reversedQ);
}
while (!QueueEmpty(&tempQ)) {
Serve(&c, &tempQ);
Append(c, q);
}
return *reversedQ;
}
int main() {
int choice;
Queue q;
CreateQueue(&q);
Queue revQueue;
CreateQueue(&revQueue);
do {
displayMenu();
printf("\n Enter your choice: \n");
scanf("%d", &choice);
Customer c;
switch (choice) {
case 1:
printf("Add a Customer Id :\n");
scanf("%d", &c.Id);
printf("Add a Customer Name :\n");
scanf("%s", c.Name);
Append(c,&q);
printf("\n Customer %s added successfully \n", c.Name);
break;
case 2:
Serve(&c,&q);
printf("\n the customer %s served successfully \n",c.Name);
break;
case 3:
printf(" Customers Information :\n");
TraverseQueue(&q,&displayCustomers);
break;
case 4:
printf("Display Customers information in the 'most-recent' Order\n");
revQueue =reverseQueue(&q,&revQueue);
TraverseQueue(&revQueue,&displayCustomers);
break;
case 5:
printf("Exiting menu\n");
break;
default:
printf("Invalid choice. Please enter a number between 1 and 5.\n");
}
} while (choice != 5);
return 0;
}
Download