-
1 Attachment(s)
Stack & Queue
ok i have this activity which i dont know if it's really the correct output on the input ones.
i have this problem making two stacks and two queues.
A.Input stack: used to store all user input
B.input queue: used to store all user input
C.Output stack: used to store data deleted from input queue
D. Output queue: used to store data deleted from input stack
I only have this menu on selecting insert or delete.
i'll just post my main.c
Code:
#include<stdio.h>
#include<stdlib.h>
#include"teststack.h"
void main(){
StackList s1=create();
int choice,item;
do{
do{
printf("Press 1-Insert\n");
printf("Press 2-Delete\n");
printf("Press 3-Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
if(choice<1 || choice>3)
printf("Invalid input");
}while (choice<1 || choice>3);
switch(choice)
{
case 1:
printf ("Enter number to push: ");
scanf ("%d", &item);
printf("\n====================");
printf("\nInput Stack: \n");
push (s1, item);
display(s1);
printf("\n");
printf("\nInput Queue: \n");
enqueue(s1, item);
display(s1);
printf("====================\n");
break;
case 2:
pop(s1);
push (s1, item);
printf("\n====================");
printf("\nOutput Stack: \n");
display(s1);
printf("\n");
printf("\nOutput Queue: \n");
dequeue(s1);
enqueue(s1, item);
display(s1);
printf("\n");
printf("====================\n");
break;
default:
printf("End.");
}
}while(choice!=3);
}
and this is the output of Input Stacks & Queues.
Attachment 12929
is this correct? :o
-
Usually stack places items at one end and deletes "pops" them off from the same end. Example:
stack: [ ]
push 1
stack: [1]
push 50
stack: [1 50]
push 12
stack: [1 50 12]
pop() returns 12
stack: [1 50]
pop() returns 50
stack: [1]
Stack is called LIFO "last in first out" for this reason. Queue in concept is similar but deletion hapens from opposite end ("first in first out" FIFO). Your queue program seems to operating at both ends based on reading the output.