0% found this document useful (0 votes)
16 views

Stacks

Uploaded by

freefirr0426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Stacks

Uploaded by

freefirr0426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Stacks

1. Define a stack data structure.(2M)

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle,
meaning the last element added to the stack is the first to be removed.

 Basic Operations:
o Push: Adds an element to the top of the stack.
o Pop: Removes the top element from the stack.

2. What is the main principle of a stack?(2M)


The main principle of a stack is Last In, First Out (LIFO), meaning the last element added to
the stack is the first one to be removed.
3. How does a stack differ from a queue?(2M)
A stack follows the LIFO (Last In, First Out) principle, where the last element added is the
first to be removed.
A queue follows the FIFO (First In, First Out) principle, where the first element added is the
first to be removed.

4. What is stack overflow?(2M)


Stack overflow occurs when a stack exceeds its maximum capacity, usually due to excessive
push operations in a static stack or infinite recursion in a program. This leads to memory
allocation failure.
Stack overflow occurs when a stack exceeds its maximum capacity, usually due to excessive
push operations in a static stack or infinite recursion in a program. This leads to memory
allocation failure.

5. Write a program in C to implement a stack using arrays and

demonstrate push and pop operations.(5M)

#include <stdio.h>

#define MAX 5 // Maximum size of the stack

int stack[MAX];

int top = -1; // Initialize stack as empty

// Function to push an element

void push(int value) {

if (top == MAX - 1) {

printf("Stack Overflow!\n");

} else {
stack[++top] = value;

printf("%d pushed onto the stack.\n", value);

// Function to pop an element

void pop() {

if (top == -1) {

printf("Stack Underflow!\n");

} else {

printf("%d popped from the stack.\n", stack[top--]);

// Main function

int main() {

push(10); // Push example

push(20); // Push example

pop(); // Pop example

pop(); // Pop example

pop(); // Stack underflow example

return 0;

o/p:

10 pushed onto the stack.

20 pushed onto the stack.

20 popped from the stack.

10 popped from the stack.

Stack Underflow!

6. Describe in detail how to implement a stack using a array ,


including algorithms for push , pop, and display

operations.(10M)

6. Describe in detail how to implement a stack using a array ,

including algorithms for push , pop, and display

operations.(10M)

Structure of the Stack

1. Array: Used to store stack elements.


2. Top: A variable to track the index of the topmost element in the stack. Initially set to
-1 to indicate the stack is empty.
3. Size: The maximum number of elements the stack can hold (defined as
MAX_stack_size).

Algorithms

1. Push Operation:

 Input: Element to be added.


 Output: Updated stack with the element added at the top.
 Steps:
1. Check if the stack is full (i.e., top == MAX - 1).
2. If full, print "Stack Overflow" and exit.
3. Otherwise, increment top and insert the element at stack[top].

Algorithms:

Push(stack, top, MAX, value):

if top == MAX - 1:

Print "Stack Overflow"

Exit

top = top + 1

stack[top] = value

Print value + " pushed onto the stack"

2. Pop Operation:

 Input: None.
 Output: Element removed from the top of the stack.
 Steps:
1. Check if the stack is empty (i.e., top == -1).
2. If empty, print "Stack Underflow" and exit.
3. Otherwise, retrieve the element at stack[top], decrement top, and return the
value.

Algorithms:

Pop(stack, top):

if top == -1:

Print "Stack Underflow"

Exit

value = stack[top]

top = top - 1

Print value + " popped from the stack"

3. Display Operation:

 Input: None.
 Output: Prints all elements of the stack from top to bottom.
 Steps:
1. Check if the stack is empty (i.e., top == -1).
2. If empty, print "The stack is empty."
3. Otherwise, iterate from top to 0 and print each element.

Display(stack, top):

if top == -1:

Print "The stack is empty"

Exit

for i = top to 0:

Print stack[i]

7. Define a task.What is its principle of operation?(2M)

A task is a unit of work or computation that a system executes as part of a process or


program.

Principle of Operation:

Tasks operate on the principle of concurrency or sequential execution, depending on the


system design. They may run sequentially (one after the other) or concurrently (multiple tasks
executing simultaneously) to achieve efficiency and responsiveness in a program or system.
8. What is Stack Underflow?(2M)

Stack Underflow occurs when a pop operation is attempted on an empty stack. Since there
are no elements to remove, it results in an error condition indicating that the stack is empty.

9. Write the main operations performed on a stack?(2M)

 Push: Adds an element to the top of the stack.


 Pop: Removes and returns the top element from the stack.
10. What are the steps involved in Push operation?(2M)

The steps involved in the Push operation on a stack are:

1. Check for Overflow: Ensure that the stack is not full (i.e., top != MAX - 1).
2. Increment the Top: Increase the top pointer to the next position.
3. Insert the Element: Place the new element at the position pointed to by top.

11. What are the steps involved in Pop operation?(2M)

The steps involved in the Pop operation on a stack are:

1. Check for Underflow: Ensure that the stack is not empty (i.e., top != -1).
2. Retrieve the Element: Access and store the element at the position pointed to by top.
3. Decrement the Top: Decrease the top pointer to the previous position.

12. Explain the push and pop operations in a stack with

algorithms.(5M)

Algorithms

1. Push Operation:

 Input: Element to be added.


 Output: Updated stack with the element added at the top.
 Steps:
1. Check if the stack is full (i.e., top == MAX - 1).
2. If full, print "Stack Overflow" and exit.
3. Otherwise, increment top and insert the element at stack[top].

Algorithms:

Push(stack, top, MAX, value):

if top == MAX - 1:
Print "Stack Overflow"

Exit

top = top + 1

stack[top] = value

Print value + " pushed onto the stack"

2. Pop Operation:

 Input: None.
 Output: Element removed from the top of the stack.
 Steps:
1. Check if the stack is empty (i.e., top == -1).
2. If empty, print "Stack Underflow" and exit.
3. Otherwise, retrieve the element at stack[top], decrement top, and return the
value.

Algorithms:

Pop(stack, top):

if top == -1:

Print "Stack Underflow"

Exit

value = stack[top]

top = top - 1

Print value + " popped from the stack"

13. Write a C program to implement stack operations(push, pop,

and display) using an array.(10M)

#include <stdio.h>

#define MAX 5

int stack[MAX];

int top = -1; // Initialize stack as empty


// Push operation

void push(int value) {

if (top < MAX - 1) {

stack[++top] = value;

printf("%d pushed to stack\n", value);

} else {

printf("Stack Overflow\n");

// Pop operation

void pop() {

if (top >= 0) {

printf("%d popped from stack\n", stack[top--]);

} else {

printf("Stack Underflow\n");

// Display operation

void display() {

if (top >= 0) {

for (int i = top; i >= 0; i--) {

printf("%d ", stack[i]);

printf("\n");

} else {

printf("Stack is empty\n");

}
// Main function

int main() {

push(10);

push(20);

display();

pop();

display();

pop();

pop(); // Testing underflow

return 0;

o/p:

10 pushed to stack

20 pushed to stack

20 10

20 popped from stack

10 pushed to stack

Stack is empty

You might also like