SlideShare a Scribd company logo
Lecture # 5Lecture # 5
Abstract Data TypeAbstract Data Type
 We have looked at four different implementationsWe have looked at four different implementations
of the List data structures:of the List data structures:
 Using arraysUsing arrays
 Singly linked listSingly linked list
 Doubly linked listDoubly linked list
 Circularly linked list.Circularly linked list.
 The interface to the List stayed the same, i.e.,The interface to the List stayed the same, i.e.,
add(), get(), next(), start(), remove() etc.add(), get(), next(), start(), remove() etc.
 The list is thus an abstract data type; we use itThe list is thus an abstract data type; we use it
without being concerned with how it iswithout being concerned with how it is
implemented.implemented.
Abstract Data TypeAbstract Data Type
 What we care about is theWhat we care about is the methodsmethods that arethat are
available for use with the List ADT.available for use with the List ADT.
 We will follow this theme when we develop otherWe will follow this theme when we develop other
ADT.ADT.
 We willWe will publish the interfacepublish the interface and keep theand keep the
freedom to change the implementation of ADTfreedom to change the implementation of ADT
without effecting users of the ADT.without effecting users of the ADT.
 The C++ classes provide us the ability to createThe C++ classes provide us the ability to create
such ADTs.such ADTs.
StackStack
StackStack
 Stacks in real life: stack of books, stack ofStacks in real life: stack of books, stack of
platesplates
 Add new items at the topAdd new items at the top
 Remove an item at the topRemove an item at the top
 Stack data structure similar to real life:Stack data structure similar to real life:
collection of elements arranged in a linearcollection of elements arranged in a linear
order.order.
 Can only access element at the topCan only access element at the top
 AA stackstack is an ordered collection of items intois an ordered collection of items into
which new items may bewhich new items may be insertedinserted and fromand from
which items may bewhich items may be deleteddeleted at one end only,at one end only,
calledcalled toptop of the stack. i.e.of the stack. i.e. deletion/insertiondeletion/insertion cancan
only be done fromonly be done from toptop of the stack.of the stack.
 TheThe insertinsert operation on a stack is often calledoperation on a stack is often called
PushPush operation andoperation and deletedelete operation is calledoperation is called
PopPop operation.operation.
 Figure showing stack…..Figure showing stack…..
B
A
D
C
Top of theTop of the
StackStack
NULLNULL
 In aIn a stackstack, the element, the element deleteddeleted from the list isfrom the list is
the one mostthe one most recently insertedrecently inserted..
 Stack is also referred asStack is also referred as Last-in First-outLast-in First-out i.ei.e
LIFOLIFO..
 The example of stack is theThe example of stack is the stacks of platesstacks of plates
used in cafeterias. The order in which plates areused in cafeterias. The order in which plates are
poppedpopped from the stack is the reverse of the orderfrom the stack is the reverse of the order
in which they werein which they were pushedpushed onto the stack, sinceonto the stack, since
only theonly the top plate is accessibletop plate is accessible..
 If anIf an emptyempty stack is popped, we say stackstack is popped, we say stack
underflowsunderflows, which is normally an error. and, which is normally an error. and
 IfIf toptop of the stack exceedsof the stack exceeds nn, the stack, the stack
overflowsoverflows..
Stack OperationsStack Operations
 Push(X) – insert X as the top element ofPush(X) – insert X as the top element of
the stackthe stack
 Pop() – remove the top element of thePop() – remove the top element of the
stack and return it.stack and return it.
 Top() – return the top element withoutTop() – return the top element without
removing it from the stack.removing it from the stack.
Stack OperationStack Operation
 The last element to go into the stack is the firstThe last element to go into the stack is the first
to come out:to come out: LIFOLIFO – Last In First Out.– Last In First Out.
 What happens if we call pop() and there is noWhat happens if we call pop() and there is no
element?element?
 Have IsEmpty() boolean function that returnsHave IsEmpty() boolean function that returns
