Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues.

Similar presentations


Presentation on theme: "ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues."— Presentation transcript:

1 ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues

2 = + 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

3 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

4 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()

5 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)

6 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

7 2. Stack Operations Implement - Insert() -> Push() - Delete() -> Pop() - Top() - IsEmpty() - IsFull() ArraySingle-Linked List Push()O(1) Pop()O(1) Top()O(1) IsEmpty()O(1) IsFull()O(1)

8 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

9 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

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

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

12 1 2 4 After enqueue(1) 24 Initial state 1 3 2 4 After enqueue(3) 1 3 2 4 After dequeue, which returns 2 1 3 2 4 After dequeue, which returns 4

13 1 3 2 4 After dequeue, which returns 1 1 3 2 4 After dequeue, which returns 3 and makes queue empty

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


Download ppt "ADVANCED DATA STRUCTURES AND ALGORITHM ANALYSIS Chapter 3 Lists, Stacks, and Queues."

Similar presentations


Ads by Google