Presentation is loading. Please wait.

Presentation is loading. Please wait.

Circular List removedNode General Case Single Node Case lastNode aNode

Similar presentations


Presentation on theme: "Circular List removedNode General Case Single Node Case lastNode aNode"— Presentation transcript:

1 Circular List removedNode General Case Single Node Case lastNode aNode
Insert empty case general case Remove single node 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 return null; 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 …. <Item> …… <Item2> Stack <free space> 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 Array 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 Item[capacity]; } public boolean isEmpty() { return count == 0;

19 public void insert(Item newItem) {
if(count == capacity) { increaseCapacity(); } itemArray[rear++] = newItem; if(rear == capacity) rear = 0; count ++;

20 private void increaseCapacity() {
assert (front == rear); capacity += CAPACITY_INC; Item[] tempArray = new Item[capacity]; while(int j = front; j < count; j ++) { tempArray[j+CAPACITY_INC] = itemArray[j]; itemArray[j] = null; } // moved first part of the queue to top of the array front += CAPACITY_INC; while(int I = 0; I < rear; I ++) { tempArray[I] = itemArray[I]; itemArray[I] = null; } // moved second part of the queue to bottom itemArray = tempArray; }

21 public Item peek() throws Exception {
if(count == 0) throw new Exception(…); return itemArray[front]; } public Item remove throws Exception { Item temp = peek(); itemArray[front] = null; count --; front ++; if(front == capacity) front = 0; return temp;

22 Complexity Analysis isEmpty push peek pop List Stack O(1) Array Stack
O(1) average O(n) worst case isEmpty insert peek remove List Queue O(1) Array Queue O(1) average O(n) worst case

23 Class QueueTester { static String USAGE = “Usage: java QueueTester <queue impl class name>”; public static void main(String[] args) { Queue aQueue; if(args.length == 0) { York.println(USAGE); return; } try { aQueue = (Queue) Class.newInstance(args[0]); } catch (Exception ex) { York.println(ex.message()); 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() > 0) { York.println(aQueue .remove()); } // end of while } // end of main } // end of QueueTester


Download ppt "Circular List removedNode General Case Single Node Case lastNode aNode"

Similar presentations


Ads by Google