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

Linked List and Stack

Uploaded by

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

Linked List and Stack

Uploaded by

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

Link List Operations

1. Traversal

Ex. Finding the lowest number in the linked list


2. Deletion 3.Insertion
class Node:
def __init__(self, data):
self.data = data
self.next = None

# Function to insert a node at the end of the list


def insert_end(head, data):
new_node = Node(data)
if head is None:
return new_node
temp = head
while temp.next is not None:
temp = temp.next
temp.next = new_node
return head

# Function to remove nodes with odd indices


def remove_odd_indices(head):
if head is None:
return None

current = head
prev = None
index = 0

while current is not None:


if index % 2 == 1: # If the index is odd
prev.next = current.next
else: # If the index is even
prev = current
current = current.next
index += 1

Return head

# Function to print the linked list


def print_list(head):
temp = head
while temp is not None:
print(temp.data, end=” -> “)
temp = temp.next
print(“NULL”)

# Test the program


head = None

# Insert elements into the linked list


head = insert_end(head, 1)
head = insert_end(head, 2)
head = insert_end(head, 3)
head = insert_end(head, 4)
head= insert_end(head, 5)
head = insert_end(head, 6)
STACK Basic operations
 Push: Adds a new element on the stack.
 Pop: Removes and returns the top element from the stack.
 Peek: Returns the top element on the stack.
 isEmpty: Checks if the stack is empty.
 Size: Finds the number of elements in the stack.

You might also like