Simple Dynamic Data (Linked Lists) - PowerPoint PPT Presentation

About This Presentation
Title:

Simple Dynamic Data (Linked Lists)

Description:

... the realities of a real language before discussing the Object Oriented details. ... For now we want to essentially get you to convert some of the Pseudocode ... – PowerPoint PPT presentation

Number of Views:124
Avg rating:3.0/5.0
Slides: 23
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Simple Dynamic Data (Linked Lists)


1
Simple Dynamic Data(Linked Lists)
2
The Ghost of Things to Come!
  • At this point were trying to get you to come to
    grips with the realities of a real language
    before discussing the Object Oriented details.
  • That will actually involve turning your mind
    inside out!
  • For now we want to essentially get you to convert
    some of the Pseudocode algorithms into
    Simplified Java

3
Java Records?
  • Java has no record
  • Instead Java will use classes to perform this
    function
  • For example, a classic student record becomes
  • class StudentRecord
  • String name
  • double gpa
  • Now for the big surprise

4
No static records
  • In Pseudocode we could say
  • sr isoftype StudentRecord
  • and/or
  • srp isoftype Ptr toa StudentRecord
  • srp lt- new(StudentRecord)
  • In Java there is no equivalent to
  • sr isoftype StudentRecord
  • In Java we must ALWAYS make objects which are
    dynamic allocations of data in this style
  • srp lt- new(StudentRecord)

5
In Summary
StudentRec definesa record name isoftype
String gpa isoftype Num endrecord // StudentRec
  • Static Allocation
  • ts isoftype StudentRec
  • Dynamic Allocation
  • tsp isoftype Ptr toa StudentRec
  • tsp lt- new(StudentRec)

Java has no equivalent
  • In Java
  • Classes replace records
  • References replace Pointers
  • Symbols required to tell the difference are not
    needed!

6
Java References
  • In pseudocode we said
  • srp isoftype Ptr toa StudentRecord
  • making srp a pointer
  • The equivalent in Java looks like this
  • StudentRecord srr
  • This is called a reference. It is in many ways
    just like a pointer. It points or refers to some
    dynamic memory which contains an Object

7
How do we make an Object?
  • In a very similar fashion to the way we made
    dynamic allocations
  • StudentRecord srr
  • srr new StudentRecord()
  • Now we can access the fields almost like
    Pseudocode
  • srr.name Bob Smith
  • srr.gpa 4.0 // Way to go Bob!
  • Why no
  • Because theres no need!!!

8
Compare Contrast
  • Pseudocode
  • StudentRec definesa record
  • name isoftype String
  • gpa isoftype Num
  • endrecord // StudentRec
  • srp isoftype Ptr toa StudentRec
  • srp lt- new(StudentRec)
  • srp.name lt- Bob
  • srp.gpa lt- 4.0
  • Java
  • Class StudentRec
  • String name
  • double gpa
  • StudentRec srr
  • srr new StudentRec()
  • srr.name Bob
  • srr.gpa 4.0

9
Some Details
  • Unlike Pseudocode we wont define these classes
    in the code like records.
  • Well need to create each class in its own file.
  • And the file name must exactly match the class
    name
  • class StudentRecord
  • String name
  • double gpa
  • Must be in a file named StudentRecord.java
  • And must be compiled (javac StudentRecord.java)
    before use

10
By now...
  • You must be dying to make a linked list!
  • Lets start with the Node (Well have it hold
    ints)
  • class LLNode
  • int data
  • LLNode next
  • See the similarity to the pseudocode version?

11
Pseudocode version
  • procedure addInOrder(cur iot in/out Ptr toa
    LLNode,
  • newData iot in Num)
  • temp isoftype Ptr toa LLNode
  • if(cur NIL OR cur.data gt newdata) then
  • temp lt- new(LLNode)
  • temp.data lt- newData
  • temp.next lt- cur
  • cur lt- temp
  • else
  • addinOrder(cur.next, newData)
  • endif
  • endprocedure // addInOrder

12
Convert to Java Bad Idea
  • public void addInOrder(LLNode cur, int newData)
  • LLNode temp
  • if(cur null cur.data gt newdata)
  • temp new LLNode()
  • temp.data newData
  • temp.next cur
  • cur temp
  • else
  • addinOrder(cur.next, newData)
  • // addInOrder

13
The Big Problem!
  • public void addInOrder(LLNode cur, int newData)
  • LLNode temp
  • if(cur null cur.data gt newdata)
  • temp new LLNode()
  • temp.data newData
  • temp.next cur
  • cur temp
  • else
  • addinOrder(cur.next, newData)
  • // addInOrder

Not an in/out parameter!
14
One Solution
  • public LLNode addInOrder(LLNode cur, int newData)
  • LLNode temp
  • if(cur null cur.data gt newdata)
  • temp new LLNode()
  • temp.data newData
  • temp.next cur
  • cur temp
  • return cur
  • else
  • addinOrder(cur.next, newData)
  • // addInOrder

15
Necessary Adjustments
  • public LLNode addInOrder(LLNode cur, int newData)
  • LLNode temp
  • if(cur null cur.data gt newdata)
  • temp new LLNode()
  • temp.data newData
  • temp.next cur
  • cur temp
  • return cur
  • else
  • cur.next addinOrder(cur.next, newData)
  • return cur // No change!!!
  • // addInOrder

16
How do I call this?
  • Before
  • addInOrder(head, 7)
  • Now
  • head addInOrder(head, 7)
  • Consider

42
84
96
head
3
92
98
head
17
Lets try a traversal
  • This will be simple since it didnt use in/out
    parameters!
  • public void traverse(LLNode cur)
  • if(cur ! null)
  • System.out.println(cur.data)
  • traverse(cur.next)
  • // if
  • // traverse

18
Complete Program
  • class LLDemo
  • public static LLNode addInOrder(LLNode cur,
  • int
    newData)
  • LLNode temp
  • if(cur null cur.data gt newData)
  • temp new LLNode()
  • temp.data newData
  • temp.next cur
  • cur temp
  • return cur
  • else
  • cur.next addInOrder(cur.next, newData)
  • return cur // No change!!!
  • // addInOrder

19
  • // class LLDemo (continued)
  • public static void traverse(LLNode cur)
  • if(cur ! null)
  • System.out.println(cur.data)
  • traverse(cur.next)
  • // if
  • // traverse
  • public static void menu()
  • System.out.println("Enter a to add")
  • System.out.println("Enter t to traverse")
  • System.out.println("Enter q to quit")
  • // menu

20
  • // class LLDemo (continued)
  • public static void main(String args)
  • LLNode head null
  • char response
  • String input
  • int newData

Continued
21
  • // class LLDemo (continued)
  • do
  • menu()
  • input IOGadget.readLine("Enter
    choice")
  • response input.charAt(0)
  • if(response 'a')
  • input IOGadget.readLine("Enter an
    int")
  • newData Integer.parseInt(input)
  • head addInOrder(head, newData)
  • else if(response 't')
  • System.out.println("Traversing")
  • traverse(head)
  • else if(response ! 'q')
  • System.out.println("What?")
  • while(response ! 'q')
  • // main

22
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com