Ctec2909 Data Structures and Algorithms: Lecture 2 Arrays & Linked Lists

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

CTEC2909 DATA STRUCTURES AND

ALGORITHMS
Lecture 2 Arrays & Linked Lists
Arrays
Arrays are used for storing sequences of values.
All the elements in an array are the same type – e.g. integers,
booleans, doubles, Strings, etc.

34 47 5 74 19 elements
0 1 2 3 4 indices

The above array is declared as: int[] values = new int[5];


array declaration creation of the array
Iterating through arrays
Iterating through an array: The length of the array
for (int i = 0; i < names.length; i++){
System.out.println(names[i]);
}
Accesses the element at position i

Any operation that iterates through the whole array takes


O(n) time, where n is the length of the array.
Array operations
An advantage of arrays is that accessing an element can
be done in constant time.
e.g. numbers[4]
Inserting or removing an element from the middle is
more difficult. The elements after it have to be shifted.

34 47 5 74
0 1 2 3 4
70
Array operations
In the worst case, all
34 47 5 74
the elements might
0 1 2 3 4 have to move.
Therefore it is O(n)
34 47 5 74 complexity, where n is
0 1 2 3 4 the length of the array.

34 47 70 5 74
0 1 2 3 4
Array operations
Searching for a particular element also requires
searching through the whole array in the worst case –
the worst case is when the element is not in the array.

for (int i = 0; i < values.length; i++){


if (values[i] == searchItem){
return true;
}
}
return false;
Linked Lists
Linked lists are another type of data structure for storing
sequences. null
34 47 5 74

head tail
This time, each element is stored as a separate object, with a
link to the next element.
Implementation of a Linked List
red blue orange green

A Node has some data (in this case a String) and a link to the next node.

In C++ the link could be a pointer. In Java it is a reference to the next


node, so it is another Node object.

There has to be a way of getting the data and setting the data, and
getting the next link and setting the next link.
Implementation of a Linked List
red blue orange green

public class Node{ This could be other types e.g. int, depending
private String item; on what you want to store in the list.
private Node nextItem;

public Node(String s){ This is a constructor. It states how to


item = s; initialise the instance of the class.
}
}
Implementation of a Linked List
public void setItem(String s){
public class Node{
item = s;
private String item;
}
private Node nextItem;
public Node next(){
public Node(String s, Node n){
return nextItem;
item = s;
}
nextItem = n;
}
public void setNext(Node n){
nextItem = n;
public String getItem(){
}
return item;
} // End of class.
}
Implementation of a Linked List
red blue orange green

To create the above list:


null
Node n4 = new Node(“green”, null);
Node n3 = new Node(“orange”, n4);
Node n2 = new Node(“blue”, n3);
Node n1 = new Node(“red”, n2);
Implementation of a Linked List
red blue orange green

What is the output of the following: null


Node n = n1.next(); This returns the next node, n2.
String s = n1.next().getItem();
n1.next() gives n2 so then it becomes:
n2 n2.getItem(), which gives “blue”.
Implementation of a Linked List
red blue orange green

What is the output of the following: null


n = n1.next().next(); n1.next() gives n2 so then it becomes:
n2.next(), which gives the node n3.
s = n1.next().next().getItem();
n1.next().next() gives n3 so then it becomes:
n3.getItem() which gives “orange”.
n2

n3
Implementation of a Linked List
red blue orange green

Problem: How do we have the reference to the first node? null


Solution: It is stored as a head variable in a LinkedList class.

The LinkedList class represents the list, but we don’t need to store
all the nodes in it.
Each node has a link to the next, so we only need a link to the first
node.
Implementation of a Linked List
red blue orange green

null
public class LinkedList(){ public getHead(){
private Node head; return head;
}
public LinkedList(Node n){ public setHead(Node n){
head = n; head = n;
} }
}// end of class
Implementation of a Linked List
red blue orange green

Now we can create a LinkedList: null


