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.
12.
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
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.