Stack in C programming | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

29th Sep 2021, 4:12 PM
Ravi
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.
30th Sep 2021, 10:10 AM
Rusiru Rathmina
Rusiru Rathmina - avatar
+ 3
Post your code
29th Sep 2021, 4:42 PM
Rusiru Rathmina
Rusiru Rathmina - avatar
+ 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
30th Sep 2021, 5:26 PM
A͢J
A͢J - avatar
+ 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"); } }
30th Sep 2021, 10:25 AM
Rusiru Rathmina
Rusiru Rathmina - avatar
0
https://code.sololearn.com/c3VmLg6G93s3/?ref=app this is my attempt, why this is storing more that max i.e. 10 elements?
30th Sep 2021, 5:17 AM
Ravi
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?
30th Sep 2021, 5:26 AM
Ravi
30th Sep 2021, 6:04 AM
Ravi
0
Ravi Code is working as per example. Where did you get problem
30th Sep 2021, 6:49 AM
A͢J
A͢J - avatar
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.
30th Sep 2021, 7:28 AM
Ravi
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.
30th Sep 2021, 12:28 PM
Ravi
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
30th Sep 2021, 12:31 PM
Ravi
0
This is a example only. You can refer this code and practice it by yourself. PRACTICING MAKES YOU PERFECT :)
30th Sep 2021, 12:35 PM
Rusiru Rathmina
Rusiru Rathmina - avatar
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ In my 3rd Semester assignment problem.
1st Oct 2021, 12:55 PM
Ravi
0
Ravi Ok so there should be more examples. Can you share?
1st Oct 2021, 3:09 PM
A͢J
A͢J - avatar
0
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ No, this is the complete Question. Only one example was there
2nd Oct 2021, 8:16 AM
Ravi