Presentation on theme: "ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues."— Presentation transcript:
ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues
= + ADT : An abstract data type (ADT) is a set of objects together with a set of operations. Problem Data Structure (ADT) Algorithm Graph = (V, E) Tree Binary Tree Binary Search Tree Balanced BST
Note – Logical relationship between elements. = Linear ADT Linear Direct AccessArray / Set Sequential AccessList Stack (Fire In Last Out) Queue (Fire In First Out) Non-LinearTree / Graph
1. List List : A list is an abstract data type that implements a finite ordered collection of values, where the same value may occur more than once. Operations - printList() - makeEmpty() - Find() = Search - Insert() - Delete / Remove()
1. List Implement ① Array ② Linked List SortedUnsorted printListO(N) FindO(logN)O(N) InsertO(N)O(1) DeleteO(N) SortedUnsorted printListO(N) FindO(N) InsertO(1) DeleteO(N)
2. Stack (LIFO) Stack : A special case of a List A stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop. Simple representation of a stack
2. Stack Mathematic Expression ① infix expression -> A+B ② postfix expression -> AB+ ③ prefix expression -> +AB * Evaluate a postfix expression -> We have to use a stack infixpostfixresult stack
3. Queues: (FIFO) These are special case of lists. In a queue, the insertion is done at one end and deletion is done at the other end. Dequeue Enqueue < < Queue
Operations (Queue) 1.Enqueue ……………. O (1) 2.Dequeue …………... O (1) 3.IsEmpty …………..... O (1) 4.IsFull …………………. O (1) Enqueue : Inserts an element at the end of the list called the rear. Dequeue: Deletes the element at the start of the list called as front.
Implementation of Queues:Array vs. Single Linked List In Queue, same like stacks, both the linked list and array implementations give fast O(1) running times for every operation. Example: For each queue data structure, we keep an array “thearray” positions “front” and “back” which represents the ends of the queue, number of elements in the queue “currentsize” To enqueue an element “X”, increment currentsize and back then set thearray[back]= X. To dequeue an element, set the return value to thearray[front], decrement currentsize and then increment front. There arises a problem with this implementation, after several times of performing enqueue operation the queue will be full since back is now at the last array index, the next queue would be in a non existing position. The simple solution to this problem is that whenever front or back gets to the end of array, it is wrapped around to the beginning, know as Circular array implementation.
1 2 4 After enqueue(1) 24 Initial state After enqueue(3) After dequeue, which returns After dequeue, which returns 4
After dequeue, which returns After dequeue, which returns 3 and makes queue empty
How to distinguish queue is empty to queue is full? For an array, Use upto n-1 items Use “counter” to keep track of # number of items in a queue If counter=0 then, array is full. If counter=1 then, array is empty. Applications of Queues: There are many algorithms to give efficient running times. 1.Generally all waiting lines which we see around is a queue. For instance, Waiting line at a ticket counter are queues, because they follow first come first serve rule. 2.Calls to large companies are generally placed on a queue when all operators are busy. 3.Jobs sent to a line printer are placed on a queue.