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

Introduction To Stack

Stack file

Uploaded by

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

Introduction To Stack

Stack file

Uploaded by

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

### Introduction to Stack

A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means the last
element pushed onto the stack will be the first one to be removed. Stacks primarily support two operations:
- Push : Adds an element to the top of the stack.
- Pop : Removes the element from the top of the stack.

In Python, lists can be used to implement stacks, where `append()` acts as the push operation, and `pop()`
acts as the pop operation.

### Stack Operations without Functions

#### Push Operation (without function)

# Initialize an empty stack


stack = []

# Pushing elements into the stack


stack.append(10) # Push 10
stack.append(20) # Push 20
stack.append(30) # Push 30

# Print the stack


print("Stack after pushing elements:", stack)

#### Pop Operation (without function)

# Initialize the stack with some elements


stack = [10, 20, 30] # Elements in the stack

# Popping elements from the stack


element = stack.pop() # Pop the top element (30)
print("Popped element:", element)

# Print the stack after popping


print("Stack after popping an element:", stack)

#### Combined Push and Pop Example (without function)

# Initialize an empty stack


stack = []

# Pushing elements into the stack


stack.append(10) # Push 10
stack.append(20) # Push 20
stack.append(30) # Push 30

# Print the stack


print("Stack after pushing elements:", stack)

1
# Popping elements from the stack
element1 = stack.pop() # Pop the top element (30)
print("Popped element:", element1)

element2 = stack.pop() # Pop the next top element (20)


print("Popped element:", element2)

# Print the stack after popping


print("Stack after popping elements:", stack)

### Stack Operations with Functions

Now let’s make the code more reusable by implementing push and pop operations using functions.

#### Push Operation (with function)

# Define a push function


def push(stack, element):
stack.append(element)
print("Pushed", element, "into the stack")

# Initialize an empty stack


stack = []

# Call the push function


push(stack, 10)
push(stack, 20)
push(stack, 30)

# Print the stack


print("Stack after pushing elements:", stack)

#### Pop Operation (with function)

# Define a pop function


def pop(stack):
if len(stack) == 0:
print("Stack is empty!")
return None
return stack.pop()

# Initialize the stack with some elements


stack = [10, 20, 30]

# Call the pop function


popped_element = pop(stack)
print("Popped element:", popped_element)

# Print the stack after popping


print("Stack after popping an element:", stack)

2
#### Combined Example (with functions)

# Define a push function


def push(stack, element):
stack.append(element)
print("Pushed", element, "into the stack")

# Define a pop function


def pop(stack):
if len(stack) == 0:
print("Stack is empty!")
return None
return stack.pop()

# Initialize an empty stack


stack = []

# Pushing elements using the function


push(stack, 10)
push(stack, 20)
push(stack, 30)

# Popping elements using the function


popped_element1 = pop(stack)
print("Popped element:", popped_element1)

popped_element2 = pop(stack)
print("Popped element:", popped_element2)

# Print the stack after popping


print("Stack after popping elements:", stack)
```

### Programming-based Questions

1. Question : Write a Python function that pushes 5 elements onto a stack and then pops 2 elements. Print
the stack after each operation.
- Answer :

def push(stack, element):


stack.append(element)
print("Pushed", element, "into the stack:", stack)

def pop(stack):
if len(stack) == 0:
print("Stack is empty!")
return None
return stack.pop()

# Initialize an empty stack


stack = []

3
# Push 5 elements
push(stack, 10)
push(stack, 20)
push(stack, 30)
push(stack, 40)
push(stack, 50)

# Pop 2 elements
pop(stack)
print("Stack after popping:", stack)
pop(stack)
print("Stack after popping:", stack)

2. Question : Create a Python program that checks if a stack is empty before performing a pop operation. If
the stack is empty, it should print a message indicating so.
- Answer :

def pop(stack):
if len(stack) == 0:
print("Stack is empty, cannot pop!")
return None
return stack.pop()

# Initialize an empty stack


stack = []

# Try to pop from the empty stack


pop(stack)

# Push an element and then pop


stack.append(100)
pop(stack)

3. Question : How can you implement a peek operation in Python that returns the top element of the stack
without removing it?
- Answer :

def peek(stack):
if len(stack) == 0:
print("Stack is empty, nothing to peek!")
return None
return stack[-1]

# Initialize the stack


stack = [10, 20, 30]

# Peek the top element


top_element = peek(stack)
print("Top element is:", top_element)

You might also like