0% found this document useful (0 votes)
30 views15 pages

DS Python 22

The document discusses creating and manipulating linked lists and doubly linked lists in Python. It covers creating and connecting nodes to form singly linked lists, traversing linked lists, inserting nodes at different positions in linked lists, removing nodes from linked lists, creating doubly linked lists with prev and next pointers, and inserting and appending nodes to doubly linked lists.

Uploaded by

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

DS Python 22

The document discusses creating and manipulating linked lists and doubly linked lists in Python. It covers creating and connecting nodes to form singly linked lists, traversing linked lists, inserting nodes at different positions in linked lists, removing nodes from linked lists, creating doubly linked lists with prev and next pointers, and inserting and appending nodes to doubly linked lists.

Uploaded by

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

1.

Creating Linked List

class Node:

def __init__(self, dataval=None):

self.dataval = dataval

self.nextval = None

class SLinkedList:

def __init__(self):

self.headval = None

list1 = SLinkedList()

list1.headval = Node("Mon")

e2 = Node("Tue")

e3 = Node("Wed")

# Link first Node to second node

list1.headval.nextval = e2

# Link second Node to third node

e2.nextval = e3
2.Traversing Linked List

class Node:

def __init__(self, dataval=None):

self.dataval = dataval

self.nextval = None

class SLinkedList:

def __init__(self):

self.headval = None

def listprint(self):

printval = self.headval

while printval is not None:

print (printval.dataval)

printval = printval.nextval

list = SLinkedList()

list.headval = Node("Mon")

e2 = Node("Tue")

e3 = Node("Wed")

# Link first Node to second node

list.headval.nextval = e2

# Link second Node to third node

e2.nextval = e3

list.listprint()
3.Insertion in LinkedList

a) Inserting at beginning

class Node:

def __init__(self, dataval=None):

self.dataval = dataval

self.nextval = None

class SLinkedList:

def __init__(self):

self.headval = None

# Print the linked list

def listprint(self):

printval = self.headval

while printval is not None:

print (printval.dataval)

printval = printval.nextval

def AtBegining(self,newdata):

NewNode = Node(newdata)

# Update the new nodes next val to existing node

NewNode.nextval = self.headval

self.headval = NewNode

list = SLinkedList()

list.headval = Node("Mon")

e2 = Node("Tue")

e3 = Node("Wed")
list.headval.nextval = e2

e2.nextval = e3

list.AtBegining("Sun")

list.listprint()

b) Inserting at End

class Node:

def __init__(self, dataval=None):

self.dataval = dataval

self.nextval = None

class SLinkedList:

def __init__(self):

self.headval = None

# Function to add newnode

def AtEnd(self, newdata):

NewNode = Node(newdata)

if self.headval is None:

self.headval = NewNode

return

laste = self.headval

while(laste.nextval):

laste = laste.nextval

laste.nextval=NewNode
# Print the linked list

def listprint(self):

printval = self.headval

while printval is not None:

print (printval.dataval)

printval = printval.nextval

list = SLinkedList()

list.headval = Node("Mon")

e2 = Node("Tue")

e3 = Node("Wed")

list.headval.nextval = e2

e2.nextval = e3

list.AtEnd("Thu")

list.listprint()

c) Inserting in between two nodes

class Node:

def __init__(self, dataval=None):

self.dataval = dataval

self.nextval = None

class SLinkedList:

def __init__(self):

self.headval = None

# Function to add node


def Inbetween(self,middle_node,newdata):

if middle_node is None:

print("The mentioned node is absent")

return

NewNode = Node(newdata)

NewNode.nextval = middle_node.nextval

middle_node.nextval = NewNode

# Print the linked list

def listprint(self):

printval = self.headval

while printval is not None:

print (printval.dataval)

printval = printval.nextval

list = SLinkedList()

list.headval = Node("Mon")

e2 = Node("Tue")

e3 = Node("Thu")

list.headval.nextval = e2

e2.nextval = e3

list.Inbetween(list.headval.nextval,"Fri")

list.listprint()
4. Removing node in Linked List

class Node:

def __init__(self, data=None):

self.data = data

self.next = None

class SLinkedList:

def __init__(self):

self.head = None

def Atbegining(self, data_in):

NewNode = Node(data_in)

NewNode.next = self.head

self.head = NewNode

# Function to remove node

def RemoveNode(self, Removekey):

HeadVal = self.head

if (HeadVal is not None):

