SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
Data Structures-2
Prof. K. Adisesha
BE, M.Sc., M.Th., NET, (Ph.D.)
2
Learning objectives
Objective
• Linear data structures
• Type Linear data structures
 Stacks – PUSH, POP using a list (Single Data, Multiple Data)
 Queue- INSERT, DELETE using a list (Single Data, Multiple Data)
• Implementation in Python
3
Data Structure
What is Data Structure ?
• Data structures are a way of organizing and storing data so
that they can be accessed and worked with efficiently.
• Linear Data Structures
 Data collections of ordered items
 Order depends on how items are
added and removed
 Once added item stays in position
 Examples: stacks, queues
4
Linear Data Structure
Characteristics Linear Data Structures:
• Two ends (left – right, front – rear)
• Linear structures distinguished by how items are added and
removed
• Additions of new items only allowed at the end
• Deletion of existing items only allowed at the end
• Appear in many algorithms
5
Linear Data Structure
Stack:
• Stacks are linear Data Structures which are based on the principle
of Last-In-First-Out (LIFO) where data which is entered last will
be the first to get accessed.
 Addition/removal of items always takes place at same end (top)
 Base represents bottom and contains item has been in stack the
longest
 Most recently added to be removed first (last in-first-out, LIFO)
 Top: newer items;
 bottom: lower items
6
Stack
Operations of Stack
• PUSH: pushing (adding) elements into Top of Stack,
• POP: Popping (deleting) elements and accessing elements from Top of
Stack.
• TOP: This TOP is the pointer to the current position of the stack.
7
Stack
Applications Using Stacks
• Stacks are prominently used in applications such as:
 Recursive Programming
 Reversing words
 Expression Conversion
• In-fix to Post-fix
 Backtracking
 Undo mechanisms in word editors
 Check if delimiters are matched
• Matching of opening and closing symbols: {,},[,],(,)
• Check: {{a}[b]{[{c}](d(e)f)}((g))} and ({[a}b(c)])
8
Stack
Stack - Abstract Data Type
• Stack() creates a new, empty stack; no parameters and returns an
empty stack.
• push(item) adds a new item at top of stack; needs the item and
returns nothing.
• pop() removes top item; needs no parameters, returns item, stack is
modified
• peek() returns top item from the stack but doesn’t remove it; needs no
parameters, stack is not modified
• isEmpty() test if stack is empty; needs no parameters, returns a
boolean value
• size() returns number of items on stack; needs no parameters; returns
an integer
9
Stack
Implementing Stack using List in Python
10
Stack
Implementing Stack using List in Python
• To use lists as stacks, that is a data structures (LIFO).
 An item can be added to a list by using the append() method.
 The last item can be removed from the list by using the pop()
