Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 2
Stack in C programming
Write push(), pop(), isFull() and isEmpty() functions for an integer stack. Create a menu-driven program that repeatedly takes one of the following four options as input: 1, 2 ,3 or 4 for PUSH, POP, DISPLAY and EXIT respectively, and performs the appropriate sequence of actions. Note that DISPLAY prints stack elements in order from A[0] up to A[TOP]. Test input will comprise of a series of PUSH/POP followed by DISPLAY and EXIT. Any attempts to PUSH onto a full stack or POP from an empty stack should be simply ignored by your program, i.e., it should not even print any error message. Assume that the maximum stack size is 10 elements. Example input: 1 10 1 20 1 30 2 3 4 (Meaning: Push 10, Push 20, Push 30, Pop, Display Exit) Output: 10 20
15 Answers
+ 5
top is starting from -1. Thats why it is storing than maximum limit.
to store 10 elements, you need to check top == MAX - 1.
+ 3
Post your code
+ 3
Ravi
I think there should not print anything except stack elements.
Where did you get this program?
https://code.sololearn.com/cQoCW74EC107/?ref=app
+ 1
// Try this functions :)
#include <stdio.h>
#define MAX 10
int stack[MAX];
int top = -1;
int push(int val){
if(!isFull()){
top = top + 1;
stack[top] = val;
}
else{
printf("STACK IS FULL\n");
}
}
int pop(){
int val;
if(!isEmpty()){
val = stack[top];
top = top - 1;
return val;
}
else{
printf("STACK IS EMPTY\n");
}
}
int isEmpty(){
if(top == -1)
return 1;
else
return 0;
}
int isFull(){
if(top == MAX-1)
return 1;
else
return 0;
}
void getSize(){
printf("\nSIZE : %d\n", top+1);
}
void display(){
int i;
if(top == -1){
printf("\nSTACK IS EMPTY/UNDERFLOW");
}
else{
for(i = top; i >= 0; i--){
printf("\n%d", stack[i]);
}
}
}
void peek(){
int val;
if(!isEmpty()){
val = stack[top];
printf("%d\n\n", val);
}
else{
printf("STACK IS EMPTY\n\n");
}
}
0
https://code.sololearn.com/c3VmLg6G93s3/?ref=app
this is my attempt, why this is storing more that max i.e. 10 elements?
0
I'm confused in "any attempt to push onto full stack or pop from empty stack should be simply ignored by the program."
So shall I put \n in printf statements or any other thing?
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
Please help me
0
Ravi
Code is working as per example. Where did you get problem
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
It is storing more than maximum limit i.e. 10, when we try to do so.
0
RUSIRU
It is written in the Question that no message and error should be printed when stack is full or empty, it should simply ignore.
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
Is it ok to write \n in printf statement when stack is full or empty as it is written in the Question that no error and message should be printed when stack is full or empty.
Please help
Or you suggest your code please
0
This is a example only. You can refer this code and practice it by yourself. PRACTICING MAKES YOU PERFECT :)
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
In my 3rd Semester assignment problem.
0
Ravi
Ok so there should be more examples. Can you share?
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟
No, this is the complete Question.
Only one example was there