include include define FALSE 0 define TRUE 1 A Node declarat
#include
#include
#define FALSE 0
#define TRUE 1
/*A Node declaration to store a value and a link*/
struct Node
{
int val;
struct Node *next;
};
struct QueueRecord
{
struct Node *front; /* pointer to front of queue */
struct Node *rear; /* pointer to rear of queue */
int size; /* number of items in queue */
};
typedef struct QueueRecord *Queue;
Queue CreateQueue(int);
void MakeEmptyQueue(Queue);
int QueueSize(Queue);
void Enqueue(int, Queue);
int Dequeue(Queue);
int FrontOfQueue(Queue);
int RearOfQueue(Queue);
int IsEmptyQueue(Queue);
void DisplayQueue(Queue);
int main()
{
}
/*This function initialises the queue*/
Queue CreateQueue(int maxElements)
{
Queue q;
q=(struct QueueRecord*)malloc(sizeof(struct QueueRecord));
if(q==NULL){
printf(\"Out of memory\");
return 0;}
MakeEmptyQueue(q);
return q;
}
/*This function sets the queue size to 0, and creates a dummy element
and sets the front and rear point to this dummy element*/
void MakeEmptyQueue(Queue q)
{
struct Node *dummy;
dummy= (struct Node *) malloc(sizeof(struct Node));
q->front=dummy;
q->size=0;
q->front->next=NULL;
q->rear=q->front;
}
/*Shows if the queue is empty*/
int IsEmptyQueue(Queue q)
{
if(!q->size==0)
return 1;
else
return 0;
}
/*Returns the queue size*/
int QueueSize(Queue q)
{
return q->size;
}
/*Enqueue - Adds a new element to the queue, simly creates a node and
adds it to the rear of the queue*/
void Enqueue(int x, Queue q)
{
struct Node *newnode;
newnode= (struct Node *) malloc(sizeof(struct Node));
newnode->next=NULL;
newnode->val=x;
q->rear->next=newnode;
q->rear=newnode;
q->size+1;
}
/*Dequeue - Removes a node from the queue, basically removes a node from
the front of the queue*/
int Dequeue(Queue q)
{
int val;
struct Node *delnode;
delnode=(struct Node *) malloc(sizeof(struct Node));
delnode=q->front->next;
q->front->next=q->front->next->next;
val=delnode->val;
free(delnode);
q->size-1;
return val;
}
/*Returns the value stored in the front of the queue*/
int FrontOfQueue(Queue q)
{
if (!IsEmptyQueue(q))
return q->front->next->val;
else
{
printf(\"The queue is empty\ \");
return -1;
}
}
/*Returns the value stored in the rear of the queue*/
int RearOfQueue(Queue q)
{
if (!IsEmptyQueue(q))
return q->rear->val;
else
{
printf(\"The queue is empty\ \");
return -1;
}
}
/*Displays the content of the queue*/
void DisplayQueue(Queue q)
{
struct Node *print;
print = q->front->next;
while (print != NULL)
{
printf(\"--> %d \", print->val);
print = print->next;
}
printf(\"\ \");
}
I need simple as possible not to complicated
-Write a function called “orderElement()“ that continuously asks user to enter an integer value and stores them in the queue, and once the user enters -1 this displays the list of the entered integer numbers in the order they are entered.
-Using only the queue functions (e.g., enqueue, dequeue, etc.), write a function called “copyQueue()” that copies the content of one queue to another.
-Modify the function you created in the previous step such that “copyQueue()” function copies the content of one queue to another and the original queue content will still be the same after the copying operation.
-Write a function called “deleteNegatives()” that only uses queue functions and deletes the negative values in a queue. The queue content will be the same but the content will not have negative values.
-Modify your queue such that it stores the name, salary and id of an employee. You will then need to update all the relevant functions to enqueue and dequeuer an employee. Write a function called “employeeOrder()” that asks the user to continuously enter an employee until they enter an employee with id “-1”. When they do that, this function will display the details of the employees in the entered order.
Solution
Program: Queue.c
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
/*A Node declaration to store a value and a link*/
struct Node
{
int val;
struct Node *next;
};
struct QueueRecord
{
struct Node *front; /* pointer to front of queue */
struct Node *rear; /* pointer to rear of queue */
int size; /* number of items in queue */
};
typedef struct QueueRecord *Queue;
Queue CreateQueue(int);
void MakeEmptyQueue(Queue);
int QueueSize(Queue);
void Enqueue(int, Queue);
int Dequeue(Queue);
int FrontOfQueue(Queue);
int RearOfQueue(Queue);
int IsEmptyQueue(Queue);
void DisplayQueue(Queue);
void orderElement(Queue);
Queue copyQueue(Queue);
void deleteNegatives(Queue);
/*This function initialises the queue*/
Queue CreateQueue(int maxElements)
{
Queue q;
q=(struct QueueRecord*)malloc(sizeof(struct QueueRecord));
if(q==NULL){
printf(\"Out of memory\");
return 0;}
MakeEmptyQueue(q);
return q;
}
/*This function sets the queue size to 0, and creates a dummy element
and sets the front and rear point to this dummy element*/
void MakeEmptyQueue(Queue q)
{
struct Node *dummy;
dummy= (struct Node *) malloc(sizeof(struct Node));
q->front=dummy;
q->size=0;
q->front->next=NULL;
q->rear=q->front;
}
/*Shows if the queue is empty*/
int IsEmptyQueue(Queue q)
{
if(!q->size==0)
return 1;
else
return 0;
}
/*Returns the queue size*/
int QueueSize(Queue q)
{
return q->size;
}
/*Enqueue - Adds a new element to the queue, simly creates a node and
adds it to the rear of the queue*/
void Enqueue(int x, Queue q)
{
struct Node *newnode;
newnode= (struct Node *) malloc(sizeof(struct Node));
newnode->next=NULL;
newnode->val=x;
q->rear->next=newnode;
q->rear=newnode;
q->size+=1;
}
/*Dequeue - Removes a node from the queue, basically removes a node from
the front of the queue*/
int Dequeue(Queue q)
{
int val;
struct Node *delnode;
delnode=(struct Node *) malloc(sizeof(struct Node));
delnode=q->front->next;
q->front->next=q->front->next->next;
val=delnode->val;
free(delnode);
q->size-=1;
return val;
}
/*Returns the value stored in the front of the queue*/
int FrontOfQueue(Queue q)
{
if (!IsEmptyQueue(q))
return q->front->next->val;
else
{
printf(\"The queue is empty\ \");
return -1;
}
}
/*Returns the value stored in the rear of the queue*/
int RearOfQueue(Queue q)
{
if (!IsEmptyQueue(q))
return q->rear->val;
else
{
printf(\"The queue is empty\ \");
return -1;
}
}
/*Displays the content of the queue*/
void DisplayQueue(Queue q)
{
struct Node *print;
print = q->front->next;
while (print != NULL)
{
printf(\"--> %d \", print->val);
print = print->next;
}
printf(\"\ \");
}
/* Problem #1 */
/* continuously asks user to enter an integer value and stores them in the queue,
and once the user enters -1 this displays the list of the entered integer numbers
in the order they are entered. */
void orderElement(Queue q){
printf(\"Enter value. Enter -1 to stop \ \");
int val;
scanf(\"%d\",&val);
while(val!=-1){
Enqueue(val,q);
scanf(\"%d\",&val);
}
DisplayQueue(q);
}
/* Problem #2 & Problem #3*/
/* -Using only the queue functions (e.g., enqueue, dequeue, etc.),
write a function called “copyQueue()” that copies the content of one queue to another.
-Modify the function you created in the previous step such that “copyQueue()” function copies
the content of one queue to another and the original queue content will still be the same
after the copying operation. */
/* Just uncomment MakeEmptyQueue for Problem #2 */
Queue copyQueue(Queue q){
Queue copy = CreateQueue(10);
struct Node *node;
node = q->front->next;
while (node != NULL)
{
Enqueue(node->val,copy);
node = node->next;
}
//MakeEmptyQueue(q);
return copy;
}
/* Problem 4 */
/* Write a function called “deleteNegatives()”
that only uses queue functions and deletes the negative values in a queue.
The queue content will be the same but the content will not have negative values. */
void deleteNegatives(Queue q){
struct Node *node,*prevNode;
node = q->front->next;
prevNode = q->front;
while(node != NULL){
if(node->val<0){
prevNode->next = node->next;
free(node);
node=prevNode->next->next;
q->size-=1;
}else{
node=node->next;
prevNode = prevNode->next;
}
}
}
int main(){
Queue q = CreateQueue(10);
orderElement(q);
Queue copy = copyQueue(q);
DisplayQueue(q);
DisplayQueue(copy);
deleteNegatives(copy);
DisplayQueue(copy);
return 0;
}
Program: EmployeeQueue.c
/* Problem #5 - Modify your queue such that it stores the name, salary and id of an employee.
You will then need to update all the relevant functions to enqueue and dequeuer an employee.
Write a function called “employeeOrder()” that asks the user to continuously
enter an employee until they enter an employee with id “-1”. When they do that,
this function will display the details of the employees in the entered order.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
/*A Node declaration to store a value and a link*/
struct Node
{
int employeeId;
char name[30];
int salary;
struct Node *next;
};
struct QueueRecord
{
struct Node *front; /* pointer to front of queue */
struct Node *rear; /* pointer to rear of queue */
int size; /* number of items in queue */
};
typedef struct QueueRecord *Queue;
Queue CreateQueue(int);
void MakeEmptyQueue(Queue);
int QueueSize(Queue);
void Enqueue(char[],int,int, Queue);
int Dequeue(Queue);
int FrontOfQueue(Queue);
int RearOfQueue(Queue);
int IsEmptyQueue(Queue);
void DisplayQueue(Queue);
void employeeOrder();
int main(){
employeeOrder();
return 0;
}
/*This function initialises the queue*/
Queue CreateQueue(int maxElements)
{
Queue q;
q=(struct QueueRecord*)malloc(sizeof(struct QueueRecord));
if(q==NULL){
printf(\"Out of memory\");
return 0;}
MakeEmptyQueue(q);
return q;
}
/*This function sets the queue size to 0, and creates a dummy element
and sets the front and rear point to this dummy element*/
void MakeEmptyQueue(Queue q)
{
struct Node *dummy;
dummy= (struct Node *) malloc(sizeof(struct Node));
q->front=dummy;
q->size=0;
q->front->next=NULL;
q->rear=q->front;
}
/*Shows if the queue is empty*/
int IsEmptyQueue(Queue q)
{
if(!q->size==0)
return 1;
else
return 0;
}
/*Returns the queue size*/
int QueueSize(Queue q)
{
return q->size;
}
/*Enqueue - Adds a new element to the queue, simly creates a node and
adds it to the rear of the queue*/
void Enqueue(char name[30], int salary, int employeeId, Queue q)
{
struct Node *newnode;
newnode= (struct Node *) malloc(sizeof(struct Node));
newnode->next=NULL;
strcpy(newnode->name,name);
newnode->salary=salary;
newnode->employeeId=employeeId;
q->rear->next=newnode;
q->rear=newnode;
q->size+=1;
}
/*Dequeue - Removes a node from the queue, basically removes a node from
the front of the queue*/
int Dequeue(Queue q)
{
int employeeId;
struct Node *delnode;
delnode=(struct Node *) malloc(sizeof(struct Node));
delnode=q->front->next;
q->front->next=q->front->next->next;
employeeId=delnode->employeeId;
free(delnode);
q->size-=1;
return employeeId;
}
/*Returns the value stored in the front of the queue*/
int FrontOfQueue(Queue q)
{
if (!IsEmptyQueue(q))
return q->front->next->employeeId;
else
{
printf(\"The queue is empty\ \");
return -1;
}
}
/*Returns the value stored in the rear of the queue*/
int RearOfQueue(Queue q)
{
if (!IsEmptyQueue(q))
return q->rear->employeeId;
else
{
printf(\"The queue is empty\ \");
return -1;
}
}
/*Displays the content of the queue*/
void DisplayQueue(Queue q)
{
struct Node *print;
print = q->front->next;
while (print != NULL)
{
printf(\"--> %d, %s, %d \ \", print->employeeId,print->name,print->salary);
print = print->next;
}
printf(\"\ \");
}
void employeeOrder(){
Queue q = CreateQueue(10);
char name[30];
int employeeId, salary;
printf(\"Enter Employee Information. Enter employee id -1 to exit \ \");
while(1){
scanf(\"%d\",&employeeId);
if(employeeId==-1){
break;
}
scanf(\"%s\",&name);
scanf(\"%d\",&salary);
Enqueue(name,salary,employeeId,q);
}
printf(\"%d\",QueueSize(q));
DisplayQueue(q);
}








