Data Structures - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Data Structures

Description:

Vectors are dynamic (can grow and shrink) but do so inefficiently ... books.add ('Jonathan Livingston Seagull'); books.add ('A Tale of Two Cities' ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 26
Provided by: joseph120
Category:

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures
  • Joe Komar

2
Dynamic Structures
  • Arrays are static in the sense they do not change
    their size
  • Vectors are dynamic (can grow and shrink) but do
    so inefficiently
  • We can use references to create dynamic
    structures of many types
  • We will look at
  • Linked Lists
  • Queues
  • Stacks

3
Linked Lists
  • Use references to objects as part of an object to
    link objects in a list

class Node int stuff Node next_node
4
Abstract Data Types (ADT)
  • An ADT is a collection of data and the particular
    operations that are allowed on that data
  • It hides the implementation details behind a
    well-defined interface
  • Java objects are well suited to defining Abstract
    Data Types (encapsulation is excellent)

5
Library2 Linked List
public class Library2 public static void
main (String args) Book_List books
new Book_List() books.add ("The
Hitchhiker's Guide to the Galaxy")
books.add ("Jonathan Livingston Seagull")
books.add ("A Tale of Two Cities")
books.add ("Java Software Solutions")
books.print() try System.in.read()
catch (Exception e) // method
main // class Library2
6
Library2 (contd)
class Book_List private Book list
Book_List () list null //
constructor
7
Library2 (contd)
public void add (String new_title)
Book new_book new Book (new_title)
Book current if (list null)
list new_book else
current list while
(current.get_next() ! null)
current current.get_next()
current.set_next (new_book) //
method add
8
Library2 (contd)
public void print () Book current
list while (current ! null)
current.print() current
current.get_next() // method
print // class Book_List
9
Library2 (contd)
class Book private String title
private Book next Book (String new_title)
title new_title next null
// constructor public Book get_next()
return next // method get_next
10
Library2 (contd)
public void set_next (Book next_book)
next next_book // method set_next
public void print () System.out.println
(title) // method print // class Book
11
Library2 Output
12
Variations on Lists
  • Removing items from the list
  • Inserting items into the list
  • Bi-directional links (previous and next)
  • Header link -- reference to the beginning
  • End link -- reference to the last item in the list

13
Queues
  • First-in, First-out (FIFO)

33 12 15 24 56
14
QTrek Example
class QTrek public static void main
(String args) Queue ship new
Queue() if (ship.empty())
System.out.println ("Nobody Home!")
ship.enqueue ("Pickard") ship.enqueue
("Data") ship.enqueue ("Crusher")
if (ship.empty ()) System.out.println
("Still nobody home!")
15
Qtrek (contd)
System.out.println (ship.dequeue())
System.out.println (ship.dequeue())
System.out.println (ship.dequeue()) if
(ship.empty()) System.out.println
("Every person for herself!") try
System.in.read() catch (Exception e)
// method main // class QTrek
16
Qtrek (contd)
class Queue Queue_Item first, last
boolean empty() return first null
// method empty void enqueue (Object
item) Queue_Item new_item new
Queue_Item (item) if (first ! null)
last.set_next (new_item)
last new_item else last
new_item first last
// method enqueue
17
Qtrek (contd)
Object dequeue () if (first ! null)
Object result first.get_item()
first first.get_next()
return result else
return null // method dequeue //
class Queue
18
Qtrek (contd)
class Queue_Item private Object item
private Queue_Item next Queue_Item (Object
qitem) item qitem next
null // constructor
19
Qtrek (contd)
Object get_item() return item //
method get_item Queue_Item get_next ()
return next // method get_next
void set_next (Queue_Item qitem) next
qitem // method set_next // class
Queue_Item
20
Qtrek Output
21
Stacks
  • Last-in, First-out (LIFO)

13 23 43 11 44
22
Java.util API Class Stack
  • There is a built in class in java.util called
    Stack
  • public Object push (Object obj)
  • public Object pop ()
  • public Object peek ()
  • public boolean empty ()
  • public int search (Object obj)
  • Stack class is derived from the Vector class

23
Decode Example
import java.io. import java.util.Stack public
class Decode public static void main
(String args) throws IOException
BufferedReader stdin new BufferedReader
(new InputStreamReader (System.in))
Stack word new Stack() String
message int index 0
System.out.println ("Enter the coded message")
message stdin.readLine()
System.out.println ("The decoded message is")
24
Decode (contd)
while (index lt message.length()) while
(index lt message.length() message.charAt
(index) ! ' ') word.push
(new Character(message.charAt(index)))
index while
(!word.empty()) System.out.print
(((Character)word.pop()).charValue())
System.out.print (" ") index
System.out.println () try
System.in.read() catch (Exception e)
// method main // class Decode
25
Decode Output
Write a Comment
User Comments (0)
About PowerShow.com