Lecture 04 - Linked Lists - Arrays, ArrayList, LinkedList
Lecture 04 - Linked Lists - Arrays, ArrayList, LinkedList
Lecture # 04:
Linked Lists :
Arrays, ArrayList and
LinkedList
1
© Slides adapted from the textbook, ICS 202 slides from KFUPM and/or CS 211 slides from Taibah University (Ms. Rana & Mr. Modhi) and/or many other online resources.
Outline
• Introduction
• Arrays
• ArrayList and LinkedList classes
• Linked Lists
– Singly Linked List
– Circular Linked Lists
– Doubly Linked List
2
Introduction
• List – A sequence of elements in certain
linear order
A B C
Head
3
• Basic operations:
Inserting
Deletion
Traversing
Searching
• For example, we can insert / add a node anywhere
into the list:
1.Empty List
2. Before head
3. After tail
4. In between
4
Arrays
An array is a sequenced collection of elements, normally
of the same data type
Some programming languages accept arrays in which elements
are of different types
5
Arrays - Declaration
• Declared using [ ] operator
1.datatype[] arrayRefVar;
Example:
double[] myList;
2.datatype arrayRefVar[];
Example:
double myList[];
3.datatype arrayRefVar[] = {list items};
Example:
int[] height = {2, 34, 45, 22, 235};
6
Arrays - Memory Allocation
• In the definition:
– int [ ] tests;
7
Arrays - Anatomy
In the definition:
– int [ ] tests;
– tests = new int[length];
8
Array - Examples
Once an array is created, its size is
fixed. It cannot be changed. You
can find its size using
arrayRefVar.length
,For example
myList.length
will returns 10 //
9
Multi-Dimensional Arrays
Arrays may be one-dimensional or multi-dimensional
arrays
In a one-dimensional array data are organized linearly in only
one direction
Many applications require that data be stored in more than one
dimension. The Figure below shows a table, which is commonly
called a two-dimensional array
10
Example-1:
Find sum of all
elements in a 2-d
array input by the
user.
11
Operations on Array
Although we can apply conventional operations defined for each element of
an array, there are some operations that we can define on an array as a
data structure.
The common operations on arrays as structures are searching, insertion,
deletion, retrieval and traversal.
Although searching, retrieval and traversal of an array is an easy job,
insertion and deletion is time consuming.
The elements need to be shifted down before insertion and shifted up after
deletion.
An array is a suitable structure when a small number of insertions and
deletions are required, but a lot of searching and retrieval is needed.
12
Arrays - Pros & Cons
Pros:
Fast element access
Cons:
Static arrays are impossible to resize
Required size may not be always available in adjacent
form
13
ArrayList class in Java
• ArrayList is an array implementation of a list.
14
* ArrayList was briefly studied with an example program in Lecture 02 as well
Example-2:
import java.io.*; list1.add(new Integer(4));
import java.util.*; list1.retainAll(list2);
class TestArrayList { // intersection: [4, 5, 6, 4] , [3, 4, a, 1.1] ==> [4, 4]
public static void main(String[] ar) { System.out.println(list1);
ArrayList list1 = new ArrayList(); list1.add(1,new Integer(5));
list1.add(new Integer(4)); list1.add(2,new Integer(6));
list1.add(new Integer(5)); list1.addAll(list2);
list1.add(new Integer(6)); // union:
list1.add(new Integer(4)); // [4, 5, 6, 4] , [3, 4, a, 1.1] ==> [4, 5, 6, 4, 3, 4, a, 1.1]
ArrayList list2 = new ArrayList(4); System.out.println(list1);
list2.add(new Integer(3)); List list3 = list1.subList(2,5);
list2.add(new Integer(4)); System.out.println(list3); // [6, 4, 3]
list2.add(new Character('a')); list1.set(3,new Integer(10)); // update list1 and list3
list2.add(new Double(1.1)); System.out.println(list1); // [4, 5, 6, 10, 3, 4, a, 1.1]
System.out.println(list1); System.out.println(list3); // [6, 10, 3]
System.out.println(list2); list3.clear();
list1.removeAll(list2); System.out.println(list1); // [4, 5, 4, a, 1.1]
// difference: [4, 5, 6, 4] , [3, 4, a, 1.1] ==> [5, 6] System.out.println(list3); // []
System.out.println(list1); }
list1.add(0,new Integer(4)); }
15
LinkedList class in Java
• The LinkedList class in the java.util package is an
implementation of various operations on the nodes of a linked
list. So you simply declare an instance of the LinkedList class
and call member methods.
• LinkedList class implements a list as a generic doubly
linked list with references to the head and to the tail.
• Methods:
– LinkedList(), LinkedList(Collection c)
– boolean add(Object ob), void add(int pos, Object ob), boolean addAll(Collection c),
boolean addAll(int pos, Collection), void addFirst(Object ob), void addLast(Object ob), void
clear(), Object clone(), boolean contains(Object ob), boolean containsAll (Collection c),
boolean equals(Object ob), Object get(int pos), Object getFirst(), Object getLast(), int
hashCode(), int indexOf(Object ob), boolean isEmpty(), Iterator iterator(), int
lastIndexOf(Object ob), ListIterator listIterator(), ListIterator listIterator(int n), boolean
remove(Object ob), Object remove(int pos), Boolean removeAll(Collection c), Object
removeFirst(), Object removeLast(), void removeRange(int first, int last), Boolean
retainAll(Collection c), Object set(int pos, Object ob), int size(), List subList(int first, int last),
Object[] toArray(), Object[] toArray(Object a[]), String toString().
16
Example-3: Integer[] b = {new Integer(1), new Integer(2)}; // b = [1,
2]
import java.io.*; for (int i = 0; i < b.length; i++)
import java.util.LinkedList; System.out.print(b[i] + " ");
System.out.println();
class TestLinkedLists { Integer[] a1 = (Integer[]) lst2.toArray(b); // a1 = b = [4, 7]
public static void main(String[] ar) { for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
LinkedList lst1 = new LinkedList(); // lst1 = [] System.out.println();
lst1.addFirst(new Integer(4)); // lst1 = [4] a1 = (Integer[]) lst1.toArray(b); // a1 = [4, 6, 5], b = [4, 7]
lst1.addFirst(new Integer(5)); // lst1 = [5, 4] for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
lst1.addLast(new Integer(6)); // lst1 = [5, 4, 6]
System.out.println();
lst1.addLast(new Integer(5)); // lst1 = [5, 4, 6, 5] for (int i = 0; i < a1.length; i++)
System.out.println("lst1: " + lst1); // lst1 = [5, 4, 6, 5] System.out.print(a1[i] + " ");
System.out.println(lst1.lastIndexOf(new Integer(5)));// 3 System.out.println();
System.out.println(lst1.indexOf(new Integer(5))); // 0 Object[] a2 = lst1.toArray();
System.out.println(lst1.indexOf(new Integer(7))); // -1 for (int i = 0; i < a2.length; i++) // a2 = [4, 6, 5]
System.out.print(a2[i] + " "); // 4 6 5
lst1.remove(new Integer(5)); // lst1 = [4, 6, 5]
System.out.println();
LinkedList lst2=new LinkedList(lst1); // lst2=[4, 6, 5] for (int i = 0; i < lst1.size(); i++)
lst2.add(2,new Integer(8)); // lst2 = [4, 6, 8, 5] System.out.print(lst1.get(i) + " "); // 4 6 5
lst2.remove(new Integer(5)); // lst2 = [4, 6, 8] System.out.println();
lst2.remove(1); // lst2 = [4, 8] for (java.util.Iterator it=lst1.iterator(); it.hasNext(); )
System.out.println(lst2.getFirst()+" "+lst2.getLast()); //4 System.out.print(it.next() + " "); // 4 6 5
8 System.out.println();
System.out.println(lst2.set(1,new Integer(7))); //8, }
}
lst2=[4,7] 17
Exercise
• Implement QUEUE in a bank using LinkedList class :
1. Provide the menu as shown below; use an infinite loop; stop when user opts 4.
2. Add a customer should add an int number at the end of queue using the
method addLast().
3. Remove a customer should delete the int from front using the method
removeFirst().
4. Show the queue should display the numbers in queue using the method
System.out.println().
18