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

2 Stack

Uploaded by

karanjaiswalf39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2 Stack

Uploaded by

karanjaiswalf39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Part 1

Assignment no. 2 Title:


Stack
and Part 2 are mandatory.
Part 3 is bonus. You can expect part 3 type ques ons in the end semester exam.
• Part l
1. Stack Implementa on:
a. Define a constant "MAX SIZE" to represent the maximum size of the stack.
b. Define an integer array named "stack" with a size of "MAX SIZE" to represent the stack.
c. Declare an integer variable named "top" and ini alize it to -1. This variable will track the index of the topmost
element in the stack.
d. Create func ons for the following stack opera ons:

• "push" - adds an element to the top of the stack.

• "pop" - removes the topmost element from the stack.

• "isEmpty" - checks if the stack is empty.

• "isFull" - checks if the stack is full.


• "display" - prints the elements of the stack.
e. Implement the func ons men oned above to perform the respec ve opera ons.
2. Stack Opera ons:

a. Create an empty stack using the defined stack implementa on.


b. Push some integer values onto the stack.
c. Print the elements of the stack using the "display" func on.
d. Pop elements from the stack and print the popped elements.
e. Check if the stack is empty and print the result.
f. Push addi onal elements onto the stack and print the updated stack.

1. Parentheses Matching:
a. Implement a func on named "isParenthesesMatch" that takes a string as a parameter and checks if the
parentheses in the string are balanced.
b. The func on should use a stack to check for balanced parentheses.
Part 2
c. Test the func on by passing strings with balanced and unbalanced parentheses and print the results.
2. Repeat the above assignment (1-2 in Part 1 and 1 in Part 2) using linked lists as a replacement for array.
Part 3

1. Expression Evalua on: Implement a func on that ta kes a mathema cal expression in infix nota on as a string and
evaluates it using the stack-based approach for infix to pos ix conversion and pos ix evalua on. Test the func on by
passing different mathema cal expressions and prin ng the results.
2. Stack Reversal: Implement a func on that takes a stack as a parameter and reverses its order using an auxiliary stack.
Print the elements of the reversed stack to verify the reversal.
3. Stack-based Algorithm: Implement a stack-based algorithm such as pos ix expression evalua on, infix to pos ix
conversion, or infix to prefix conversion. Choose one algorithm and implement it using the stack data structure. Test
the func on by passing suitable inputs and prin ng the results.
4. Func on Call Stack: The stack is extensively used in programming languages to handle func on calls and manage local
variables. When a func on is called, its execu on context, including parameters and local variables, is pushed onto the
stack. When the func on completes execu on, its context is popped from the stack, and control is returned to the
calling func on.
This mechanism allows for nested func on calls and efficient memory management.
5. Undo/Redo Opera ons: Stacks are used to implement the undo/redo func onality in various applica ons, including
text editors, graphic design so wa re, and comma nd-line interfaces. Each performed ac on is stored on a stack,
allowing the user to undo the last ac on by popping it from the stack. The undone ac ons can be pushed onto a redo
stack, enabling the user to redo them if needed.
6. Browser History: Web browsers u lize stacks to maintain the history of visited web pages. Each me a user visits a new
page, the URL is pushed onto the stack. When the user clicks the "Back" bu on, the last visited URL is popped from the
stack, allowing naviga on to the previous page. Similarly, the "Forward" bu on can be implemented using a separate
stack to store URLs when the user navigates back.
7. Balancing Symbols: Stacks are helpful in checking the balance of symbols, such as parentheses, braces, and brackets,
in programming languages. As code is parsed, opening symbols are pushed onto the stack, and when a closing symbol
is encountered, it is compared with the topmost symbol on the stack. If they match, the opening symbol is popped,
indica ng balanced symbols. Any imbalance in the stack indicates an error in the code.
8. Backtracking Algorithms: Various algorithms, like depth-first search (DFS) and backtracking, employ stacks to keep track
of the visited nodes or states. In DFS, the stack is used to store unvisited adjacent nodes during traversal, allowing for
backtracking to explore other paths. Similarly, backtracking algorithms store the state of the search space on a stack,
enabling explora on of alterna ve paths when needed.
PART 1:
#include <stdio.h>

