- 2069 Poush · 1+3 marks
Stack definition and basic operations
Short answer
A stack is a linear LIFO structure (e.g., pile of plates). Push inserts at top, pop removes top, peek returns top. Algorithms: push increments top and stores x; pop returns top element and decrements top; overflow/underflow checks ensure bounds.
Define Stack with suitable example. Write down the algorithm for the basic operation that can be performed over the stack.
A stack is a linear data structure in which insertion (push) and deletion (pop) take place only at one end, called the top. The last element inserted is the first one removed, so it follows LIFO (Last In First Out).
Example: a pile of plates. Push 10, 20, 30 gives stack (bottom to top) 10 20 30; pop returns 30, leaving 10 20.
Algorithms (array S[0..MAX-1], top = -1 initially)
PUSH(S, x)
1. if top == MAX - 1: print "Stack overflow"; exit
2. top = top + 1
3. S[top] = x
POP(S)
1. if top == -1: print "Stack underflow"; exit
2. x = S[top]
3. top = top - 1
4. return x
PEEK(S) /* stacktop */
1. if top == -1: print "Stack empty"; exit
2. return S[top]
EMPTY(S): return (top == -1)
FULL(S): return (top == MAX - 1)
C code
#define MAX 5
int s[MAX], top = -1;
void push(int x) { if (top == MAX-1) printf("Stack overflow\n"); else s[++top] = x; }
int pop(void) { if (top == -1) { printf("Stack underflow\n"); return -1; } return s[top--]; }
Every operation takes O(1) time.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- ADT of stack: value and operator definition · Data Structure and Algorithm, chapter 1
- Data structure and basic operations · Data Structure and Algorithm, chapter 1
- Linked list and its basic operations · Data Structure and Algorithm, chapter 4
- Linear and circular queue operations · Data Structure and Algorithm, chapter 2
- Radix sort definition and algorithm · Data Structure and Algorithm, chapter 7
- List and its operations · Data Structure and Algorithm, chapter 3
- Basic forging operations · Workshop Technology, chapter 9
- CCS7 stack; why CCS over in-channel · Telecommunications, chapter 5
- Basic lathe operations · Workshop Technology, chapter 6
- Lathe operations, taper turning and turning calculations · Manufacturing and Production Processes, chapter 7