CS5539 Data Structures and Algorithms - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS5539 Data Structures and Algorithms

Description:

A queue is a sequence of elements where. Nodes can only ... Barney joins queue. Barney. Joe. Harry. front = Joe; back = Barney : 3 elements. Typical Operations ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 17
Provided by: kchr
Category:

less

Transcript and Presenter's Notes

Title: CS5539 Data Structures and Algorithms


1
CS5539Data Structures and Algorithms
  • Lecture 15
  • Linear Structures

2
Queues
  • A queue is a sequence of elements where
  • Nodes can only be removed from the head/front
  • Nodes can only be added to the tail/end
  • A queue is thus a first-in, first-out structure
    (FIFO)
  • A queue has a length
  • the number of elements it contains
  • If a queue is empty it has length zero

3
Reading
  • Watt and Brown Chapter 7 Queues
  • Watt and Brown Chapter 8 Lists

4
Queue Behaviour
no queue
front back 0 elements
Fred
Fred joins queue
front back Fred 1 element
Joe joins queue
front Fred back Joe 2 elements
Harry joins queue
front Fred back Harry 3 elements
Fred catches bus
front Joe back Harry 2 elements
Barney joins queue
front Joe back Barney 3 elements
5
Typical Operations
  • OPERATION PRE-CONDITION POST-CONDITION
  • append (Object item)q not full queue1,
  • item added at end queue
  • remove() q not empty queue 1, item at
    front removed
  • front() q not empty queue same
  • isEmpty() none queue same
  • back() q not empty queue same
  • isFull() none queue same

6
Specification of a Queue ADT
  • Associated ADTs Boolean, Element
  • Operations
  • create Queue
  • append Element Queue Queue
  • front Queue Element
  • remove Queue Queue
  • isEmpty Queue Boolean

7
Specification of a Queue ADT
  • Requirements
  • isEmpty(create) true
  • For every x,q isEmpty(append(x,q)) false
  • front(append(x,create)) x
  • For every x,q!create front(append(x,q))
    front(q)
  • remove(append(x,create)) create
  • For every x,q!create remove(append(x,q))
    append(x,remove(q))
  • Exceptions
  • front(create) is illegal
  • remove(create) is illegal

8
Linked List Implementation
  • public class Queue
  • private class SLLnode
  • Object element
  • SLLnode succ
  • private SLLnode(Object element, SLLnode succ)
  • this.element element
  • this.succ succ
  • private SLLnode front, rear
  • // The methods go here ...

9
Array Implementation
  • public class Queue
  • private Object items
  • private int length, front, back
  • public queue (int maxsize)
  • items new Object maxsize
  • length 0
  • front 0
  • rear 0

10
Methods (Array Implementation)
  • public void append(Object item)
  • if (length items.length) makeLonger()
  • itemsrear item
  • if (rear items.length)
  • rear 0
  • length

11
Methods (Array Implementation)
  • public Object remove()
  • if (length 0)
  • throw new NoSuchElementException()
  • Object firstItem itemsfront
  • itemsfront null
  • if (front items.length)
  • front 0
  • length--
  • return firstItem

12
Methods (Array Implementation)
  • public Object front()
  • if (length 0)
  • throw new NoSuchElementException()
  • return itemsfront
  • public boolean isEmpty()
  • return (length 0)

13
Array Implementation
14
Lists
  • Operations on a list vary according to
    application
  • There may be more than one operation of the same
    name but with different number and types of
    parameters
  • eg
  • adding a new item at a specific position
  • this needs 2 parameters position and element
  • adding a new item at the end
  • this needs 1 parameter element

15
Lists Implementation
  • Array
  • Means maximum size bounded list
  • Linked List
  • Any size unbounded list but requires links to
    both first and last elements

16
Time Complexities and Implementations
Array O(1) O(1) O(1) O(n) O(n)
Linked list O(n) O(n) O(1) O(n) O(n)
  • Operation
  • get
  • set
  • add at end
  • addanywhere
  • remove
Write a Comment
User Comments (0)
About PowerShow.com