1) Download and run. 2) Add the ability to push an integer array containing [8][
ID: 3864336 • Letter: 1
Question
1) Download and run. 2) Add the ability to push an integer array containing [8][7][6][5][4][3][2][1] when option 8 is selected. Element [1] should be at the top of the stack at the ed of this operation. 2) Add the ability to push an integer array containing [12][11][10][9] when option 4 is selected. Element [9] should be at the top of the stack at the ed of this operation. 3) Add the ability to pop 6 elements off the stack when option 6 is selected. 4) Demo for checkoff */ /* Write a C program to implement stack. Stack is a LIFO data strcuture * * LIFO - Last in First Out * * Perform PUSH(insert operation), POP(Delete operation) and Display stack */ #include <stdio.h> #define MAXSIZE 5 struct Stack /* Structure definition for stack */ { int stk[MAXSIZE]; int top; }; struct Stack s; /* Function declaration/Prototype*/ void push (void); int pop(void); void display (void); void main () { int choice; int option = 1; s.top = -1; printf ("STACK OPERATION "); while (option) { printf ("------------------------------------------ "); printf (" 1 --> PUSH "); printf (" 2 --> POP "); printf (" 3 --> DISPLAY "); printf (" 4 --> EXIT "); printf ("------------------------------------------ "); printf ("Enter your choice "); scanf ("%d", &choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: return; } printf ("Do you want to continue(Type 0 or 1)? "); scanf ("%d", &option); } } /*Function to add an element to the stack*/ void push () { int num; if (s.top == (MAXSIZE - 1)) { printf ("Stack is Full "); return; } else { printf ("Enter the element to be pushed "); scanf ("%d", &num); s.top = s.top + 1; s.stk[s.top] = num; } return; } /*Function to delete an element from the stack*/ int pop () { int num; if (s.top == - 1) { printf ("Stack is Empty "); return (s.top); } else { num = s.stk[s.top]; printf ("poped element is = %d ", s.stk[s.top]); s.top = s.top - 1; } return(num); } /*Function to display the status of the stack*/ void display () { int i; if (s.top == -1) { printf ("Stack is empty "); return; } else { printf (" The status of the stack is "); for (i = s.top; i >= 0; i--) { printf ("%d ", s.stk[i]); } } printf (" "); }
Explanation / Answer
I think it is better to implement the stack as a linked list if you want to add these functionalities. I have done the same.
Please check the code and comment if you need anything else.
/* Write a C program to implement stack. Stack is a LIFO data strcuture *
* LIFO - Last in First Out *
* Perform PUSH(insert operation), POP(Delete operation) and Display stack */
#include<stdio.h>
#include<stdlib.h>
typedef struct stack /* Structure definition for stack */
{
int value;
struct stack* next;
}Stack;
Stack * st = NULL; // It's not a good practice to declare variable global.
int arr8[] = {8,7,6,5,4,3,2,1};
int arr4[] = {12,11,10,9};
/* Function declaration/Prototype*/
void push();
void push_4();
void push_8();
int pop();
int pop_6();
void display();
void main ()
{
int choice = 1;
int option = 1;
printf ("STACK OPERATION ");
while (option)
{
printf ("------------------------------------------ ");
printf (" 1 --> PUSH ");
printf (" 2 --> POP ");
printf (" 3 --> DISPLAY ");
printf (" 4 --> PUSH 4 ELEMENTS ");
printf (" 6 --> POP 6 ELEMENTS ");
printf (" 8 --> PUSH 8 ELEMENTS ");
printf (" 9 --> EXIT ");
printf ("------------------------------------------ ");
printf ("Enter your choice ");
scanf ("%d", &choice);
switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4:
push_4();
break;
case 6:
pop_6();
break;
case 8:
push_8();
break;
case 9:
return;
default:
printf("** Wrong option ");
break;
}
printf ("Do you want to continue(Type 0 or 1)? ");
scanf ("%d", &option);
}
return;
}
/*Function to add an element to the stack*/
void push ()
{
int num = 0;
Stack* temp = (Stack*)malloc(sizeof(Stack));
printf ("Enter the element to be pushed ");
scanf ("%d", &num);
temp->value = num;
temp->next = NULL;
if(st == NULL){
st =temp;
}
else{
temp->next = st;
st = temp;
}
return;
}
/*Function to delete an element from the stack*/
int pop ()
{
int num;
Stack* temp = NULL;
if (st == NULL)
{
printf ("Stack is Empty ");
return -100;
}
else
{
num = st->value;
temp = st;
st = st->next;
temp->next = NULL;
free(temp);
printf ("popped element is = %d ", num);
}
return(num);
}
/*Function to display the status of the stack*/
void display ()
{
int i = 0;
Stack* temp = NULL;
if (st == NULL)
{
printf ("Stack is empty ");
return;
}
else
{
printf(" The status of the stack is ");
printf(" HEAD | ");
temp = st;
while(temp != NULL)
{
printf ("%d ", temp->value);
if(temp->next != NULL){
printf("-> ");
}
temp = temp->next;
}
}
printf(" ");
return;
}
void push_8(){
int size = 8, i =0;
Stack* temp = NULL;
while(i < size){
temp = (Stack*)malloc(sizeof(Stack));
temp->value = arr8[i];
temp->next = NULL;
if(st == NULL){
st = temp;
}
else{
temp->next = st;
st = temp;
}
i++;
}
return ;
}
void push_4(){
int size = 4, i =0;
Stack* temp = NULL;
while(i < size){
temp = (Stack*)malloc(sizeof(Stack));
temp->value = arr4[i];
temp->next = NULL;
if(st == NULL){
st = temp;
}
else{
temp->next = st;
st = temp;
}
i++;
}
return ;
}
int pop_6()
{
int num = 0 , i = 1;
Stack* temp = NULL;
if(st == NULL)
printf("Stack is Empty ");
while((i <= 6) && ( (num = pop()) != -100 )){
num = pop();
i++;
}
if(i <=6){
printf("Popped %d elements from stack . Stack is now empty ",i-1 );
}
return (i-1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.