0% found this document useful (0 votes)
11 views85 pages

Array&Linked List New

The document provides an overview of arrays and linked lists, detailing their structures, operations, and comparisons. It explains the limitations of arrays, such as fixed size and inefficient insertion/deletion, while highlighting the advantages of linked lists, including dynamic sizing and easier data manipulation. The content also covers various linked list types, including singly and doubly linked lists, and their respective operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views85 pages

Array&Linked List New

The document provides an overview of arrays and linked lists, detailing their structures, operations, and comparisons. It explains the limitations of arrays, such as fixed size and inefficient insertion/deletion, while highlighting the advantages of linked lists, including dynamic sizing and easier data manipulation. The content also covers various linked list types, including singly and doubly linked lists, and their respective operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Lists & Linked Lists

(Section 4)

Algorithms and Data Structures


(CS211 & CS202)

College of Computer Science and Engineering


Taibah University
S1, 1438
Lecture Scope

◼ Arrays

◼ Linked Lists
 Singly Linked List

 Doubly Linked List

◼ Linked Lists Analysis (Complexity Analysis)


 With Singly Linked List

 With Doubly Linked List

2
Arrays

3
Arrays
◼ Imagine that we have 100 scores. We need to read them, process
them and print them.

◼ We must also keep these 100 scores in memory for the duration of
the program.

◼ We can define a hundred variables, each with a different name, as


shown below

4
Arrays (cont…)

◼ But having 100 different names creates other problems. We need


100 references to read them, 100 references to process them and
100 references to write them. The following Figure shows a diagram
that illustrates this problem.

5
Arrays (cont…)

◼ An array is a sequenced collection of elements, normally of the


same data type, although some programming languages accept
arrays in which elements are of different types. We can refer to
the elements in the array as the first element, the second element
and so forth, until we get to the last element.

6
Arrays (Declaration)

◼ Declared using [ ] operator


1- datatype[] arrayRefVar;

Example:

double[] myList;

OR
2- datatype arrayRefVar[]; // This style is allowed

Example:

double myList[];

7
Arrays (Storage in Memory)

◼ In the definition:
int [ ] tests;

tests = new int[SIZE]; // SIZE is 5

allocates the following memory

first second third fourth fifth


element element element element element

8
Arrays (Terminology)

◼ In the definition:
int [ ] tests;
tests = new int[ISIZE];

◼ int is the data type of the array elements

◼ tests is the name of the array

◼ ISIZE, in [ISIZE], is the size declarator. It shows the number


of elements in the array.

◼ The size of an array is the number of bytes allocated for it

(number of elements) * (bytes needed for each element)

9
Arrays (Terminology) (cont…)

Array Terminology Examples The Length of an Array

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 //returns 10

10
Arrays (Accessing Array Elements)

◼ Each array element has a subscript (index), used to


access the element.

◼ Subscripts (index) start at 0

Subscripts
(index) 0 1 2 3 4

11
Arrays (Example)

public class Test { After the array is created

public static void main(String[] args)


0 0

{ 1 0

2 0
int[] values = new int[5]; 3 0

0
for (int i = 1; i < 5; i++) { 4

values[i] = i + values[i-1];
After the first iteration
}
0 0
values[0] = values[1] + values[4]; 1 1

2 0
} 0
3

} 4 0

12
Multi-Dimensional Arrays

◼ The arrays discussed so far are known as one-dimensional arrays


because the data is 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.

13
Multi-Dimensional Arrays (cont…)
◼ Two-Dimensional Array Example:

14
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.

15
Operations on Array (cont…)

◼ The following algorithm gives an example of finding the average of elements


in array whose elements are reals.

16
Records

◼ A record is a collection of related elements, possibly of different


types, having a single name.

◼ Each element in a record is called a field.

◼ A field is the smallest element of named data that has meaning.

◼ A field has a type and exists in memory.

◼ Fields can be assigned values, which in turn can be accessed for


selection or manipulation.

◼ A field differs from a variable primarily in that it is part of a record.

17
Records (cont…)

