ECE 242 Spring 2003 Data Structures in Java - PowerPoint PPT Presentation

1 / 82
About This Presentation
Title:

ECE 242 Spring 2003 Data Structures in Java

Description:

... student enters UMass, we insert him/her into this ... You can insert an item and delete an ... add() insert new nodes in Linked List. delete() remove node ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 83
Provided by: jhjh
Category:
Tags: ece | data | insert | java | spring | structures

less

Transcript and Presenter's Notes

Title: ECE 242 Spring 2003 Data Structures in Java


1
ECE 242 Spring 2003Data Structures in Java
  • http//rio.ecs.umass.edu/ece242
  • List
  • Prof. Lixin Gao

2
Todays Topics
  • A New Abstract Data Type List
  • Array Implementation of List
  • Limitation of Array Implementation
  • Motivation of Linked List Implementation
  • Description
  • Implementation
  • Application

3
Abstract Data Types
  • List
  • An ordered sequence of items.
  • insert and/or delete an item anywhere
  • More general than Stack and Queue
  • Stack and Queue are special cases of list.

4
Application Of List
  • Telephone Directory for UMass
  • When a new student enters UMass, we insert
    him/her into this list
  • When a student graduates from Umass, we remove
    him/her from the list
  • Database in a bookstore
  • Anything else ?

5
List
A list is a sequence of items. You can insert an
item and delete an item anywhere. And you can
retrieve or replace the entry in any
position. Stack and Queue are special cases of
list.
C
B
A
D
0
1
2
3
position
6
Operations On Lists
  • Basic Operations
  • Insert() Insert an item x at pos in List
  • Delete() Delete an item at pos in List
  • Size() Number of item in List
  • Retrieve() Search for a particular item in the
    List
  • Other useful operations
  • Traverse all items in List
  • Update an item value in List

7
Insert/Delete An Item In List
Insert C at the position 2
D
B
A
E
C
B
A
D
E
0
1
2
3
0
1
2
3
4
Delete item B at position 1
D
C
A
E
C
B
A
D
E
0
1
2
3
0
1
2
3
4
8
Implementation of List
  • There are many possible implementations
  • Array Implementation of List
  • Use an array of Object
  • Object array

9
Array Implementation of List
  • Use an array to implement List

Max_Size-1
n-1
  • Main Operations
  • Insert(pos, item)
  • Delete(pos)



4
3
count the number of items in the list
C
2
B
1
A
0
10
Array Implementation Insert Operation
Operation insert(2, C)
maxsize-1
maxsize-1
maxsize-1
Count6
F
F
5
Count5
4
F
4
E
4
E
Move 2
Insert C
E
D
D
3
3
3
in pos 2
3, 4 up
2
D
2
2
C
1
B
1
B
1
B
A
0
A
0
A
0
11
Array Implementation Insert Operation
  • Move items from pos to count-1 one position up
  • Put new item in pos
  • Increment count by one