true if stack is empty, false otherwise.true if stack is empty, false otherwise.
 Throw StackEmpty exception: advanced C++Throw StackEmpty exception: advanced C++
concept.concept.
Lecture5
Stack Implementation: ArrayStack Implementation: Array
 Worst caseWorst case for insertion and deletion from anfor insertion and deletion from an
array when insert and delete from thearray when insert and delete from the
beginning:beginning: shift elements to the leftshift elements to the left..
 Best caseBest case for insert and delete is at the endfor insert and delete is at the end
of the array –of the array – no need to shift any elementsno need to shift any elements..
 Implement push() and pop() by inserting andImplement push() and pop() by inserting and
deleting at thedeleting at the end of an arrayend of an array
Stack Implementation: ArrayStack Implementation: Array
Stack using an ArrayStack using an Array
 In case of an array, it is possible that theIn case of an array, it is possible that the
array may “fill-up” if we push enougharray may “fill-up” if we push enough
elements.elements.
 Have a boolean functionHave a boolean function IsFull()IsFull() whichwhich
returns true if stack (array) is full, falsereturns true if stack (array) is full, false
otherwise.otherwise.
 We would call this function before callingWe would call this function before calling
push(x).push(x).
Stack Using Linked ListStack Using Linked List
 We canWe can avoid the size limitationavoid the size limitation of a stackof a stack
implemented with an array by using aimplemented with an array by using a
linked list to hold the stack elements.linked list to hold the stack elements.
 As with array, however, we need to decideAs with array, however, we need to decide
where to insert elements in the list andwhere to insert elements in the list and
where to delete them so that push andwhere to delete them so that push and
pop will run the fastest.pop will run the fastest.
Lecture5

More Related Content

PPTX
Stack project
PPTX
My lecture stack_queue_operation
PPT
Stacks and queue
PPT
data structure, stack, stack data structure
PPTX
Stack and Queue by M.Gomathi Lecturer
PPT
Lecture 2d queues
PPT
stack and queue array implementation in java.
PPT
Stack Implementation
Stack project
My lecture stack_queue_operation
Stacks and queue
data structure, stack, stack data structure
Stack and Queue by M.Gomathi Lecturer
Lecture 2d queues
stack and queue array implementation in java.
Stack Implementation

What's hot (20)

PPTX
Stack and Queue
PPT
Stack & queue
PPT
Stack and queue
PPT
Difference between stack and queue
PPTX
U3.stack queue
PDF
Queues and Stacks
PDF
Stack and Queue (brief)
PPT
Data Structure Lecture 2
PPTX
Data Structures - Lecture 6 [queues]
PPTX
Queue Data Structure (w/ php egs)
PPT
23 stacks-queues-deques
PPT
PPTX
Stack operation algorithms with example
PPT
Stacks & Queues By Ms. Niti Arora
PPT
Stack, queue and hashing
PPTX
stack & queue
PDF
Stacks and queues
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPTX
Mca ii dfs u-3 linklist,stack,queue
Stack and Queue
Stack & queue
Stack and queue
Difference between stack and queue
U3.stack queue
Queues and Stacks
Stack and Queue (brief)
Data Structure Lecture 2
Data Structures - Lecture 6 [queues]
Queue Data Structure (w/ php egs)
23 stacks-queues-deques
Stack operation algorithms with example
Stacks & Queues By Ms. Niti Arora
Stack, queue and hashing
stack & queue
Stacks and queues
Bsc cs ii dfs u-2 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
Ad

Viewers also liked (14)

PPT
Lec8
PPT
Computer notes - Josephus Problem
PPT
9781285852744 ppt ch18
PPT
Stacks in algorithems & data structure
PPTX
10. Search Tree - Data Structures using C++ by Varsha Patil
PPTX
15. STL - Data Structures using C++ by Varsha Patil
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
PPTX
7. Tree - Data Structures using C++ by Varsha Patil
PPTX
Linked list
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPT
មេរៀនៈ Data Structure and Algorithm in C/C++
PPT
Fundamentals of data structures
Lec8
Computer notes - Josephus Problem
9781285852744 ppt ch18
Stacks in algorithems & data structure
10. Search Tree - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
Linked list
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
មេរៀនៈ Data Structure and Algorithm in C/C++
Fundamentals of data structures
Ad