◼ Two examples of records: The first example, fraction,


has two fields, both of which are integers. The second
example, student, has three fields made up of three
different types.

18
Array of Records
◼ If we need to define a combination of elements and at the same time
some attributes of each element, we can use an array of records.

◼ For example, in a class of 30 students, we can have an array of 30


records, each record representing a student.

19
Array of Records (cont…)

◼ Example:

◼ The following shows how we access the fields of


each record in the students array to store values in
them.

20
Array of Records (cont…)

◼ However, we normally use a loop to read data into an array of records.


Algorithm 11.2 shows part of the pseudocode for this process.

21
Arrays versus Array of Records

◼ Both an array and an array of records represent a


list of items.

◼ An array can be thought of as a special case of an


array of records in which each element is a record
with only a single field.

22
Arrays: The Pros & Cons

◼ Pros:
 Fast element access

◼ Cons:
 While many applications require resizing, static arrays

 are impossible to resize

 Required size is not always immediately available

23
Linked Lists

24
Introduction
◼ Storing data items in arrays has at least two limitations
 The array size is fixed once it is created: Changing the size of the array requires
creating a new array ad then copying all data from the old array to the new array

 The data items in the array are next to each other in memory: Inserting an item
inside the array requires shifting other items

◼ A linked structure is introduced to overcome limitations of arrays and allow


easy insertion and deletion
 A collection of nodes storing data items and links to other nodes

 If each node has a data field and a reference field to another node called next or
successor, the sequence of nodes is referred to as a singly linked list

 Nodes can be located anywhere in the memory

 The first node is called head and the last node is called tail
25
Linked Structures

◼ An alternative to array-based implementations are linked structures

◼ A linked structure uses object references to create links between


objects

◼ Recall that an object reference variable holds the address of an


object

26
Linked Structures (cont…)

◼ A Person object, for instance, could contain a reference


to another Person object

◼ A series of Person objects would make up a linked list:

27
Linked Structures (cont…)

◼ Links could also be used to form more complicated,


non-linear structures

28
Linked Lists

◼ Linked Lists are dynamic data structures that grow and shrink one
element at a time, normally without some of the inefficiencies of
arrays.

◼ A linked list is a series of connected nodes

A B C 
Head

◼ We create a new Node every time we add something to the List and
we remove nodes when item removed from list and reclaim memory

29
Linked Lists (cont…)

◼ Allocate elements one at a time as needed, have each element


keep track of the next element

◼ Result is referred to as linked list of elements, track next element


with a pointer

Array of Elements in Memory


Jane Bob Anne

Linked List
Jane Anne Bob

30
Linked Lists (cont…)

◼ Each node contains at least

1. A piece of data (any type)

2. Pointer to the next node in the list

◼ Head : pointer to the first node

◼ The last node points to NULL

node

A
data pointer

31
Linked Lists (cont…)

◼ There are no index values built into linked lists

◼ To access each node in the list you must follow the references
from one node to the next

Person current = first;

while (current != null)

System.out.println(current);

current = current.next;

32
Linked Lists Operations

 MakeEmpty

 InsertNode Transformers

 DeleteNode

 IsEmpty

 FindNode Observers

 DisplayList

33
Linked Lists Operations (cont…)

◼ Care must be taken to maintain the integrity of the links

◼ To insert a node at the front of the list, first point the new node to
the front node, then reassign the front reference

34
Linked Lists Operations (cont…)

◼ To delete the first node, reassign the front reference accordingly

◼ If the deleted node is needed elsewhere, a reference to it must be


established before reassigning the front pointer

35
Linked Lists Operations (Inserting a Node)

◼ Before insertion into a linked list, we first apply the searching algorithm.
If the flag returned from the searching algorithm is false, we will allow
insertion, otherwise we abort the insertion algorithm, because we do
not allow data with duplicate values.

◼ Four cases can arise:

❑ Inserting into an empty list.

❑ Insertion at the beginning of the list.

❑ Insertion at the end of the list.

❑ Insertion in the middle of the list.

36
Linked Lists Operations (Deleting a node)

