# Data Structure – codewindow.in

## Related Topics ## Data Structure

1. #### Declare an array to hold the stack elements and an integer variable to hold the top index of the stack.

``````#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
``````
1. #### Push operation: To insert an element into the stack, increment the top variable and insert the element at the top of the stack.

``````void push(int element) {
if (top >= MAX_SIZE - 1) {
printf("Stack overflow!\n");
return;
}
stack[++top] = element;
}
``````
1. #### Pop operation: To remove an element from the stack, return the element at the top of the stack and decrement the top variable.

``````int pop() {
if (top < 0) {
printf("Stack underflow!\n");
return -1;
}
return stack[top--];
}
``````
1. #### Peek operation: To return the element at the top of the stack without removing it, simply return the element at the top index of the array.

``````int peek() {
if (top < 0) {
printf("Stack is empty!\n");
return -1;
}
return stack[top];
}
``````
1. #### Check if stack is empty: To check if the stack is empty, simply check if the top variable is less than 0.

``````int is_empty() {
}
``````

#### Here's an example implementation:

``````class Node:
def __init__(self, value):
self.value = value
self.next = None

class Stack:
def __init__(self):
self.size = 0

def push(self, value):
new_node = Node(value)
self.size += 1

def pop(self):
return None
else:
self.size -= 1
return value

def peek(self):
return None
else:

def is_empty(self):
return self.size == 0
``````

#### To reverse a string using a stack, we can push each character of the string onto the stack, then pop the characters off the stack and append them to a new string. Here is an example implementation in Python:

``````def reverse_string(string):
stack = []
for char in string:
stack.append(char)
reversed_string = ''
while len(stack) > 0:
reversed_string += stack.pop()
return reversed_string
``````

#### Here is an example implementation in Python:

``````def check_balanced_parentheses(expression):
stack = []
for char in expression:
if char in '([{':
stack.append(char)
elif char in ')]}':
if not stack:
return False
top = stack.pop()
if (char == ')' and top != '(') or \
(char == ']' and top != '[') or \
(char == '}' and top != '{'):
return False
return not stack
``````

#### This implementation assumes that the expression only contains parentheses, square brackets, and curly braces, and no other characters. If the expression can contain other characters, you may need to modify the implementation accordingly. #### Questions on Chapter 4  #### Questions on Chapter 5  ## We Loveto Support you

Go through our study material. Your Job is awaiting.