Similar to Lecture5 (20)

PPT
Stack data structures with definition and code
PPTX
Stacks Data structure.pptx
PDF
Stacks
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PDF
DS UNIT 1.pdf
PDF
DS UNIT 1.pdf
PPTX
Data Structures Stack and Queue Data Structures
PPT
Lecture2
PPTX
Stack & Queue
PPTX
Stack and its operations
PPTX
Module 2 ppt.pptx
PPTX
Stack and its operations, Queue and its operations
PPT
Stack linked list
DOCX
CDS artificial intelligence and Machine.docx
DOCX
Stacks in data structure
PPTX
Introduction to stack
PPTX
STACK is a linear data structure it follows lifo approach
PPTX
Stack data structure
PPTX
Stack and its operation implemented with array new - Copy.pptx
PPT
Lect 15-16 Zaheer Abbas
Stack data structures with definition and code
Stacks Data structure.pptx
Stacks
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
DS UNIT 1.pdf
DS UNIT 1.pdf
Data Structures Stack and Queue Data Structures
Lecture2
Stack & Queue
Stack and its operations
Module 2 ppt.pptx
Stack and its operations, Queue and its operations
Stack linked list
CDS artificial intelligence and Machine.docx
Stacks in data structure
Introduction to stack
STACK is a linear data structure it follows lifo approach
Stack data structure
Stack and its operation implemented with array new - Copy.pptx
Lect 15-16 Zaheer Abbas

More from Muhammad Zubair (13)

PPTX
Cloud computing
PPTX
Cloud computing disadvantages
PPT
Lecture10
PPT
Lecture9 recursion
PPT
Lecture8
PPT
Lecture7
PPT
Lecture6
PPT
Lecture4
PPT
Lecture3
PPT
Lecture3
PPT
Lecture1
DOCX
Indin report by zubair
DOCX
Cyber crime in pakistan by zubair
Cloud computing
Cloud computing disadvantages
Lecture10
Lecture9 recursion
Lecture8
Lecture7
Lecture6
Lecture4
Lecture3
Lecture3
Lecture1
Indin report by zubair
Cyber crime in pakistan by zubair

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Computing-Curriculum for Schools in Ghana
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
RMMM.pdf make it easy to upload and study
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Computing-Curriculum for Schools in Ghana
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial diseases, their pathogenesis and prophylaxis
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
102 student loan defaulters named and shamed – Is someone you know on the list?