12
Array Implementation Code for Insert Operation
  • public void insert(int pos, Object item)
  • if( countltarray.length pos gt0 pos lt
    count)
  • //shift one position up
  • for( int icount-1 igtpos i-- )
  • arrayi1arrayi
  • arraypositem
  • count
  • else
  • throw new IllegalArgumentException (pos is not
    correct")

13
Array Implementation Delete An Item
Operation delete(2)
maxsize-1
maxsize-1
Count5
Count4
4
E
Remove an item
D
E
3
3
pos2
at position 2
pos
2
C
2
D
1
B
1
B
A
0
A
0
14
Array Implementation Delete An Item
  • Shift items from pos1 to count-1 down by one
    position
  • Decrement count by one

15
Array Implementation Code For Deleting An Item
  • public void delete(int pos)
  • if( countltarray.length pos gt0 pos ltcount)
  • for(int ipos1iltcount-1i)
  • listi-1listi
  • count--
  • else
  • throw new IllegalArgumentException (pos
    is not correct")

16
Limitation of Array Implementation
  • Need to move items Costly
  • Need to know the max size of list
  • Too large waste of memory
  • Too small cant expand dynamically

17
Ideal Implementation
  • Take as much memory as needed
  • no more and no less
  • allocate the memory dynamically if needed
  • No need to move items around

18
Another Implementation of List
  • Linked List

b
a
c
null
19
Linked List Demos
  • A Linked List Java applet
  • http//www.unf.edu/rzucker/javadir/ronslist.html
  • A song about Linked List
  • http//www.engr.mun.ca/theo/Courses/ds/gilbert.ht
    ml
  • Another Linked List Java applet
  • http//www.cs.stir.ac.uk/mew/dissertation/swingsi
    m.htm

20
Advantage of Linked List
  • Nodes can be located anywhere in the memory.
  • Getting from one node to another by storing the
    reference of the next node
  • Insertion/removal does not require moving other
    items

b
a
c
d
e
null
21
Simple Linked List
  • Singly Linked List (SLList)
  • A sequence of nodes

b
a
c
null
head
Node its value is a, this node also has a link
which points to its succeeding node
22
Node
  • A basic element in Linked List
  • Node contains
  • items or values
  • references to next Node

Node
b
a
value
link
23
Example for Node
  • Each node contains Employee record
  • name
  • telephone
  • next reference to next node

next
name
telephone
value
reference to next node
24
Node Definition
  • Define an abstract object Node
  • private class Node
  • private Object item
  • Node next
  • Example for Employee Node
  • private class Node
  • Employee e
  • Node next

25
Node Definition
  • For simplicity, use String data type instead of
    Employee Object
  • private class Node
  • private String name
  • private String tel
  • Node next

26
Singly Linked List --- head/tail
  • Important variables in Singly Linked List
  • head Indicates the beginning of the List
  • tail Indicates the end of the List

tail
head
node1
node2
node3
27
Last Nodes Next --- null
  • In the list as below
  • two nodes, node a and node b
  • node as next node is node b
  • node b is the last node
  • node bs next is null

null
b
a
28
Implementing SLList for Umass Telephone Directory
  • All information is sorted by students name
  • Each student is denoted by one node
  • name and telephone
  • Suppose student names are unique

29
Interface for SLList
  • public interface InterfaceSLList
  • public void add(String name, String tel)
  • public void delete(String name)
  • public void search(String name)
  • public void printall()
  • public int size()

30
Node Class
  • private class Node
  • private String name // students name
  • private String tel // students
    telephone
  • private Node next
  • // constructor
  • private Node(String n, String t)
  • name n
  • tel t
  • next null

31
SLList Implementation
  • public class TelDirectorySLList implements
    InterfaceSLList
  • // nesting private class Node
  • private class Node
  • . .
  • // important variables head and tail
  • private Node head, tail
  • // constructor method
  • public TelDirectorySLList()
  • head tail null

32
Operations on Linked List
  • add() insert new nodes in Linked List
  • delete() remove node from Linked List
  • printall() traverse the entire Linked List
  • search() locate wanted nodes in Linked List
  • size() return number of nodes in Linked List

33
Printall() Method In Listed Link
  • For an empty list, return empty list
  • For a nonempty list
  • Print out all the nodes one by one from head

tail
head
node1
node2
node3
34
Code For Printall() method
  • public void printall()
  • if( headnull ) // the list is empty
  • System.out.println("this list is
    empty")
  • return
  • Node probe head
  • System.out.println("\nAll items in the
    list are ")
  • do
  • System.out.println(probe.name "'s
    phone is " probe.tel )
  • probe probe.next
  • while ( probe!null )

35
Search() Method In Listed Link
  • For an empty list, return empty list
  • For a nonempty list
  • Search for the wantedNode from head, print the
    record if matched

tail
head
wantedNode
node1
node2
node3
36
Code For Search() Method
  • public void search(String name)
  • //return when the linked list is empty
  • if( headnull )
  • System.out.println("this list is
    empty")
  • return

37
Code For Search() Method
  • Node probe head
  • do
  • if( probe.name.equals(name) )
  • System.out.println(name "'s
    telephone is " probe.tel)
  • return
  • probe probe.next
  • while ( probe!null )
  • System.out.println( name " is not in
    the list")

38
Size() Method In Listed Link
  • For an empty list, return size 0
  • For a nonempty list
  • count the total number of nodes in the Linked
    List

tail
head
node1
node2
node3
39
Code For Size() Method
  • public int size()
  • if( headnull )
  • return 0
  • Node probe head
  • int count 0
  • do
  • count count 1
  • probe probe.next
  • while ( probe!null )
  • return count

40
Add New Node --- Case 1
  • Case 1 for an empty list
  • set head and tail to newNode

head
newNode
tail
41
Add New Node --- Case 2
  • Case 2 for a non-empty list, find where newNode
    should be inserted
  • (a) insert in front of head
  • (b) insert in the middle of list
  • (c) insert after tail

tail
head
node1
node2
node3
42
Add New Node --- Case 2 (a)
  • (a) Insert newNode in front of head

43
Add New Node --- Case 2 (a)
  • (a) Insert newNode in front of head

newNode
44
Add New Node --- Case 2 (a)
  • (a) Insert newNode in front of head

newNode
45
Add New Node --- Case 2 (a)
  • (a) Insert newNode in front of head

newNode
46
Add New Node --- Case 2 (b)
  • (b) Insert newNode in the middle of list
  • Suppose newNode needs to be inserted between
    node 2 and node 3

47
Add New Node --- Case 2 (b)
  • (b) Insert newNode in the middle of list
  • Suppose newNode needs to be inserted between
    node 2 and node 3

newNode
48
Add New Node --- Case 2 (b)
  • (b) Insert newNode in the middle of list
  • Suppose newNode needs to be inserted between
    node 2 and node 3

newNode
49
Add New Node --- Case 2 (b)
  • (b) Insert newNode in the middle of list
  • Suppose newNode needs to be inserted between
    node 2 and node 3

newNode
50
Add New Node --- Case 2 (c)
  • (c) Insert newNode after tail

51
Add New Node --- Case 2 (c)
  • (c) Insert newNode after tail

newNode
52
Add New Node --- Case 2 (c)
  • (c) Insert newNode after tail

newNode
53
Add New Node --- Case 2 (c)
  • (c) Insert newNode after tail

tail
newNode
54
Code For Add() Method
  • public void add(String name, String tel)
  • // create a new node for name
  • Node newNode new Node(name, tel)
  • // case 1 the list is empty
  • if (head null)
  • head newNode
  • tail newNode

55
Code For Add() Method (Cont.)
  • //case 2 find the node pos after which we insert
    the new node
  • Node pos null
  • Node probe head
  • while( probe.name.compareTo(newNode.name)lt
    0 )
  • pos probe
  • if( probe.next!null)
  • probe probe.next
  • else
  • break

56
Code For Add() Method (Cont.)
  • // case 2(a) new node should be inserted in
    front of head
  • if( posnull )
  • newNode.next head head newNode
  • // case 2(b) new node should be inserted in the
    middle of list
  • else if( pos.next!null )
  • newNode.next pos.next pos.next
    newNode
  • // case 2(c) new node should be inserted after
    tail
  • else
  • pos.next newNode tail newNode
    newNode.next null

57
Delete() Method --- Case 1
  • Case 1, for an empty list
  • Could not delete any item from an empty list
  • Print out the error message

58
Delete() Method --- Case 2
  • Case 2, for a non-empty list
  • find the location of wantedNode, remove it from
    the list
  • (a) wantedNode is both head and tail
  • (b) wantedNode is head, but not tail
  • (c) wantedNode is not head, not tail
  • (d) wantedNode is not head, but tail
  • (e) wantedNode is not found in this list

59
Delete Method --- Case 2 (a)
  • (a) wantedNode is both head and tail

head
headnull
after deletion
node 1
tail
tailnull
60
Delete Method --- Case 2(b)
  • (b) wantedNode is head, but not tail
  • Suppose node 1 needs to be deleted

61
Delete Method --- Case 2(b)
  • (b) wantedNode is head, but not tail
  • Suppose node 1 needs to be deleted

tail
node2
node3
node1
62
Delete Method --- Case 2(b)
  • (b) wantedNode is head, but not tail

tail
head
node2
node3
node1
63
Delete Method --- Case 2(c)
  • (c) wantedNode is not head, not tail
  • Suppose node2 needs to be deleted

tail
head
node1
node2
node3
To be deleted
64
Delete Method --- Case 2(c)
  • (c) wantedNode is not head, not tail
  • Suppose node2 needs to be deleted

tail
head
node1
node2
node3
To be deleted
65
Delete Method --- Case 2(c)
  • (c) wantedNode is not head, not tail
  • Suppose node2 needs to be deleted

tail
head
node1
node3
66
Delete Method --- Case 2(d)
  • (d) wantedNode is not head, but tail

tail
head
node2
node3
node1
67
Delete Method --- Case 2(d)
  • (d) wantedNode is not head, but tail

tail
head
node2
node3
node1
68
Delete Method --- Case 2(d)
  • (d) wantedNode is not head, but tail

tail
head
node2
node1
69
Delete Method --- Case 2(e)
  • (e) wantedNode is not found in this list
  • Could not find this item in the list
  • Print out the error message

70
Code For Delete() Method
  • public void delete(String name)
  • // case 1 the linked list is empty
  • if( headnull )
  • System.out.println("this list is
    empty")
  • return

71
Code For Delete() Method (Cont.)
  • Node pos null
  • Node probe head
  • // find the node to be deleted
  • while( probe.name.compareTo(name)!0 )
  • pos probe
  • if( probe.next!null)
  • probe probe.next
  • else
  • break

72
Code For Delete() Method (Cont.)
  • // case 2(a) only one item is in the list,
  • // which needs to be deleted
  • if( posnull tailhead )
  • tail head null
  • // case 2(b) more than one item in the list,
  • // head needs to be deleted
  • else if( posnull tail!head )
  • head head.next

73
Code For Delete() Method (Cont.)
  • // case 2(c) the middle node in the list needs
    to be deleted
  • else if( pos!tail probe!tail )
  • pos.next probe.next
  • // case 2(d) the tail needs to be deleted
  • else if( pos!tail probetail )
  • tail pos pos.next null
  • // case 2(e) could not find name in the list
  • else
  • System.out.println( name " is not
    in the list")

74
Complete Examples
  • Interface for Simply Linked List
  • InterfaceSLList.java
  • Telephone Directory Simply Linked List
  • TelDirectorySLList.java

75
Summary of Linked List
  • Size grows as needed
  • Data manipulation is efficient
  • Add, remove, etc.
  • Linked List implementation is more efficient than
    array

76
Application of Linked List
  • Telephone Directory Example
  • Records students/faculties/staffs and their
    telephone numbers.
  • Operations like add/remove/search are necessary
    and frequent
  • Other useful information could be added if
    necessary
  • Address, personal profile, etc.

77
Create A Linked List
  • public class ApplicationSLList
  • public static void main(String args)
  • TelDirectorySLList db new
    TelDirectorySLList ()
  • EasyIn ei new EasyIn()
  • String name, tele
  • //add several records in the
    telephone directory
  • db.add("Mike", "645-2353")
  • db.add("Tom", "877-0908")
  • db.add("Gilbert", "233-8867")
  • System.out.println("\nAll the records
    in the database are")
  • db.printall()

78
Add Records In List
  • //add a new record into the telephone directory
  • System.out.print(\nPlease input the name of a
    new record ")
  • name ei.readString()
  • System.out.print("Please input the telephone of
    a new record ")
  • tele ei.readString()
  • db.add(name, tele)

79
Print/Delete Records In List
  • //list all the records in the directory
  • System.out.println("\nAll the records in the
    database are")
  • db.printall()
  • System.out.println("")
  • //delete a record from the directory
  • System.out.print("Type the name of record you
    want to delete ")
  • name ei.readString()
  • db.delete(name)

80
Search Records In List
  • //list all the records in the directory
  • System.out.println("All the records in the
    database are")
  • db.printall()
  • //search a record in the directory
  • System.out.print("Type the name of the record you
    look for ")
  • name ei.readString()
  • db.search(name)
  • System.out.println("Press enter to continue.")
  • ei.readString()

81
The Number Of Records In List
  • //list all the records in the directory
  • System.out.println("All the records in the
    database are")
  • db.printall()
  • System.out.println("")
  • //print out the size of the directory at that
    time
  • System.out.println("The size of the database
    is")
  • System.out.println("Size " db.size())

82
Complete Examples
  • Interface for Simply Linked List
  • InterfaceSLList.java
  • Telephone Directory Simply Linked List
  • TelDirectorySLList.java
  • Application for Simply Linked List
  • ApplicationSLList.java
Write a Comment
User Comments (0)
About PowerShow.com