method without passing any index to it.
• Example
>>> stack = ['a','b','c','d']
>>> stack.append('e')
>>> stack.append('f')
>>> stack
Output: ['a', 'b', 'c', 'd', 'e', 'f']
>>> stack.pop()
Output: 'f'
>>> stack
Output: ['a, 'b', 'c', 'd', 'e']
11
Stack
Implementing Stack using List in Python
#Program to implement Stack Operation on Single data
def push(stack,x): #function to add element at the end of list
stack.append(x)
def pop(stack): #function to remove last element from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
def display(stack): #function to display stack entry
if len(stack)<=0:
print("Stack empty...........Nothing to display")
for i in stack:
print(i,end=" ")
12
Stack
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")
13
Queue
Queue
• A queue is also a linear data structure which is based on the
principle of First-In-First-Out (FIFO)
• where the data entered first will be accessed first.
• It has operations which can be performed from both ends of the
Queue, that is, head-tail or front-back.
 En-Queue: Add items on one end
 De-Queue: Remove items on the other end
14
Queue
Queue
• A queue is also a linear data structure which is based on the
principle of First-In-First-Out (FIFO)
 En-Queue: Add items on one end (Rear)
 De-Queue: Remove items on the other end (Front)
15
Queue
Applications of Queues
• Queues are used in various applications:
 In network equipment like switches and routers
 Network Buffers for traffic congestion management
 Operating Systems for Job Scheduling
 Checkout line
 Printer queue
 Take-off at airport
16
Queue
Queue - Abstract Data Type
• Queue() creates a new, empty queue; no parameters and
returns an empty queue.
• enqueue(item) adds a new item to rear of queue; needs the
item and returns nothing.
• dequeue() removes front item; needs no parameters, returns
item, queue is modified
• isEmpty() test if queue is empty; needs no parameters, returns
a boolean value
• size() returns number of items in the queue; needs no
parameters; returns an integer
17
Queue
Implementing Queue in Python
18
Queue
Implementing Queue in Python
• Usage of list, again presented in Python documentation is to use lists as
queues, that is a first in - first out (FIFO).
• Example
>>> queue = ['a', 'b', 'c', 'd']
>>> queue.append('e')
>>> queue.append('f')
>>> queue
Output: ['a', 'b', 'c', 'd', 'e', 'f']
>>> queue.pop(0)
Output: 'a'
19
Queue
Implementing Queue using List in Python
def add_element(Queue,x): #function to add element at the end of list
Queue.append(x)
def delete_element(Queue): #function to remove last element from list
n = len(Queue)
if(n<=0):
print("Queue empty....Deletion not possible")
else:
del(Queue[0])
def display(Queue): #function to display Queue entry
if len(Queue)<=0:
print("Queue empty...........Nothing to display")
for i in Queue:
print(i,end=" ")
20
Queue
#main program starts from here
x=[]
choice=0
while (choice!=4):
print(" ********Queue menu***********")
print("1. Add Element ")
print("2. Delete Element")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice : "))
if(choice==1):
value = int(input("Enter value : "))
add_element(x,value)
if(choice==2):
delete_element(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")
21
• We learned about:
 Linear data structures
 Type Linear data structures
• Stacks – PUSH, POP using a list (Single Data, Multiple Data)
• Queue- INSERT, DELETE using a list (Single Data, Multiple Data)
 Implementation in Python
Thank you
Conclusion!

Weitere ähnliche Inhalte

Was ist angesagt?

Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 

Was ist angesagt? (20)

Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEFUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Lists
ListsLists
Lists
 
Java Queue.pptx
Java Queue.pptxJava Queue.pptx
Java Queue.pptx
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Data Structures
Data StructuresData Structures
Data Structures
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
List in Python
List in PythonList in Python
List in Python
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Python-List.pptx
Python-List.pptxPython-List.pptx
Python-List.pptx
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 

Ähnlich wie 9 python data structure-2

Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
Getachew Ganfur
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 

Ähnlich wie 9 python data structure-2 (20)

Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Data Structures and Files
Data Structures and FilesData Structures and Files
Data Structures and Files
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
CD3291 2.5 stack.pptx
CD3291 2.5 stack.pptxCD3291 2.5 stack.pptx
CD3291 2.5 stack.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Queue
QueueQueue
Queue
 
Stacks
StacksStacks
Stacks
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 

Mehr von Prof. Dr. K. Adisesha

Mehr von Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Kürzlich hochgeladen (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

9 python data structure-2

  • 1. Data Structures-2 Prof. K. Adisesha BE, M.Sc., M.Th., NET, (Ph.D.)
  • 2. 2 Learning objectives Objective • Linear data structures • Type Linear data structures  Stacks – PUSH, POP using a list (Single Data, Multiple Data)  Queue- INSERT, DELETE using a list (Single Data, Multiple Data) • Implementation in Python
  • 3. 3 Data Structure What is Data Structure ? • Data structures are a way of organizing and storing data so that they can be accessed and worked with efficiently. • Linear Data Structures  Data collections of ordered items  Order depends on how items are added and removed  Once added item stays in position  Examples: stacks, queues
  • 4. 4 Linear Data Structure Characteristics Linear Data Structures: • Two ends (left – right, front – rear) • Linear structures distinguished by how items are added and removed • Additions of new items only allowed at the end • Deletion of existing items only allowed at the end • Appear in many algorithms
  • 5. 5 Linear Data Structure Stack: • Stacks are linear Data Structures which are based on the principle of Last-In-First-Out (LIFO) where data which is entered last will be the first to get accessed.  Addition/removal of items always takes place at same end (top)  Base represents bottom and contains item has been in stack the longest  Most recently added to be removed first (last in-first-out, LIFO)  Top: newer items;  bottom: lower items
  • 6. 6 Stack Operations of Stack • PUSH: pushing (adding) elements into Top of Stack, • POP: Popping (deleting) elements and accessing elements from Top of Stack. • TOP: This TOP is the pointer to the current position of the stack.
  • 7. 7 Stack Applications Using Stacks • Stacks are prominently used in applications such as:  Recursive Programming  Reversing words  Expression Conversion • In-fix to Post-fix  Backtracking  Undo mechanisms in word editors  Check if delimiters are matched • Matching of opening and closing symbols: {,},[,],(,) • Check: {{a}[b]{[{c}](d(e)f)}((g))} and ({[a}b(c)])
  • 8. 8 Stack Stack - Abstract Data Type • Stack() creates a new, empty stack; no parameters and returns an empty stack. • push(item) adds a new item at top of stack; needs the item and returns nothing. • pop() removes top item; needs no parameters, returns item, stack is modified • peek() returns top item from the stack but doesn’t remove it; needs no parameters, stack is not modified • isEmpty() test if stack is empty; needs no parameters, returns a boolean value • size() returns number of items on stack; needs no parameters; returns an integer
  • 10. 10 Stack Implementing Stack using List in Python • To use lists as stacks, that is a data structures (LIFO).  An item can be added to a list by using the append() method.  The last item can be removed from the list by using the pop() method without passing any index to it. • Example >>> stack = ['a','b','c','d'] >>> stack.append('e') >>> stack.append('f') >>> stack Output: ['a', 'b', 'c', 'd', 'e', 'f'] >>> stack.pop() Output: 'f' >>> stack Output: ['a, 'b', 'c', 'd', 'e']
  • 11. 11 Stack Implementing Stack using List in Python #Program to implement Stack Operation on Single data def push(stack,x): #function to add element at the end of list stack.append(x) def pop(stack): #function to remove last element from list n = len(stack) if(n<=0): print("Stack empty....Pop not possible") else: stack.pop() def display(stack): #function to display stack entry if len(stack)<=0: print("Stack empty...........Nothing to display") for i in stack: print(i,end=" ")
  • 12. 12 Stack #main program starts from here x=[] choice=0 while (choice!=4): print("********Stack Menu***********") print("1. push(INSERT)") print("2. pop(DELETE)") print("3. Display ") print("4. Exit") choice = int(input("Enter your choice :")) if(choice==1): value = int(input("Enter value ")) push(x,value) if(choice==2): pop(x) if(choice==3): display(x) if(choice==4): print("You selected to close this program")
  • 13. 13 Queue Queue • A queue is also a linear data structure which is based on the principle of First-In-First-Out (FIFO) • where the data entered first will be accessed first. • It has operations which can be performed from both ends of the Queue, that is, head-tail or front-back.  En-Queue: Add items on one end  De-Queue: Remove items on the other end
  • 14. 14 Queue Queue • A queue is also a linear data structure which is based on the principle of First-In-First-Out (FIFO)  En-Queue: Add items on one end (Rear)  De-Queue: Remove items on the other end (Front)
  • 15. 15 Queue Applications of Queues • Queues are used in various applications:  In network equipment like switches and routers  Network Buffers for traffic congestion management  Operating Systems for Job Scheduling  Checkout line  Printer queue  Take-off at airport
  • 16. 16 Queue Queue - Abstract Data Type • Queue() creates a new, empty queue; no parameters and returns an empty queue. • enqueue(item) adds a new item to rear of queue; needs the item and returns nothing. • dequeue() removes front item; needs no parameters, returns item, queue is modified • isEmpty() test if queue is empty; needs no parameters, returns a boolean value • size() returns number of items in the queue; needs no parameters; returns an integer
  • 18. 18 Queue Implementing Queue in Python • Usage of list, again presented in Python documentation is to use lists as queues, that is a first in - first out (FIFO). • Example >>> queue = ['a', 'b', 'c', 'd'] >>> queue.append('e') >>> queue.append('f') >>> queue Output: ['a', 'b', 'c', 'd', 'e', 'f'] >>> queue.pop(0) Output: 'a'
  • 19. 19 Queue Implementing Queue using List in Python def add_element(Queue,x): #function to add element at the end of list Queue.append(x) def delete_element(Queue): #function to remove last element from list n = len(Queue) if(n<=0): print("Queue empty....Deletion not possible") else: del(Queue[0]) def display(Queue): #function to display Queue entry if len(Queue)<=0: print("Queue empty...........Nothing to display") for i in Queue: print(i,end=" ")
  • 20. 20 Queue #main program starts from here x=[] choice=0 while (choice!=4): print(" ********Queue menu***********") print("1. Add Element ") print("2. Delete Element") print("3. Display ") print("4. Exit") choice = int(input("Enter your choice : ")) if(choice==1): value = int(input("Enter value : ")) add_element(x,value) if(choice==2): delete_element(x) if(choice==3): display(x) if(choice==4): print("You selected to close this program")
  • 21. 21 • We learned about:  Linear data structures  Type Linear data structures • Stacks – PUSH, POP using a list (Single Data, Multiple Data) • Queue- INSERT, DELETE using a list (Single Data, Multiple Data)  Implementation in Python Thank you Conclusion!