Lecture5

  • 2. Abstract Data TypeAbstract Data Type  We have looked at four different implementationsWe have looked at four different implementations of the List data structures:of the List data structures:  Using arraysUsing arrays  Singly linked listSingly linked list  Doubly linked listDoubly linked list  Circularly linked list.Circularly linked list.  The interface to the List stayed the same, i.e.,The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc.add(), get(), next(), start(), remove() etc.  The list is thus an abstract data type; we use itThe list is thus an abstract data type; we use it without being concerned with how it iswithout being concerned with how it is implemented.implemented.
  • 3. Abstract Data TypeAbstract Data Type  What we care about is theWhat we care about is the methodsmethods that arethat are available for use with the List ADT.available for use with the List ADT.  We will follow this theme when we develop otherWe will follow this theme when we develop other ADT.ADT.  We willWe will publish the interfacepublish the interface and keep theand keep the freedom to change the implementation of ADTfreedom to change the implementation of ADT without effecting users of the ADT.without effecting users of the ADT.  The C++ classes provide us the ability to createThe C++ classes provide us the ability to create such ADTs.such ADTs.
  • 5. StackStack  Stacks in real life: stack of books, stack ofStacks in real life: stack of books, stack of platesplates  Add new items at the topAdd new items at the top  Remove an item at the topRemove an item at the top  Stack data structure similar to real life:Stack data structure similar to real life: collection of elements arranged in a linearcollection of elements arranged in a linear order.order.  Can only access element at the topCan only access element at the top
  • 6.  AA stackstack is an ordered collection of items intois an ordered collection of items into which new items may bewhich new items may be insertedinserted and fromand from which items may bewhich items may be deleteddeleted at one end only,at one end only, calledcalled toptop of the stack. i.e.of the stack. i.e. deletion/insertiondeletion/insertion cancan only be done fromonly be done from toptop of the stack.of the stack.  TheThe insertinsert operation on a stack is often calledoperation on a stack is often called PushPush operation andoperation and deletedelete operation is calledoperation is called PopPop operation.operation.
  • 7.  Figure showing stack…..Figure showing stack….. B A D C Top of theTop of the StackStack NULLNULL
  • 8.  In aIn a stackstack, the element, the element deleteddeleted from the list isfrom the list is the one mostthe one most recently insertedrecently inserted..  Stack is also referred asStack is also referred as Last-in First-outLast-in First-out i.ei.e LIFOLIFO..  The example of stack is theThe example of stack is the stacks of platesstacks of plates used in cafeterias. The order in which plates areused in cafeterias. The order in which plates are poppedpopped from the stack is the reverse of the orderfrom the stack is the reverse of the order in which they werein which they were pushedpushed onto the stack, sinceonto the stack, since only theonly the top plate is accessibletop plate is accessible..
  • 9.  If anIf an emptyempty stack is popped, we say stackstack is popped, we say stack underflowsunderflows, which is normally an error. and, which is normally an error. and  IfIf toptop of the stack exceedsof the stack exceeds nn, the stack, the stack overflowsoverflows..
  • 10. Stack OperationsStack Operations  Push(X) – insert X as the top element ofPush(X) – insert X as the top element of the stackthe stack  Pop() – remove the top element of thePop() – remove the top element of the stack and return it.stack and return it.  Top() – return the top element withoutTop() – return the top element without removing it from the stack.removing it from the stack.
  • 11. Stack OperationStack Operation  The last element to go into the stack is the firstThe last element to go into the stack is the first to come out:to come out: LIFOLIFO – Last In First Out.– Last In First Out.  What happens if we call pop() and there is noWhat happens if we call pop() and there is no element?element?  Have IsEmpty() boolean function that returnsHave IsEmpty() boolean function that returns true if stack is empty, false otherwise.true if stack is empty, false otherwise.  Throw StackEmpty exception: advanced C++Throw StackEmpty exception: advanced C++ concept.concept.
  • 13. Stack Implementation: ArrayStack Implementation: Array  Worst caseWorst case for insertion and deletion from anfor insertion and deletion from an array when insert and delete from thearray when insert and delete from the beginning:beginning: shift elements to the leftshift elements to the left..  Best caseBest case for insert and delete is at the endfor insert and delete is at the end of the array –of the array – no need to shift any elementsno need to shift any elements..  Implement push() and pop() by inserting andImplement push() and pop() by inserting and deleting at thedeleting at the end of an arrayend of an array
  • 14. Stack Implementation: ArrayStack Implementation: Array
  • 15. Stack using an ArrayStack using an Array  In case of an array, it is possible that theIn case of an array, it is possible that the array may “fill-up” if we push enougharray may “fill-up” if we push enough elements.elements.  Have a boolean functionHave a boolean function IsFull()IsFull() whichwhich returns true if stack (array) is full, falsereturns true if stack (array) is full, false otherwise.otherwise.  We would call this function before callingWe would call this function before calling push(x).push(x).
  • 16. Stack Using Linked ListStack Using Linked List  We canWe can avoid the size limitationavoid the size limitation of a stackof a stack implemented with an array by using aimplemented with an array by using a linked list to hold the stack elements.linked list to hold the stack elements.  As with array, however, we need to decideAs with array, however, we need to decide where to insert elements in the list andwhere to insert elements in the list and where to delete them so that push andwhere to delete them so that push and pop will run the fastest.pop will run the fastest.