Data Structures

Only available on StudyMode
  • Topic: Binary search tree, Graph theory, Tree
  • Pages : 73 (13763 words )
  • Download(s) : 99
  • Published : April 29, 2013
Open Document
Text Preview
Data Structures Class Notes
Reg Dodds Department of Computer Science University of the Western Cape c 2007 Reg Dodds rdodds@cs.sun.ac.za rdodds@uwc.ac.za

August 12, 2009

Fundamental Data Structures
• These slides form the core part of your courses COS224, COS 254 and COS234 and may be found on the Sun systems in the directory /export/home/notes/ds/ in the files —full size: ds-slides.ps, ds-slides.pdf, —4 to a page: 4up-ds-slides.ps, 4up-ds-slides.pdf. Read them. • Prescribed book in 2007: Adam Drozdek, 2004, “Data Structures and Algorithms in Java”, 2nd Ed., Thomson Learning. ISBN 0-619-21515-1. This book has been prescribed but the book suppliers let us down so we will revert to: • Prescribed in 2004–2006: A very good book. Michael T. Goodrich and Roberto Tamassia, 2006, “Data Structures and Algorithms in Java”, 4rd Ed. (2006) John Wiley and Sons. Inc. • A full set of 4-up slides are available for Goodrich and Tamassia’s book.

1

Shuffling Cards—obvious but bad method
• Start with a deck cards, represented by an array of integers {1, 2, . . . , 52}. Suppose the array is initialized as follows: for (i=1; i output $(DEP).class, $(LIST).class: javac -deprecation $(LIST).java javac -deprecation $(DEP).java clean: -rm *~ *.class errors output

48

Improved class linkedList
public class linkedList{ protected static int length; protected node head; public linkedList(){ head = new node (0, "head node"); length=0; } public void insert(node n){ n.next = head.next; head.next = n; length++; } public int length(){ return length; }

49

Improved class linkedList continued
public boolean isEmpty(){ return length==0; } public void clear(){ head.next = null; this.length = 0; } public void display(){ node here = this.head.next; while (here != null) { System.out.println( "Key = " + here.key + " Data = " + here.data); here = here.next; } System.out.println( "length=" + this.length); } } 50

The class node revised
This version of class node is not properly encapsulated— it is possible to access parts of the class that should be invisible from outside of the node class. public class node{ long key; String data; node next; public node(long k, String d){ key = k; data = d; next = null; } public node(){ key = 0; data = ""; next = null; } public String toString() { return "" } }

For example the insert method in the linkedList class can access the next next field directly. public void insert(node n){ n.next = head.next; head.next = n; length++; }

51

The class node revised
The fields inside node should be made private or protected and methods to get and set them should be supplied. public class node{ private long key; private String data; private node next;

public node(long k, String d){ key = k; public String getData() { data = d; return data; next = null; } } public node(){ key = 0; data = ""; next = null; } public void setKey(long k) { key = k; } public long getKey() { return key; } . . . } public node getNext() { return next; } public void setKey(long k) { key = k; } public void setData(String d) { data = d; } public void setNext(node n) { next = n; }

public long getKey() { return key; }

52

The class node revised
Now the linkedList method insert becomes
public void insert(node n){ \\ n.next = head.next; n.setNext(head.getNext); \\ head.next = n; head.setNext(n); length++; }

53

Stacks
• A stack behaves like a linked list where operations are allowed only at the head of the list. • Suppose that a stack variable called S is created by public static stack S; • Our stack will hold items of type node. • If n is of type node, then a new instance of a stack with n placed on the stack is created by S = new stack(n); • If m is a node, then it may be pushed onto the stack with S.push(m); S:

S:

Λ
stack

stack n stack

S:

m stack n stack

54

Stacks—a similar stack created differently
• Suppose that a stack variable called S is created by public static stack S; • If n is of type...
tracking img