# Data Structure – codewindow.in

## Related Topics ## Data Structure

#### Here is an example implementation of a singly linked list in Python:

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

def __init__(self):

def insert_at_beginning(self, value):
new_node = Node(value)

def insert_at_end(self, value):
new_node = Node(value)
return
while current_node.next:
current_node = current_node.next
current_node.next = new_node

def delete_node(self, value):
return
return
while current_node.next:
if current_node.next.value == value:
current_node.next = current_node.next.next
return
current_node = current_node.next

def print_list(self):
while current_node:
print(current_node.value)
current_node = current_node.next
``````

#### Here is an example implementation of a doubly linked list in Python:

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

def __init__(self):
self.tail = None

def insert_at_beginning(self, value):
new_node = Node(value)
self.tail = new_node
else:

def insert_at_end(self, value):
new_node = Node(value)
if not self.tail:
self.tail = new_node
else:
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node

def delete_node(self, value):
return
return
while current_node.next:
if current_node.next.value == value:
current_node.next = current_node.next.next
if current_node.next:
current_node.next.prev = current_node
else:
self.tail = current_node
return
current_node = current_node.next

def print_list(self):
while current_node:
print(current_node.value)
current_node = current_node.next
``````

#### Here is an example implementation of a circular linked list in Python:

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

def __init__(self):
self.tail = None

def insert_at_beginning(self, value):
new_node = Node(value)
self.tail = new_node
else:

def insert_at_end(self, value):
new_node = Node(value)
if not self.tail:
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node

def delete_node(self, value):
return
return
if current_node.next.value == value:
current_node.next = current_node.next.next
return
current_node = current_node.next
if self.tail.value == value:
self.tail = current_node

def print_list(self):
while current_node:
print(current_node.value)
current_node = current_node.next
break
``````

#### Here is an example implementation of a stack using a singly linked list in Python:

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

class Stack:
def __init__(self):
self.top = None

def push(self, value):
new_node = Node(value)
new_node.next = self.top
self.top = new_node

def pop(self):
if not self.top:
return None
value = self.top.value
self.top = self.top.next
return value

def peek(self):
if not self.top:
return None
return self.top.value

def is_empty(self):
return self.top is None
``````

#### In this implementation, the `Stack` class has a single instance variable `top` that points to the top of the stack. When we push a new element onto the stack, we create a new node with the given value and set its `next` attribute to the current `top` node. Then, we set the `top` node to be the new node, effectively making it the new top of the stack. When we pop an element from the stack, we return the value of the `top` node and update `top` to point to the next node in the stack. If the stack is empty, we return `None`. The `peek` method returns the value of the `top` node without removing it from the stack. Finally, the `is_empty` method checks if the `top` node is `None` to determine if the stack is empty. #### Questions on Chapter 1  #### Questions on Chapter 1  ## We Loveto Support you

Go through our study material. Your Job is awaiting.