if (HeadVal.data == Removekey):

self.head = HeadVal.next

HeadVal = None

return

while (HeadVal is not None):

if HeadVal.data == Removekey:

break

prev = HeadVal

HeadVal = HeadVal.next

if (HeadVal == None):

return
prev.next = HeadVal.next

HeadVal = None

def LListprint(self):

printval = self.head

while (printval):

print(printval.data),

printval = printval.next

llist = SLinkedList()

llist.Atbegining("Mon")

llist.Atbegining("Tue")

llist.Atbegining("Wed")

llist.Atbegining("Thu")

llist.RemoveNode("Tue")

llist.LListprint()
5. Creating Doubly Linked List

class Node:

def __init__(self, data):

self.data = data

self.next = None

self.prev = None

class doubly_linked_list:

def __init__(self):

self.head = None

# Adding data elements

def push(self, NewVal):

NewNode = Node(NewVal)

NewNode.next = self.head

if self.head is not None:

self.head.prev = NewNode

self.head = NewNode

# Print the Doubly Linked list

def listprint(self, node):

while (node is not None):

print(node.data),

last = node

node = node.next

dllist = doubly_linked_list()

dllist.push(12)

dllist.push(8)

dllist.push(62)

dllist.listprint(dllist.head)
6. Inserting in Doubly Linked List

# Create the Node class

class Node:

def __init__(self, data):

self.data = data

self.next = None

self.prev = None

# Create the doubly linked list

class doubly_linked_list:

def __init__(self):

self.head = None

# Define the push method to add elements

def push(self, NewVal):

NewNode = Node(NewVal)

NewNode.next = self.head

if self.head is not None:

self.head.prev = NewNode

self.head = NewNode

# Define the insert method to insert the element

def insert(self, prev_node, NewVal):

if prev_node is None:

return

NewNode = Node(NewVal)

NewNode.next = prev_node.next

prev_node.next = NewNode

NewNode.prev = prev_node
if NewNode.next is not None:

NewNode.next.prev = NewNode

# Define the method to print the linked list

def listprint(self, node):

while (node is not None):

print(node.data),

last = node

node = node.next

dllist = doubly_linked_list()

dllist.push(12)

dllist.push(8)

dllist.push(62)

dllist.insert(dllist.head.next, 13)

dllist.listprint(dllist.head)
7. Appending in Doubly Linked List

# Create the node class

class Node:

def __init__(self, data):

self.data = data

self.next = None

self.prev = None

# Create the doubly linked list class

class doubly_linked_list:

def __init__(self):

self.head = None

# Define the push method to add elements at the begining

def push(self, NewVal):

NewNode = Node(NewVal)

NewNode.next = self.head

if self.head is not None:

self.head.prev = NewNode

self.head = NewNode

# Define the append method to add elements at the end

def append(self, NewVal):

NewNode = Node(NewVal)

NewNode.next = None

if self.head is None:

NewNode.prev = None

self.head = NewNode

return
last = self.head

while (last.next is not None):

last = last.next

last.next = NewNode

NewNode.prev = last

return

# Define the method to print

def listprint(self, node):

while (node is not None):

print(node.data),

last = node

node = node.next

dllist = doubly_linked_list()

dllist.push(12)

dllist.append(9)

dllist.push(8)

dllist.push(62)

dllist.append(45)

dllist.listprint(dllist.head)
8. Stack in Python (PUSH)

class Stack:

def __init__(self):

self.stack = []

def add(self, dataval):

# Use list append method to add element

if dataval not in self.stack:

self.stack.append(dataval)

return True

else:

return False

# Use peek to look at the top of the stack

def peek(self):

return self.stack[-1]

AStack = Stack()

AStack.add("Mon")

AStack.add("Tue")

AStack.peek()

print(AStack.peek())

AStack.add("Wed")

AStack.add("Thu")

print(AStack.peek())
9. Stack in Python (POP)

class Stack:

def __init__(self):

self.stack = []

def add(self, dataval):

# Use list append method to add element

if dataval not in self.stack:

self.stack.append(dataval)

return True

else:

return False

# Use list pop method to remove element

def remove(self):

if len(self.stack) <= 0:

return ("No element in the Stack")

else:

return self.stack.pop()

AStack = Stack()

AStack.add("Mon")

AStack.add("Tue")

AStack.add("Wed")

AStack.add("Thu")

print(AStack.remove())

print(AStack.remove())

You might also like