# Data Structures

Topics: Binary search tree, Graph theory, Tree Pages: 73 (13763 words) Published: April 29, 2013
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 ﬁles —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

• 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

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

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 ﬁeld directly. public void insert(node n){ n.next = head.next; head.next = n; length++; }

51

The class node revised
The ﬁelds 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

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 diﬀerently
• Suppose that a stack variable called S is created by public static stack S; • If n is of type...