0% found this document useful (0 votes)
21 views37 pages

Abstract Data Types (Adt)

Uploaded by

AZ8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views37 pages

Abstract Data Types (Adt)

Uploaded by

AZ8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

ABSTRACT DATA TYPES

(ADT)
.
OBJECTIVES
• Show understanding that an ADT is a collection of data and a
set of operations on those data
• Show understanding that data structures not available as
built-in types in a particular programming language need to
be constructed from those data structures which are built-in
within the language
• Write algorithms to find an item in each of the following:
linked list, binary tree, hash table
• Write algorithms to insert an item into each of the following:
stack, queue, linked list, binary tree, hash table
ABSTRACT DATA TYPES
• This is a collection of data and the operation to be
carried out on the data
• For example, in a stack, you will have data and you can
added more data (push) or remove data (pop)
• We will look at three ADTs: stack, queue and linked list
USES OF STACKS, QUEUES AND LINKED LIST

• Stacks, queues and


linked lists are used
in many computing
concepts.
• Some of these are
shown in the table
STACK
• A stack is a list containing several items which are
operating on the last in, first out (LIFO) principle.
• Items can be added to the stack (push) and removed
from the stack (pop).
• The first item added to the stack will be the last item to
be removed from the stack
QUEUE
• A queue on the other hand is a list containing several
items operating on the first in, first out (FIFO)
principle.
• Items can be added to the queue (enqueue) and
removed from the queue (dequeue).
• The first item to be added to the queue is the first item
to be removed from the queue
LINKED LIST
• This is a list containing several items in which each item
in the list points to the next item in the list
• In a linked list, a new item is always added to the start
of the list
STACKS, QUEUES AND LINKED LISTS
• Stacks, queues and linked
lists all make use of pointers
to manage their operations
• Items stored in stacks and
queues are always added at
the end
• Linked lists on the other
hand makes use of an
ordering algorithm for the
items (ascending or
descending)
STACK
• A stack uses two pointers:
• A base pointer – pointing to
the first item in the stack
• A top pointer – pointing to
the last item in the stack
• When the value of the base
pointer and the top pointer are
equal, it means there is only
one item in the stack
QUEUE
• A queue uses two pointers:
• A front pointer – pointing to
the first item in the queue
• A rear pointer – pointing to
the last item in the queue
• When the value of the front
pointer is equal to the value of
the rear pointer, it means there
is only one item in the queue
LINKED LIST

• A linked list uses a start pointer that points to the first


