Sunteți pe pagina 1din 27

CSC248

FUNDAMENTALS OF
DATA STRUCTURES

Topic 3:
Linked List (Part 2)
LINKED LIST OPERATIONS
(COUNTING - SIZE)
Counting all data
Traversing a linked list
// traverse the whole list and count all data
public int size()
{
int size = 0;
Node ptr ; // the external pointer

ptr = head; // start at the first node


while (ptr !=null )
{ size++;
ptr = ptr.next;
}

return size;
}
Test Yourself

 Apply the LinkList class to manipulate the Fruit class as


below.

LinkList fruitList = new LinkList ()

1. Add the fruit objects into fruitList until the user


wants to stop.
2. Count how many fruits have been stored.
LINKED LIST OPERATIONS
(ACCESS & TRAVERSE)
Display all data
Traversing a linked list
• an external pointer is usually used to access nodes in the
list from the first until the last
// traverse the whole list and display all data - specific data ONLY
public void printAll()
{
Node ptr ; // the external pointer

ptr = head; // start at the first node

while (ptr!=null)
{ System.out.println(ptr.data.toString());
ptr = ptr.next;
}
}
LINKED LIST OPERATIONS
(ACCESS & RETRIEVE)
Methods for retrieval
• These methods will use the current data field
public class LinkList
{ private class Node
{ ….
}
Node head;
Node current; // this field will be used

}
• Node current is used to point to the current node
being processed
Retrieve the front
Retrieve data at the front of the list

// returns the first data in the list and


// sets current to point to the second node

public Object getFirst()


{ if (isEmpty())
return null;
else
{ current = head.next;
return head.data;
}
}
Retrieve the next (current) node
Retrieve data of next (current) node
// gets the data in the node pointed by current
// and points current to the next node

public Object getNext ()


{
Object info = null;

if (current != null)
{ info = current.data;
current = current.next;
}

return info;
}
Retrieve after a specific node
Retrieve data after a node
// gets the data in the node pointed by specified node
// and points current to the next node

public Object getNext (Node p)


{
Node x = null; //as the next node
Object info = null;

if (p != null)
{
x = p.next;
info = x.data;
}

return info;
}
Retrieve the last
Test Yourself - Retrieve

1. Prepare the steps for following method:


a) Retrieve data from the front of the list. (as discussed)
b) Retrieve data after a node. (as discussed)
c) Retrieve data after the specified node. (as discussed)
d) Retrieve data from the last of the list.

2. Define method Object getLast()

3. ** getLast() is similar to getBack()


Retrieve data at the last of the list
public Object getLast ()
{
Object info = null;

if (isEmpty())
{
System.err.println(“The list is empty");
return info;
}
else
{
Node ptr = head; //last node

while (ptr.next !=null) //finding the last node


{
ptr = ptr.next;
}

info = ptr.data;
return info;
}
}
GENERIC CLASS
VS
SPECIFIC CLASS
• if the LinkList class has been designed to
be generic (ie. the data type is Object),
only generic methods could be written
• if the LinkedList class has been declared
specifically for one type of data eg. Student
data, specific methods to process Student
data in class LinkList can be written

public class LinkList


{ private class Node
{ Student data;
Node next;
}
Node head;

}
LINKED LIST
APPLICATION
Eg. Using the methods in an application
// in the main() method
LinkList list = new LinkList();
…….
// get some data and put into the list
for (int j=0; j<10; j++)
{ System.out.println(“Enter a name “);
name = input.nextLine();
list.insertFront(name);
}
// display all the data in the list
list.printAll();
Eg. Using the generic traversal methods in an
Application
// to display all data in list
// (assuming list is a LinkList and it already has Student data)

Student stu = (Student) list.getFirst();

while (stu != null)


{
System.out.println(stu.toString());
stu = (Student) list.getNext();
}
LinkedList Application

Other Applications of Linked List

 Searching - search a particular element in list


 Sorting - sort the elements in the list (insert
elements at the right location in ascending or
descending order)
END

S-ar putea să vă placă și