Data structure
In data structure we organize the data in a structure way so , we can use
efficintly
data structure are manly two type
1- syatem or predifine data structure
2- user define data structure
-> in predifine data structure
int , float , doolen , array , set , python( list , set , tuple , set ,
disknery)
-> user define data structure
stack , queas , link-list , heap , tree , graphs
note --> we study predifine data structure in any programing language.
User difine data structure cantinue.
1 stack --
stack also know as LIFO or FILO tackniqu , in this we add elements or data one
by one or one after another , and exess this elements from last or top elements
so examples is redo/undo feathuer , etc
some method of stack = push() , pop() , peek or top , isEmpty
how it use in python or how we create the stack in python
we create stack in python from two ways
1. list
2. mathods
[Link] ( create the stack using list)
push() = append()
pop() = pop()
isEmpty = len()
peek from top = listName[ -1 ]
# creating the stack ############
stack = []
def push():
eliment = int(input("enter the number /n" ))
[Link](eliment)
print(eliment)
def pop():
e = [Link]()
print(e)
def peek():
topelimeant = stack[-1]
print(topelimeant)
while True:
key = input("enter the a: add , d:delite t:for-top-element q:quit")
if key == "a":
push()
elif key == "d":
pop()
elif key == "t":
peek()
elif key == "q":
break
else:
print("enter the following key")
print( stack )
###########################
2-> Queue's
queue is based on fifo method
In Queue we add element from one side and get the element from another side , we
add the element from left side and get the element from right side or add the
element
from right side and get from left side.
eg --> enqueue/front -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7-> 8
dequeue/rear get the element from this side
rear/dequeue 1 <- 2 <- 3 <- 4 <- 5 <- 6 <- 7 <- 8<-
enqueue/front
enqueue: process of adding elements to Queue === append
method
dequeue: process of removing the elements from the Queue === pop
method : [Link](0)
we crete the Queue using list and Modules.
following method is = enqueue , dequeue , isfull , isEmpty
# creating the Queue
queue = []
def enqueue():
eliment = int(input("inter the number "))
[Link](eliment)
print(eliment)
def dequeue():
if not queue:
print(" queue is empty. ")
else:
eliment = [Link](0)
print(eliment)
while True:
key = int(input(" enter follwing numbeers 1:add element , 2:removing the
eliment , 3:quite : "))
if key == 1:
enqueue()
elif key == 2:
dequeue()
elif key ==3:
print(queue)
break
else:
print( "please enter following numbers. ")
following method also
[Link](0,10) --> 10
[Link](0,20) --> 20,10
[Link](0,30) --> 30,20,10
import collections
q = [Link]()
[Link](10)
[Link](20)
[Link]()
one more librery is ( from queue import Queue )
put( item, block=True , timeout)
get( block=True , timeout= None)
=== Priority Queue:
in this given the Priority to the queue eliment for get or selecting
eg = we given the Priority to ascending order eliment
q = [ 1, 8 , 5 ] , [Link] , the select the eliment one by one from low level
one librery is ----> import queue
q = [Link]()
[Link](70)
[Link](50)
[Link](60)
[Link]() we get the element 50
[Link]() we get the element 60