You are on page 1of 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 1.2

Student Name: Aman thakur UID:22BCS16401


Branch: CSE Section/Group:901-A
Semester: 4 Date of Performance:20-01-24
Subject Name: Python Subject Code:22CSH-259

1. Aim: Implementation of Stack, Queue and Linked List.

2. Source Code:

2.1 CODE FOR STACK:


# Define a class named Stack
class Stack:
# Constructor method to initialize an empty list to store stack
items
def __init__(self):
self.items = []

# Method to check if the stack is empty


def is_empty(self):
return len(self.items) == 0

# Method to push an item onto the stack


def push(self, item):
self.items.append(item)

# Method to pop an item from the stack


def pop(self):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Check if the stack is not empty before popping


if not self.is_empty():
return self.items.pop()
else:
# Raise an IndexError if trying to pop from an empty
stack
raise IndexError("pop from an empty stack")

# Method to peek at the top item of the stack without


removing it
def peek(self):
# Check if the stack is not empty before peeking
if not self.is_empty():
return self.items[-1]
else:
# Raise an IndexError if trying to peek from an empty
stack
raise IndexError("peek from an empty stack")

# Method to get the size (number of items) of the stack


def size(self):
return len(self.items)

# Example usage of Stack


# Create an instance of the Stack class
stack = Stack()

# Push some items onto the stack


stack.push(1)
stack.push(2)
stack.push(3)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Print the current state of the stack

print("Stack:", stack.items)

# Print the top item of the stack without removing it


print("Peek:", stack.peek())

# Pop an item from the stack and print it


print("Pop:", stack.pop())

# Print the current size of the stack


print("Size:", stack.size())

2.2 CODE FOR QUEUE:

# Define a Queue class


class Queue:
# Initialize the queue with an empty list
def __init__(self):
self.items = []

# Check if the queue is empty


def is_empty(self):
return len(self.items) == 0

# Add an item to the end of the queue


def enqueue(self, item):
self.items.append(item)

# Remove and return the front item from the queue


def dequeue(self):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Check if the queue is not empty


if not self.is_empty():
# Remove and return the front item (index 0)
return self.items.pop(0)
else:
# Raise an error if dequeue is attempted on an empty
queue
raise IndexError("dequeue from an empty queue")

# Return the front item of the queue without removing it


def front(self):
# Check if the queue is not empty
if not self.is_empty():
# Return the front item (index 0)
return self.items[0]
else:
# Raise an error if front is called on an empty queue
raise IndexError("front from an empty queue")

# Return the size (number of items) of the queue


def size(self):
return len(self.items)

# Example usage of the Queue


queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)

# Display the current state of the queue


print("Queue:", queue.items)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Display the front item of the queue


print("Front:", queue.front())

# Dequeue (remove and display) the front item of the queue


print("Dequeue:", queue.dequeue())

# Display the updated size of the queue


print("Size:", queue.size())

2.3 CODE FOR LINKED-LIST:

# Define a Node class to represent individual


elements in the linked list
class Node:
# Initialize a node with a data value and a
reference to the next node
def __init__(self, data):
self.data = data
self.next = None

# Define a LinkedList class to represent the linked


list
class LinkedList:
# Initialize the linked list with an empty head (no
elements)
def __init__(self):
self.head = None

# Check if the linked list is empty


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

def is_empty(self):
return self.head is None

# Append a new node with the given data to the


end of the linked list
def append(self, data):
# Create a new node with the provided data
new_node = Node(data)

# Check if the linked list is empty


if self.is_empty():
# If empty, make the new node the head of
the linked list
self.head = new_node
else:
# If not empty, traverse to the end and add
the new node
current = self.head
while current.next:
current = current.next
current.next = new_node

# Display the elements of the linked list


def display(self):
# Create an empty list to store the elements
elements = []
# Start from the head of the linked list
current = self.head
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Traverse the linked list and append each


element to the list
while current:
elements.append(current.data)
current = current.next
# Print the elements joined by an arrow (->)
print(" -> ".join(map(str, elements)))

# Example usage of the Linked List


linked_list = LinkedList()
linked_list.append(1)
linked_list.append(2)
linked_list.append(3)

# Display the current state of the linked list


print("Linked List:")
linked_list.display()

3. Screenshot of Outputs:
OUTPUT OF STACK:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

OUTPUT OF QUEUE:

OUTPUT OF LINKED LIST:

You might also like