◼ Before deleting a node in a linked list, we apply the search algorithm.


If the flag returned from the search algorithm is true (the node is
found), we can delete the node from the linked list.

◼ Two cases:

❑ Deleting the first node

❑ Deleting any other node.

◼ In other words, the deletion of the last and the middle nodes can be
done by the same process.

37
Linked Lists Operations (Retrieving a node)

◼ Retrieving means randomly accessing a node for the purpose of


inspecting or copying the data contained in the node.

◼ Before retrieving, the linked list needs to be searched. If the data


item is found, it is retrieved, otherwise the process is aborted.

◼ Retrieving uses only the cur pointer, which points to the node
found by the search algorithm.

38
Linked Lists Operations (Traversing a node)

◼ To traverse the list, we need a “walking” pointer, which is a pointer


that moves from node to node as each element is processed.

◼ We start traversing by setting the walking pointer to the first node in


the list. Then, using a loop, we continue until all of the data has been
processed.

◼ Each iteration of the loop processes the current node, then


advances the walking pointer to the next node. When the last node
has been processed, the walking pointer becomes null and the loop
terminates.

39
Variations of Linked Lists

◼ There are many variations on the basic linked list


concept

◼ Linked list can be Singly-Linked or Doubly-Linked

40
Variations of Linked Lists (cont…)

A doubly
linked list
contains next
and previous
members

A single
linked list
contains only
a next
member.
41
Variations of Linked Lists (cont…)

◼ Circular linked lists


 The last node points to the first node of the list

A B C
Head

 How do we know when we have finished traversing the


list? (Tip: check if the pointer of the current node is
equal to the head.)

42
Variations of Linked Lists (cont…)

◼ Circular linked lists


list

2 8 3 5
Circular singly linked list

list
5 2 8 3
Circular doubly linked list

43
Singly Linked Lists

◼ A singly linked list is a concrete


data structure consisting of a
next
sequence of nodes

◼ Each node stores

 element node
element
 link to the next node

A B C D
44
Singly Linked Lists

Inserting at the Head

1. Allocate a new node

2. Insert the new element

3. Have the new node point


to old head

4. Update head to point to


the new node

45
Singly Linked Lists

Inserting at the Head (cont…)

Algorithm addFirst(v)

v.setNext(head); // make v point to the old head node

head = v; // make head point to the new node

size = size + 1; // increment the node count

46
Singly Linked Lists

Removing at the Head

1. Update head to point


to next node in the list

2. Allow garbage
collector to reclaim the
former first node

47
Singly Linked Lists

Removing at the Head (cont…)

Algorithm removeFirst()

if ( head = null )

Indicate an error: the list is empty;

t = head ;

/* make head point to the next node (or null) */

head = head.getNext();

t.setNext(null); //null out the next pointer of the removed node

size = size - 1; //decrement the node count

48
Singly Linked Lists

Inserting at the Tail

1. Allocate a new node

2. Insert the new element

3. Have the new node


point to null

4. Have the old last node


point to new node

5. Update tail to point to


new node

49
Singly Linked Lists

Inserting at the Tail (cont…)

Algorithm addLast(v)

v.setNext(null); //make the new node v point to null object

tail.setNext(v); //make the old tail node point to the new node

tail = v ; //make tail point to the new node

size = size +1; //increment the node count

50
Singly Linked Lists

Removing at the Tail

◼ Removing the tail node of a


singly linked list cannot be
done in constant time.

◼ We need to traverse the


whole linked list to remove
the tail node, which takes
O(n) time, where n is the
number of nodes in the linked
list.

51
Singly Linked Lists

Removing at the Tail (cont…)


Algorithm removeLast()
{
if ( size = 0 )
Indicate an error: the list is empty;
else
{
if ( size = 1 ) // only one node
head = null;
else // at least two nodes
{
t = head; /* make t point to the head */
while ( t.getNext != null ) /* find the last node */
{
s = t;
t = t.getNext();
}
s.setNext(null); //null out the next pointer of the last node
}
size = size - 1; //decrement the node count
}
}
52
Doubly Linked Lists

