Circular List - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Circular List

Description:

{ newNode.link = lastNode.link; lastNode.link = newNode; lastNode ... Memory bucks in JVM. Text Area code, static variables, literals. Stack method invocation ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 24
Provided by: boyi
Category:
Tags: bucks | circular | list

less

Transcript and Presenter's Notes

Title: Circular List


1
Circular List
removedNode
General Case
Single Node Case
lastNode
aNode
  • Insert
  • empty case
  • general case
  • Remove
  • empty case
  • single node case
  • general case

lastNode
Empty Case
lastNode
2
  • void insertAtLast(CListNode newNode)
  • if(lastNode null)
  • newNode.link newNode
  • else
  • newNode.link lastNode.link
  • lastNode.link newNode
  • lastNode newNode // update lastNode

3
  • CListNode removeLastNode()
  • CListNode tempNode null
  • if(lastNode! null)
  • CListNode previousNode findPreviousNode()
  • tempNode lastNode
  • if (previousNode ! lastNode) // generic case
  • previousNode.link lastNode.link
  • lastNode lastNode.link
  • else // single node case
  • lastNode null
  • // end if
  • tempNode.link null // break link to the
    list
  • // end if
  • return tempNode

4
Helper method
  • private CListNode findPreviousNode()
  • assert(lastNode ! null)
  • CListNode previousNode lastNode
  • while(previousNode.link ! lastNode)
  • previousNode previousNode.link
  • assert(previousNode.link lastNode)
  • return previousNode

5
search
  • public CListNode search(int key)
  • CListNode temp lastNode
  • if(lastNode null) return null
  • do if(temp.key key) return temp
  • temp temp.link
  • while(temp.next ! lastNode)
  • return null

6
COSC 1030 Section 7
  • Stack Queue

7
Topics
  • Stack and Queue ADTs
  • Different data representations
  • Implement Stack
  • Implement method invocation using stack
  • Implement Queue
  • Implement Tester

8
Stack ADT
  • Public interface Stack
  • // constructor create an empty stack
  • // returns true if the stack is empty,
    otherwise false
  • boolean isEmpty()
  • // pushes an item onto the stack
  • void push(Object item)
  • // pops the item on the top off the stack
  • // throw exception if stack is empty
  • Object pop() throws Exception
  • // peeks the item on the top without modifying
    stack
  • Object peek() throws Exception

9
Method Invocation
  • Memory bucks in JVM
  • Text Area code, static variables, literals
  • Stack method invocation
  • parameters
  • return address
  • return value
  • local variables
  • Heap - dynamic created objects
  • new List()
  • newInstance(java.util.Hashtable)

10
Method Invocation (2)
  • int result item.compareTo(another)
  • if(result 0)
  • local variables of compareTo
  • another
  • return address
  • return value

11
Heap
. ltItemgt ltItem2gt
Stack
ltfree spacegt temp another (param) Return
address Return value item item2
top
Text Area
result item.compareTo(item2) compareTo(Object
another) boolean temp return
temp
IP
12
Queue ADT
  • Public interface Queue
  • // constructor create an empty queue
  • // returns true if the queue is empty, false if
    not
  • boolean isEmpty()
  • // inserts an item to the rear of the queue
  • void insert(Object item)
  • // removes and returns the item in the front
    throws exception if empty
  • Object remove() throws Exception
  • // peeks the item at the front throws
    exception if the queue is empty
  • Object peek() throws Exception

13
Implementing Queue
  • List Representation
  • ListNode front
  • ListNode rear
  • Array Representation
  • Item itemArray
  • int front
  • int rear
  • int count

14
Queue Using List
  • Class ListQueue implement Queue
  • private ListNode front
  • private ListNode rear
  • private int count
  • public ListQueue()
  • front null // for remove and peek
  • rear null // for insert
  • count 0
  • boolean isEmpty()
  • return count 0

15
  • public void insert(Item newItem)
  • ListNode newNode new ListNode(newItem)
  • if (isEmpty())
  • front newNode
  • else
  • rear.setNext(newNode)
  • rear newNode
  • count

16
  • public Item peek() throws Exception
  • if(isEmpty()) throws new Exception()
  • return (Item) front.getInfo()
  • public Item remove() throws Exception
  • Item result peek() // may throw exception
  • if(count 1) // set rear to null
  • rear null
  • front front.getNext()
  • count --
  • return result

17
Queue Using Array
  • public class ArrayQueue implements Queue
  • private int front
  • private int rear
  • private int count
  • private int capacity
  • private final int CAPACITY_INC
  • private Item itemArray

18
  • public ArrayQueue()
  • front rear 0
  • count 0
  • capacity 10
  • CAPACITY_INC 5
  • itemArray new Itemcapacity
  • public boolean isEmpty()
  • return count 0

19
  • public void insert(Item newItem)
  • if(count capacity)
  • increaseCapacity()
  • itemArrayrear newItem
  • if(rear capacity) rear 0
  • count

20
  • private void increaseCapacity()
  • assert (front rear)
  • capacity CAPACITY_INC
  • Item tempArray new Itemcapacity
  • while(int j front j lt count j )
  • tempArrayjCAPACITY_INC itemArrayj
  • itemArrayj null
  • // moved first part of the queue to top of
    the array
  • front CAPACITY_INC
  • while(int I 0 I lt rear I )
  • tempArrayI itemArrayI
  • itemArrayI null
  • // moved second part of the queue to bottom
  • itemArray tempArray

21
  • public Item peek() throws Exception
  • if(count 0) throw new Exception()
  • return itemArrayfront
  • public Item remove throws Exception
  • Item temp peek()
  • itemArrayfront null
  • count --
  • front
  • if(front capacity) front 0
  • return temp

22
Complexity Analysis
23
Class QueueTester static String USAGE
Usage java QueueTester ltqueue impl class
namegt public static void main(String args)
Queue aQueue if(args.length 0)
York.println(USAGE) return try
aQueue (Queue) Class.newInstance(args0)
catch (Exception ex) York.println(ex.messag
e()) York.println(USAGE) return
aQueue.insert(red) aQueue.insert(green)
aQueue.insert(black) aQueue.insert(yellow)
aQueue.insert(blue) aQueue.insert(white)
while(aQueue .size() gt 0)
York.println(aQueue .remove()) // end of
while // end of main // end of QueueTester
Write a Comment
User Comments (0)
About PowerShow.com