Queues The Queue Interface PowerPoint PPT Presentation

presentation player overlay
1 / 9
About This Presentation
Transcript and Presenter's Notes

Title: Queues The Queue Interface


1
QueuesThe Queue Interface
public int size()? public boolean
isEmpty()? public E front()? public enqueue(E
element)? public E dequeue()?
  • __len__(self) (len(queue))?
  • is_empty(self)?
  • front(self)?
  • enqueue(self, element)?
  • dequeue(self)?

2
Queues Circular Array Implementation
Constructor public CircularArrayQueue()
head ? 0 // where is the 1st // elt. tail
? 0 count ? 0 // no. items in q Q ? (E) new
Object1
def __init__(self) self.head 0 where is the
1st elt. self.tail 0 self.count 0
no. items in q self.Q None
3
QueuesCircular Array Implementation
public int size() return count public
boolean isEmpty() return (count
0) public E front() if (isEmpty())?
throw EmptyQueueException return
Qhead public E dequeue() if
(isEmpty())? throw EmptyQueueException E e ?
Qhead head if (head Q.length)? head
? 0 --count return e
def __len__(self) return self.count def
is_empty(self) return (self.count 0)? def
front(self) if self.is_empty() raise
EmptyQueueException return self.Qself.head d
ef dequeue(self) if self.is_empty() raise
EmptyQueueException e self.Qself.head self.h
ead 1 if self.head len(self.Q) self.head
0 self.count - 1 return e
4
QueuesCircular Array Implementation
public void enqueue(E element) if (count
Q.length) // grow the queue Qnew ? (E)
new Object2 Q.length for i ? 0 to count
- 1 index ? (head i)? MOD
count Qnewi ? Qindex head ? 0
tail ? count Q ? Qnew Qtail ?
element tail if (tail Q.length)?
tail ? 0 count
def enqueue(self, element) if self.count
len(self.Q) grow the queue Qnew None
for x in range( \ 0, 2len(self.Q)) for i
in range(0,self.count) index (self.head
\ i) self.count Qnewi
self.Qindex self.head 0 self.tail
self.count self.Q Qnew self.Qself.tail
element self.tail 1 if self.tail
len(self.Q) self.tail 0 self.count 1
5
Lists The List Interface
void add(int index, Object o)? void add(Object
o)? boolean addAll(Collection c)? boolean
addAll(int i, Collection c)? void
clear()? boolean contains(Object o)? boolean
containsAll(Collection c)? boolean equals(Object
o)? Object get(int index)? int indexOf(Object
o)? boolean isEmpty()? Iterator iterator()? int
lastIndexOf(Object o)? ListIterator
listIterator()? ListIterator listIterator(int
index)? Object remove(int index)? boolean
remove(Object o)? boolean removeAll(Collection
c)? boolean retainAll(Collection c)? Object
set(int index, Object o)? int size()? List
subList(int from, int to)? Object
toArray()? Object toArray(Object a)?
append(self, o)? count(self, value)? extend(self,
iterable)? index(self, x, start,
stop)? insert(self, index, o)? pop(self,
index)? remove(self, o)? reverse(self)? sort(self,
cmp, key, reverse)? ? __eq__(self, o)
(L K)? __contains__(self, x) (x
in L)? __getitem__(self, y)
(Ly)? __getslice__(self, x, y)
(Lxy)? __setitem__(self, i, o) (Li
o)? __setslice__(self, i, j, K)
(LijK)? __delitem__(self, y) (del
Ly)? __delslice__(self, i, j) (del
Lij)? __len__(self)
(len(L))? __add__(self, K) (z L
K)? __mul__(self, x, n) (z L
n)? __iter__(self) (iter(L))?
6
ListsArray Lists
Implementation store elements in an array S.
void add(Object e)? if (t
S.size()-1)? throw ListFullException t St
? e void add(int i, Object e)? if (t
S.size()-1)? throw ListFullException for x ?
S.size()-2 down to i Sx1 ?
Sx t Si ? e
def append(self, e) if self.t
len(self.S)-1 raise ListFullException self.t
1 self.Sself.t e
def insert(self, i, e) if self.t
len(self.S)-1 raise ListFullException for x
in range(S.size()-2, \ i-1, -1)
self.Sx1 self.Sx t 1 self.Si e
7
ListsSingly-Linked List
Core implementation Node class Node Node
next Object data ... class SLList
Node first ? null Node last ? null
public SLList() ...
Core implementation Node class Node(object)
def __init__(self, n, d) self.__next n
self.__data d ... ... class
SLList(object) def __init__(self) self.__firs
t None self.__last None ...
8
ListsSingly-Linked List
insertAfter(Object o, node where)? Node
newNode ? new Node() newNode.data ? o
newNode.next ? where.next where.next ?
newNode remove(Object o)? if (first
null)? throw ListEmptyException if (o
first.data)? first ? first.next else Node
srch ? first while (srch.next ? null
and srch.next.data ? o)? srch ?
srch.next if (srch.next null)? throw
NoSuchObjectEx srch.next ? srch.next.next
def insert_after(self, o, where) newNode
Node()? newNode.data o newNode.next
where.next where.next newNode def
remove(self, o) if self.first is None raise
ListEmptyException if o self.first.data sel
f.first self.first.next else srch
self.first while srch.next is not None\
and srch.next.data ! o srch
srch.next if srch.next is None raise
NoSuchObjectEx srch.next srch.next.next

9
ListsDoubly-Linked List
Core implementation Node class Node Node
next Node prev Object data ... class
DLList Node first ? null Node last ?
null public DLList() ...
Core implementation Node class Node(object)
def __init__(self, n, \ p, d) self.__next
n self.__prev p self.__data d
... ... class DLList(object) def
__init__(self) self.__first
None self.__last None ...
Write a Comment
User Comments (0)
About PowerShow.com