◼ A doubly linked list is a concrete data structure


consisting of a sequence of nodes. prev next

◼ Each node stores:


 element
 link to the previous node
 link to the next node element node
◼ Special trailer and header nodes

header nodes/positions trailer

elements
53
Doubly Linked Lists (cont…)

◼ In the Doubly linked lists:

 Each node points to the successor and the predecessor

 Advantage:

given a node, it is easy to visit its predecessor. Convenient to


traverse lists backwards

A B C
 5 10 2 

Head
54
The Node Class of Doubly Linked List
/** Node of a doubly linked list of strings */
public class DNode {
protected String element; // String element stored by a node
protected DNode next, prev; // Pointers to next and previous nodes
/** Constructor that creates a node with given fields */
public DNode(String e, DNode p, DNode n) {
element = e;
prev = p;
next = n; } /** Returns the element of this node */
public String getElement() { return element; } /** Returns the previous node of this node */
public DNode getPrev() { return prev; } /** Returns the next node of this node */
public DNode getNext() { return next; } /** Sets the element of this node */
public void setElement(String newElem) { element = newElem; }
/** Sets the previous node of this node */
public void setPrev(DNode newPrev) { prev = newPrev; }
/** Sets the next node of this node */
public void setNext(DNode newNext) { next = newNext; }
} 55
Doubly Linked Lists

Inserting at the Head

header trailer

Rome Seattle New York

(a) Before the insertion

header trailer

Sydney Rome Seattle New York

(b) After the insertion


56
Doubly Linked Lists

Inserting at the Head (cont…)

Algorithm addFirst(v)

w = header.getNext(); // make w point to the first node

v.setNext(w); // make v point to the old first node

v.setPrev(header); // make v point to the header

w.setPrev(v); // make the old first node point to v

header.setNext(v); // make header point to v

size = size+1; // increment the node count

57
Doubly Linked Lists

Inserting in the Middle

header trailer

Rome Seattle New York

(a) Before the insertion

header trailer

Rome Seattle Sydney New York

(b) After the insertion


58
Doubly Linked Lists

Inserting in the Middle (cont…)

Algorithm addAfter(v,z)

w=v.getNext(); // make w point to the node after v

z.setPrev(v); // link z to its predecessor, v

z.setNext(w); // link z to its successor, w

w.setPrev(z); // link w to its new predecessor, z

v.setNext(z); // link v to its new successor, z

size = size+1; // increment the node count

59
Doubly Linked Lists

Removing at the Tail

header trailer

Rome Seattle Sydney New York

(a) Before the deletion

header trailer

Rome Seattle Sydney

(b) After the deletion


60
Doubly Linked Lists

Removing at the Tail (cont…)

Algorithm removeLast():

if ( size = 0 )

Indicate an error: this list is empty;

v = trailer.getPrev() // make v point to the last node

u = v.getPrev(); // u points to the node before the last node

trailer.setPrev(u);

u.setNext(trailer);

v.setPrev(null);

v.setNext(null);

size = size -1;

61
Doubly Linked Lists

Removing in the Middle

header trailer

Rome Seattle Sydney New York

(a) Before the deletion

header trailer

Rome Seattle New York

(b) After the deletion


62
Doubly Linked Lists

Removing in the Middle (cont…)

Algorithm remove(v):

u = v.getPrev() // make u point to the node before v

w = v.getNext(); // w points to the node after v

w.setPrev(u); // link out v

u.setNext(w);

v.setPrev(null); // null out the pointers of v

v.setNext(null);

size = size -1; // decrement the node count

63
Applications of linked lists

◼ A linked list is a very efficient data structure for sorted list that will go through
many insertions and deletions.

◼ A linked list is a dynamic data structure in which the list can start with no
nodes and then grow as new nodes are needed.

◼ A node can be easily deleted without moving other nodes, as would be the
case with an array.

◼ For example, a linked list could be used to hold the records of students in a
school. Each quarter or semester, new students enroll in the school and
some students leave or graduate.

