In: Computer Science
You do not have to do this in a function. This can all be done in the main function if you like.
A queue is a similar data structure in which the first item inserted into the queue is the first item processed (FIFO). After I choose to quit the stack loop, reallocate the array pointer back to a single integer and then repeat the program above, but so it processes the integers in the order of a queue rather than a stack. The output should indicate that we are now using a queue as well.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count = 0;
// pointer to implement stack
int *ptr;
//dynamic memory allocation using malloc
ptr = (int*)malloc(sizeof(int));
while(1)
{
//menu
printf("Menu:\n");
printf("1.Insert a new integer onto the stack\n");
printf("2.Process an integer from the stack\n");
printf("3.Quit the stack loop\n");
//Printing state of stack
if(count==0)
{
printf("Stack is currently empty\n");
}
else
{
printf("Current state of stack : ");
for (int i = count-1; i>=0; --i){
if(i!=count-1)
printf(", ");
printf("%d", ptr[i]);
}
printf("\n\n");
}
printf("Enter the desired option\n");
int temp;
scanf("%d",&temp);
if(temp==1)
{
int input;
printf("You choosed option 1. Enter the value to be pushed on stack\n");
scanf("%d",&input);
count++;
//Reallocation of memory using realloc
ptr = (int*)realloc(ptr,count*sizeof(int));
ptr[count-1]=input;
}
else if(temp==2)
{
printf("You choosed option 2. Processed element is %d\n",ptr[count-1]);
count--;
ptr = (int*)realloc(ptr,count*sizeof(int));
}
else if(temp==3)
{
printf("You choosed option 3. You are out of the stack loop.\n\n");
//free function to release memory
free(ptr);
break;
}
else
{
printf("Invalid Input\n");
}
}
//implemetation of queue
int head=0;
count = 0;
ptr = (int*)malloc(sizeof(int));
while(1)
{
printf("Menu:\n");
printf("1.Insert a new integer in the queue\n");
printf("2.Process an integer from the queue\n");
printf("3.Quit the queue and exit\n");
if(count==0)
{
printf("Queue is currently empty\n");
}
else
{
printf("Current state of queue : ");
for (int i = head; i<count; ++i){
if(i!=head)
printf(", ");
printf("%d", ptr[i]);
}
printf("\n\n");
}
printf("Enter the desired option\n");
int temp;
scanf("%d",&temp);
if(temp==1)
{
int input;
printf("You choosed option 1. Enter the value to be pushed in queue\n");
scanf("%d",&input);
count++;
ptr = (int*)realloc(ptr,count*sizeof(int));
ptr[count-1]=input;
}
else if(temp==2)
{
printf("You choosed option 2. Processed element is %d\n",ptr[head]);
head++;
}
else if(temp==3)
{
printf("You choosed option 3. You are out of the queue loop and exitted from the program.");
free(ptr);
break;
}
else
{
printf("Invalid Input\n");
}
}
}
C code which implemets stack and queue and allows the user to choose between three options: 1.to insert an element, 2. to take out and print the element, 3 to quit and exit.
First the stack implementation is done using int pointer ptr and dynmaic memory allocation is done using malloc.
Integer variable count is maintained which represents the size of the stack. Contents of the stack are printed out after each time menu is printed. Memory is reallocated if either option1 or 2 is chosen using realloc() and memory is freed using free() if option3 is choden.
Queue implementation is performed in a similar fashion but the processing of elements is different in the queue. Queue is based on first in first out scheme where the first element entering the queue is the first to leave out where as Stack is based on last in first out scheme where the last element entering the stack is the first to leave out.