#define MAX SIZE 5 // Define the maximum size of the


stack int stack[MAX SIZE]; // Define the stack int top = -1;
// Ini alize the top index to -1 // Func on to add an
element to the stack void push(int value) { if (top MAX SIZE-
1) {
prin ("Stack Overflow! Cannot push %d\n", value);

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

prin ("%d pushed to stack\n",

value);

// Func on to remove the topmost element from the

stack int pop() { if (top < O) { prin ("Stack Underflow! No

element to pop\n"); return -1;

} else { int poppedValue =

stack[top--]; return

poppedValue;

// Func on to check if the stack is empty


int isEmpty() {
return top - -1•

// Func on to check if the stack is full


int isFull() {

return top - MAX SIZE -1;

// Func on to display the elements of the stack


void display() {
if (top < O) { prin ("Stack is

empty\n");

} else { prin ("Stack elements

are: ");
for (int i i top; {
prin ("%d ", stack[i]);

int main() {

// Stack Opera ons

// Push elements onto the

stack push(10); push(20);

push(30); push(40);

push(50);

// A empt to push an element to a full stack

push(60);

// Display elements of the stack

display();

// Pop elements from the stack and display the popped

elements prin ("Popped element: %d\n", pop());

prin ("Popped element: %d\n", pop()); // Check if the stack is

empty if (isEmpty()) { prin ("Stack is empty\n");

} else { prin ("Stack is not

empty\n");

// Push addi onal elements onto the

stack push(70); push(80);

// Display the updated stack


display();

return O;

OUTPUT
10 pushed to stack

20 pushed to stack
30 pushed to stack

40 pushed to stack

50 pushed to stack

Stack Overflow! Cannot push 60

Stack elements are: 10 20 30 40 50

Popped element: 50

Popped element:

40 Stack is not

empty 70 pushed

to stack

80 pushed to stack

Stack elements are: 10 20 30 70 80

Code Execu on Successful

PART 2:
#include <stdio.h>

#include <string.h>

#define MAX SIZE 100 // Define maximum size of


stack char stack[MAX SIZE]; // Stack to store
parentheses int top = -1; // Ini alize top to -1 //
Func on to push an element to the stack void
push(char c) { if (top < MAX SIZE- 1) { stack[++top] =
c;

// Func on to pop an element from the

stack char pop() { if (top O) { return

stack[top--];

return '\O';

// Func on to check if the stack is empty


int isEmpty() {

return top - -1•


// Func on to check if the parentheses are
balanced int isParenthesesMatch(char* expr) { for
(int i i < strlen(expr); i++) { if (expr[i] I l expr[i] - I l
expr[i] push(expr[i]);

} else if (expr[i] " I l expr[l] - I l expr[i] {


if (isEmpty()) { return O; // Stack is empty,

so not balanced

char topChar = pop(); if ((expr[i] ')'

&& topChar (expr[i]

&& topChar I l

(expr[i] && topChar

return O; // Mismatch found

return isEmpty(); // Return true if stack is empty, meaning all parentheses are
matched

// Test the func on

int main() { char exprl[] = "(a + b)

* (c + d)"; char expr2[] = "(a +

b) * (c +

char
expr3[] -
char

expr4[] prin ("Expression:

%s\n", exprl);

prin ("Balanced: isParenthesesMatch(expr1) ? "Yes" • "No )

prin ("Expression: %s\n", expr2);

prin ("Balanced: isParenthesesMatch(expr2) ? "Yes" • "No )


prin ("Expression: expr3);

prin ("Balanced: isParenthesesMatch(expr3) ? "Yes" • "No )

prin ("Expression: expr4);


prin ("Balanced: %s\n", isParenthesesMatch(expr4) ? "Yes" : "No )

return O;

OUTPUT
Expression: (a + b) * (c + d)

Balanced: Yes

Expression: (a + b) * (c + d))
Balanced: No

Expression: {[a + b] * (c + d)}


Balanced: Yes
Expression: [a + b) * (c + d)]
Balanced: No

Code Execu on Successful

You might also like