item in the linked list
• Every item in a linked list is stored together with a
pointer to the next item
• This is known as a node
• The last item in a linked list has a null pointer
HOW A STACK OPERATES
• In a stack, the value of the base pointer always remains
the same during the operation of a stack
HOW A STACK OPERATES
• A stack can be implemented using an array and a set of
pointers
• If you define an array with a fixed size (e.g. 10
elements), the stack may get full, so you need to be
mindful of this condition
SETTING UP A STACK
• DECLARE stack : ARRAY[1:10] OF INTEGER
• DECLARE topPointer : INTEGER
• DECLARE basePointer : INTEGER
• DECLARE stackfull : INTEGER
• basePointer  1
• topPointer  0
• stackfull  10
PUSHING AN ITEM ONTO A STACK
• IF topPointer < stackfull
• THEN
• topPointer  topPonter + 1
• Stack[topPointer]  item
• ELSE
• OUTPUT “stack is full, cannot push”
• END IF
POPPING AN ITEM FROM A STACK
• IF topPointer = basePointer – 1
• THEN
• OUTPUT “stack is empty, cannot pop”
• ELSE
• Item  stack[topPointer]
• topPointer  topPointer – 1
• END IF
ACTIVITY
• What will be the value
of the topPointer and
basePointer when an
item has been popped
off the stack and the
number 67 followed by
92 have been pushed
onto the stack
ACTIVITY
• Translate the algorithm for the operation of a stack into
an equivalent Python program.
• You can use functions to split the modules for setting up
stack, pushing an item onto the stack and popping an
item from a stack
QUEUE OPERATIONS
• In a queue, the value of the frontPointer changes after
dequeue (removing an element) but the value of the
rearPointer changes after enqueue (adding an element)
QUEUE OPERATIONS
• Just like a stack, a queue can be implemented using an
array and a set of pointers (index values)
• If an array is defined with a finite size, the queue may
become full so this condition must be accounted for
• Also, as items are removed from the front and added to the
end pf the queue, the position of the queue in the array
changes
• For this reason, the queue should be managed as a circular
queue to avoid moving the position of the items in the array
every time an item is removed
QUEUE OPERATIONS
QUEUE OPERATIONS
• When you implement a queue using an array with a
fixed number of elements, it must be managed as a
circular queue
• This means that both pointers, frontPointer and
reaPointer, are updated to point to the first element and
last element respectively
HOW TO SETUP A QUEUE
• DECLARE queue : ARRAY[1:10] OF INTEGER
• DECLARE rearPointer : INTEGER
• DECLARE frontPointer : INTEGER
• DECLARE queueful : INTEGER
• DECLARE queueLength : INTEGER
• frontPointer  1
• endPointer  0
• upperBound  10
• queueful  10
• queueLength  0
TO ADD AN ITEM TO QUEUE
• IF queueLength < queueful
• THEN
• IF rearPointer < upperBound
• THEN
• rearPointer  rearPointer + 1
• ELSE
• rearPointer  1
• END IF
• Queue[rearPointer]  item
• queueLength  queueLength + 1
• ELSE
• OUTPUT “Queue is full, cannot enqueue”
TO REMOVE AN ITEM FROM QUEUE
• IF queueLength = 0
• THEN
• OUTPUT “Queue is empty, cannot dequeue”
• ELSE
• Item  queue[frontPointer]
• IF frontPointer = upperBound
• THEN
• frontPointer  1
• ELSE
• frontPointer  frontPointer + 1
• END IF
• queueLength  queueLength – 1
• END IF
ACTIVITY
LINKED LIST OPERATION
• A linked list can be implemented using two 1D arrays.
• One array will hold the items in the linked list while the
other will hold the pointers to the next items in the list
• If you define the array with a fixed size, you need to
account for this as the array may be full
• Also, as items are being removed from any position in
the linked list, the empty positions in the array must be
managed as an empty linked list
• These empty linked lists are known as heaps
LINKED LIST OPERATION
• The startPointer = -1,
if the list has no
elements.
• The heap is set up
as a linked list ready
for use
LINKED LIST OPERATION
• The startPointer is set to the
element pointed to by the
heapPointer where 37 is
inserted.
• The heapPonter is set to point
to the next element in the heap
by using the value stored in the
element with the same index in
the pointer list
• Since this is also the last
element in the list the node
pointer for it is reset to -1
LINKED LIST OPERATION
• The startPointer is changed to
the heapPointer and 45 is stored
in the element indexed by the
heapPointer
• The node pointer for this element
is set to the old startPointer.
• The nodePointer for the
heapPoint is reset to point to the
next element in the heap using
the value stored in the element
with the same index in the
pointer list
IDENTIFIER TABLE
• Below is an identifier table we can use to setup a linked
list
SETTING UP A LINKED LIST
• DECLARE myLinkedList : ARRAY [0:11] OF INTEGER
• DECLARE myLinkedListPoniters : ARRAY [0:11] OF INTEGER
• DECLARE startPointer: INTEGER
• DECLARE index : INTEGER
• heapStartPointer  0
• startPointer  -1 // list empty
• FOR index  0 TO 11
• myLinkedListPointers [index]  index + 1
• NEXT index
• myLinkedListPointers [11]  -1 // the final heap pointer is set to -1 to show
there are no futher links
EMPTY LINKED LIST
• Below is an empty linked list with its pointers
POPULATED LINKED LIST
• Below is a linked list with values and corresponding
pointers
INSERTING ITEMS INTO A LIST
• DECLARE itemAdd : INTEGER
• DECLARE startPointer : INTEGER
• DECLARE heapPointer : INTEGER
• DECLARE tempPointer : INTEGER
• CONSTANT nullPointer = - 1
• PROCEDURE linkedListAdd(itemAdd)
• // check if the list is full
• IF headStartPointer = nullPointer
• THEN
• OUTPUT “Linked list is full”
• ELSE
• //Get the next place from the list in the heap
• tempPointer  startPointer // keep the old startPointer
• startPointer  heapStartPointer // set the startPointer to the next position in the heap
• heapStartPointer  myLinkedListPointers[heapStartPointer] // reset heap startPointer
• myLinkedList[startPointer]  itemAdd // put the item in the list
• myLinkedListPointers[startPointer  tempPointer //Update linked list Pointer
• END IF
• END PROCEDURE
FINDING AN ITEM IN THE LINKED LIST
• DECLARE itemSearch : INTEGER
• DECLARE itemPointer : INTEGER
• CONSTANT nullPointer = - 1
• FUNCTION find(itemSearch) RETURN INTEGER
• DECLARE found : BOOLEAN
• Found  FALSE
• WHILE (itemPointer <> nullPointer) AND NOT found DO
• IF myLinkedList[itemPointer] = itemSearch
• THEN
• found  TRUE
• ELSE
• itemPointer  myLinkedListPointers[itemPointer]
• END IF
• END WHILE
• RETURN itemPointer
ASSIGNMENT
• Write Pseudocode to delete an item from a Linked list

You might also like