A linked list is a suitable structure if a large number of insertions and deletions


are needed, but searching a linked list is slower that searching an array.
64
Linked List Example Using JAVA

◼ Java has a LinkedList class defined as part of the Java


collection framework, which is found in the java.util
package.

◼ So you simply declare an instance of the LinkedList


class and call member methods as required by your
program to manipulate the linked list.

65
Linked List Example Using JAVA (cont…)

import java.util.LinkedList;

LinkedList list = new LinkedList();

list.add(new Integer(10)); //list.add(new object)


// the add() member method requires an object and not
a primitive data type.

66
Linked List Example Using JAVA (cont…)
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) throws IOException
{ LinkedList list = new LinkedList();
Output
list.add(new Integer(10));
list.add(new Integer(20));
list.add(new Integer(30)); 10
for(int i=0; i<list.size(); i++) 20
{
Integer temp = (Integer)list.get(i); 30
System.out.println(temp); 30
}
for(int i=list.size()-1; i>=0; i--) 20
{ Integer temp = (Integer)list.get(i); 10
System.out.println(temp);
}
}
} 67
67
Singly Linked Lists Analysis

- Complexity Analysis -

Note: some ideas in this section are repeated in another way, or another
representation way

Both are doing the same operation

68
Singly Linked Lists

Insertion at the End (Append)

public void append(Object obj){


Element element = new Element(obj, null);
if(head == null)
head = element;
else
tail.next = element;
tail = element;
}
Complexity is O(1)

69
Singly Linked Lists

Insertion at the Beginning (Prepend)

public void prepend(Object obj) {


Element element = new Element(obj, head);
if(head == null)
tail = element;
head = element;
}
Complexity is O(1)

70
Singly Linked Lists

Insertion Before and After an Element


◼ we can also define insertAfter or InsertBefore for the Element class to
insert a given object after or before a specific a node respectively:
public void insertBefore(Object obj) {
Element element = new Element(obj, this);
if(this == head) {
head = element;
return;
}
Element previous = head; Complexity is O(n)
while (previous.next != this) {
previous = previous.next;
}
previous.next = element;
}

public void insertAfter(Object obj) {


next = new Element(obj, next);
if(this == tail)
tail = next; Complexity is O(1)
}
71
Singly Linked Lists

Traversal

◼ To move a reference e from one node to the next:

e = e.next;

◼ Example: Count the number of nodes in a linked list.

public int countNodes(){


int count = 0;
Element e = head;
while(e != null){
count++;
e = e.next;
} Complexity is O(n)
return count;
}

72
Singly Linked Lists

Searching

◼ To search for an element, we traverse from head until we locate the object.

 Example: Count the number of nodes with data field equal to a given
object.

public int countNodes(Object obj){


int count = 0;
Element e = head;
while(e != null){
if(e.data.equals(obj))
count++;
e = e.next;
Complexity is O(n)
}
return count;
}

73
Singly Linked Lists

Deletion – Deleting First and Last Element


public void extractFirst() {
if(head == null)
throw new IllegalArgumentException("item not found");
head = head.next;
if(head == null)
tail = null;
} Complexity is O(1)

public void extractLast() {


if(tail == null)
throw new IllegalArgumentException("item not found");
if (head == tail)
head = tail = null;
else {
Element previous = head;
while (previous.next != tail) Complexity is O(n)
previous = previous.next;
previous.next = null;
tail = previous;
}
} 74
Singly Linked Lists

Deletion – Deleting a specific element


◼ To delete a specific element, we use either the extract method of MyLinkedList
or that of the Element inner class.
public void extract(Object obj) {
Element element = head;
Element previous = null;
while(element != null && ! element.data.equals(obj)) {
previous = element;
element = element.next;
} Complexity is O(n)
if(element == null)
throw new IllegalArgumentException("item not found");
if(element == head)
head = element.next;
else
previous.next = element.next;
if(element == tail)
tail = previous;
}
75
Doubly Linked Lists Analysis

- Complexity Analysis -

Note: some ideas in this section are repeated in another way, or another
representation way