Node n4 = new Node(“green”, null);
Node n3 = new Node(“orange”, n4);
Node n2 = new Node(“blue”, n3);
Node n1 = new Node(“red”, n2); This sets the head of the
linked list to n1.
LinkedList list = new LinkedList(n1);
head Implementation of a Linked List
red blue orange green

Now we can get the head node like this: null

Node theHead = list.getHead(); This returns the node n1.

n = theHead.next(); This returns the node n2.

theHead.next().next().setItem(“yellow”);
This changes the 3rd node to “yellow”.
Linked Lists Insertion
Inserting after a given node:

red blue orange green

Node newItem = new Node(“white”, null);


newItem.setNext(n1.next());
white
n1.setNext(newItem);
This is just O(1)!
Linked Lists Deletion
What about deletion (delete after)?

red blue orange green

This is just O(1) again.


n1.setNext(n1.next().next()); Compare that to deletion in
arrays – all the elements
n2 beyond it have to be moved
This is equivalent to: back.
n1.setNext(n3); n3
Linked Lists Searching
34 47 5 74
public boolean find(String item){
bool found = false; Searching is not so easy.
Node current = head;
while (!found && current != null){
To find a particular
if (current.getItem().equals(searchItem)){ element, the whole
found = true; list has to be
} traversed in the
current = current.next(); worst case.
return found; Therefore it is O(n).
}
Linked Lists vs Arrays
Arrays have fast access to any element. Linked lists must
traverse the list to reach elements.

Linked lists have fast insertion (inserting after a node) and


deletion anywhere in the list. Arrays must shift elements to
make room or fill up spaces.

Searching is the same for both.

Arrays have a fixed size. Linked lists can be increased or


decreased as needed.
Doubly Linked Lists
Inserting after a given node is easy but inserting before requires
traversing the list to get to the node before.
Solution: Have previous links as well.

red blue orange green

public class Node{ null


public Node next(){…}
public Node previous(){…}
Doubly Linked Lists Insertion
Inserting after a given node:

red blue orange green

Node newItem = new Node(“white”, null);


newItem.setNext(n1.next());
white newItem.setPrevious(n1);
n1.next().setPrevious(newItem);
n1.setNext(newItem);
Doubly Linked Lists Insertion
Inserting before a given node:

red blue orange green

Node newItem = new Node(“white”, null);


newItem.setNext(n2);
white newItem.setPrevious(n2.getPrevious());
n2.previous().setNext(newItem);
n2.setPrevious(newItem);
Now this is O(1) too!
Doubly Linked Lists Deletion
Previously we only looked at deleting after a given node.

red blue orange green

n1.next().next().setPrevious(n1);

n3 n1.setNext(n1.next().next());
Doubly Linked Lists Deletion
Deleting before a given node:

red blue orange green

n3.previous().previous().setNext(n3);

n3.setPrevious(n3.previous().previous());
n1
Tail Element
Doubly-linked lists have a tail element as well as the head.
red blue orange green

head null
tail
This allows the list to be traversed from either end, using the
previous links from the tail or the next links from the head.
Singly-linked lists can also have a tail. This could be useful for
adding to the end of the list.
Circular Linked Lists
Instead of the last element pointing to null, it can point to the
head element.

red blue orange green

head n = n4.next(); This gives the first node.


n4.next() == head returns true
Circular Linked Lists
A circular list with one element:

red The only node in the list


points to itself.

head
Circular Linked Lists
Adding a node to the start of the list:
Node newItem = new Node(“yellow”, head);
n4.setNext(newItem);
head = newItem;

Adding a node anywhere else:


Node newItem = new Node(“yellow”, null);
newItem.setNext(n2.next());
n2.setNext(newItem);
Circular Linked Lists
Using a sentinel or “dummy” node makes it the same for both cases:
dummy red blue green

head Inserting after a given node n:


Node newItem = new Node(“yellow”, null);
newItem.setNext(n.next());
n.setNext(newItem);

You might also like