Skip to content

Commit

Permalink
Merge pull request OpenGenus#3654 from import-keshav/master
Browse files Browse the repository at this point in the history
Single linked list programme in python
  • Loading branch information
AdiChat authored Sep 30, 2018
2 parents b44b0aa + 03bc580 commit 8b15da4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
52 changes: 52 additions & 0 deletions code/data_structures/src/linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
''' simple prpgrame for single linked list in python '''


Head=None ''' head node, initilase Null or none '''
Tail=None ''' tail node, initilase Null or none '''

''' object class '''

class A:
def __init__(self):
self.data=0
self.next=None

''' function for creating node '''

def create_node():
t=A()
return t

''' function for inserting node '''

def insert_node():
global Head,Tail
temp=create_node()
print("Enter your value",end="\n")
a=input()
temp.data=a
if(Head==None):
Head=temp
Tail=temp
else:
Tail.next=temp
Tail=temp

''' function for traversing linked list '''

def traverse(Head):
t=Head
while(t.next!=None):
print(t.data,end="\n")
t=t.next
print(t.data,end="\n")

''' main function '''

insert_node()
insert_node()
insert_node()
insert_node()

traverse(Head) ''' passing head node for tarversing'''

49 changes: 49 additions & 0 deletions code/data_structures/src/queue/double_ended_queue/deque.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
''' python programme for showing dequeue data sturcture in python '''


''' class for queue type data structure having following methods'''

class Queue():
def __init__(self):
self.array=[] # ''' initialise empty array '''

def front_enqueue(self,data): # ''' adding element from front '''
self.array.insert(0,data)

def front_dequeue(self): # ''' deleting element from front '''
k=self.array[0]
del self.array[0]

def rear_dequeue(self): # ''' deleting element from rear '''
k=self.array.pop()
print(k)

def rear_enqueue(self,data): # ''' adding element from rear '''
self.array.append(data)

def traverse(self): # ''' priting all array '''
print(self.array)

queue=Queue() # ''' initialise Queue instance '''
queue.front_enqueue(5)
queue.front_enqueue(4)
queue.front_enqueue(3)
queue.front_enqueue(2)

queue.traverse()

queue.front_dequeue()

queue.traverse()

queue.rear_enqueue(5)
queue.rear_enqueue(4)
queue.rear_enqueue(3)
queue.rear_enqueue(2)

queue.traverse()

queue.rear_dequeue()
queue.rear_dequeue()

queue.traverse()

0 comments on commit 8b15da4

Please sign in to comment.