I need code for the following program in c++. Mail & parcel delivery Systenm Con
ID: 3704812 • Letter: I
Question
I need code for the following program in c++.
Mail & parcel delivery Systenm Consider a Mail delivery system in which post man has to deliver mail and parcels in a locality Emails and parcel are stored in two stacks in an order, one stack for sorted parcel and other for sorted mails respectively. And posts have to deliver both on the basis of nearest location. After each delivery postman will check both stack for next pop, the stack having parcel/email in nearby location will be pop first.. if both thing have to deliver at same location then pop from both stack(order doesn't matter) For example both stacks have following destination detail Stack 1 having mails 1 4 13 20 30 40 Stack 2 for parcels 4 8 10 12 25 Delivery order according to provided data: Stack 1 1 Stack 2 > 2 Stack 2> 3 Stack 2 4 Stack 1 ? 4 Stack1> 5 Stack1 6 Stack1 7 Stack 2S Stack 2 10 And so on.Explanation / Answer
// C program for linked list implementation of stack
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// A structure to represent a stack
struct StackNode
{
int data;
struct StackNode* next;
};
struct StackNode* newNode(int data)
{
struct StackNode* stackNode =
(struct StackNode*) malloc(sizeof(struct StackNode));
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}
int isEmpty(struct StackNode *root)
{
return !root;
}
void push(struct StackNode** root, int data)
{
struct StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
// printf("%d pushed to stack ", data);
}
int pop(struct StackNode** root)
{
if (isEmpty(*root))
return INT_MIN;
struct StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);
return popped;
}
int peek(struct StackNode* root)
{
if (isEmpty(root))
return INT_MIN;
return root->data;
}
int main()
{
struct StackNode* stack1 = NULL;
struct StackNode* stack2 = NULL;
int x;
push(&stack1, 40);
push(&stack1, 30);
push(&stack1, 20);
push(&stack1, 13);
push(&stack1, 7);
push(&stack1, 6);
push(&stack1, 5);
push(&stack1, 4);
push(&stack1, 1);
push(&stack2, 7);
push(&stack2, 25);
push(&stack2, 12);
push(&stack2, 10);
push(&stack2, 8);
push(&stack2, 4);
push(&stack2, 3);
push(&stack2, 2);
while(isEmpty(stack1)==0 || isEmpty(stack2)==0)
{ x=peek(stack1);
while(isEmpty(stack1)==0&&x<peek(stack2))
{
printf("stack1->%d ",pop(&stack1));
x=peek(stack1);
}
x=peek(stack2);
while(isEmpty(stack2)==0 && x<=peek(stack1))
{
printf("stack2->%d ",pop(&stack2));
x=peek(stack2);
}
if(isEmpty(stack1)==1)
{
while(isEmpty(stack2)==0)
printf("stack2->%d ",pop(&stack2));
}
else if(isEmpty(stack2)==1){
while(isEmpty(stack1)==0)
printf("stack1->%d ",pop(&stack1));
}
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.