Both are doing the same operation

76
Doubly Linked Lists

Insertion at the End (append)

public void append(Object obj){


Element element = new Element(obj, null, tail);
if(head == null)
head = tail = element;
else {
tail.next = element;
tail = element; Complexity is O(1)
}
}

77
Doubly Linked Lists

Insertion at the Beginning (prepend)


public void prepend(Object obj){
Element element = new Element(obj, head, null);
if(head == null)
head = tail = element;
else {
head.previous = element;
head = element; Complexity is O(1)
}
}

78
Doubly Linked Lists

Insertion Before an Element


◼ Inserting before the current node (this) that is neither the first nor the last
node:
Element element = new Element(obj, this, this.previous);
this.previous.next = element;
this.previous = element;

Complexity is O(1)

79
Doubly Linked Lists

Traversal
◼ For DoublyLinked list, traversal can be done in either direction: forward, starting from
head, or backward starting from tail.


Element e = head; Element e = tail;
while (e != null) { while (e != null) {
//do something //do something
e = e.next; e = e.previous;
} }

◼ Example: Count the number of nodes in a linked list.


public int countNodes(){
int count = 0;
Element e = head;
while(e != null){
count++;
e = e.next;
} Complexity is O(n)
return count;
}
80
Doubly Linked Lists

Traversal (cont…)
◼ Example: The following computes the sum of the last n nodes:

public int sumLastNnodes(int n){


if(n <= 0)
throw new IllegalArgumentException("Wrong: " + n);
if(head == null)
throw new ListEmptyException();

int count = 0, sum = 0;


Element e = tail;
while(e != null && count < n){
sum += ((Integer)e.data).intValue();
count++;
e = e.previous;
}
if(count < n)
throw new IllegalArgumentException(“No. of nodes < "+n);
return sum;
}
Complexity is O(n)
81
Doubly Linked Lists

Deletion
◼ To delete an element, we use either the extract method of DoublyLinkedList or that
of the Element inner class.
public void extract(Object obj){
Element element = head;
while((element != null) && (!element.data.equals(obj)))
element = element.next;

if(element == null)
throw new IllegalArgumentException("item not found");
if(element == head) {
head = element.next;
if(element.next != null)
element.next.previous = null;
}else{
element.previous.next = element.next;
if(element.next != null)
element.next.previous = element.previous;
}
if(element == tail)
tail = element.previous;
}
Complexity is O(n) 82
Summary
◼ An array is a sequenced collection of elements, normally of the same data type.

◼ An alternative to array-based implementations are linked structures.

◼ A linked structure uses object references to create links between objects.

◼ A linked list dynamically grows as needed and essentially has no capacity limitations.

◼ The order in which references are changed is crucial to maintaining a linked list.

◼ Linked list can be Singly-Linked or Doubly-Linked.

◼ Each node in a singly linked list stores element and link to the next node.

◼ Each node in a doubly linked list stores element, link to the previous node and link to the
next node.

◼ In circular linked lists, the last node points to the first node of the list.

◼ Java has a LinkedList class defined as part of the Java collection framework.

◼ So you simply declare an instance of the LinkedList class and call member methods as
required.
83
References

◼ Java Software Structures - Designing and Using Data Structures, 4th edition, Lewis and Chase.

◼ Data Structures and Problem Solving Using Java, 4th edition, Weiss.

◼ Data Structures & Algorithms in Java, 3rd edition, Drozdek.

◼ Data Structures and Algorithm Analysis in Java, 3rd edition, Weiss.

◼ Algorithms, 4th edition, Sedgewick and Wayne.

◼ A Practical Introduction to Data Structures and Algorithm Analysis, 3rd edition, Shaffer.

◼ Data Structures and Algorithms in Java, 6th edition, Goodrich, Tamassia and Goldwasser.

◼ Foundations of Computer Science, 2nd Edition, Forouzan and Ferous.

◼ Slides at: http://faculty.kfupm.edu.sa/ICS/jauhar/ics202/

84
Any Question
???

85

You might also like