W3 ArraynLinkedlist
W3 ArraynLinkedlist
ALGORITHMS
Department: Computer science
Course Code: CSS-215
Course Instructor: Asst. Prof. Dr. Mohammed
Ala’anzy
Office no.: G-405
ABSTRACT
DATA TYPE
(ADT):
o ARRAY
o LINKED LIST
LINEAR LIST CONCEPTS
The simplest linear list structure. The sequence of a linear list is diagrammed as Figure
1.
Operations:
PrintList, MakeEmpty, Find, Find, Insert, Delete
Example:
A0 A1 A2 AN-1
Figure1: Array
INSERTION
New Item = 44
ADT list 0 1 2 3 4 5 k+1 (AFTER) MAX _LIST
positions
12 3 44 19 100 … 5 10 18 ? … ?
A0 A1 A2 AN-1
A 0
Operation:
A 1 A2 AN-1
find
return Arr[2];
DELETION
Example:
A0 A1 A2 AN-1
A0 A2 A3 AN-1
Operation: deletion
EXERCISE
Write an algorithm to count the odd number in the array list of integer number.
Definition: A collection of nodes that form a linear list structure. Each node is a
compound object that stores an element and a reference, to called next node or
another node.
10
STRUCTURE OF NODE
11
CHARACTERISTICS
first
"Ali" "Musa" "Kamel"
12
CHARACTERISTICS (CONT)
13
TERMINOLOGY
14
THE STRUCTURE OF A LINKED LIST
15
TYPE OF LINKED LIST
16
SINGLE LINKED LIST PROPERTIES
17
SINGLE LINKED LIST
Let L = {e1,e2,…,en}
Each element ei is represented in a separate node
Each node has exactly one link field that is used to locate the next
element in the linear list
The last node, en, has no node to link to and so its link field is NULL.
This structure is also called a chain.
first
Link Field ……
Data Field e1 e2 e3 en
18
CLASS ‘CHAINNODE’
19
OPERATIONS IN LINKED LIST
Create List
Receives the head structure and initializes the metadata for the list.
Destroy List
Destroy list deletes all nodes in the list.
Add new node
To add a new node to an empty list is to assign the list head pointer the address of the new
node and make sure that its link field is a null pointer.
Insert Node
Insert can be done at the beginning of the list, at the middle of the list or at the end of the
list.
20
OPERATIONS IN LINKED LIST
Empty List
When the head pointer of the list is null, then the list is empty.
Delete Node
Logically remove a node from the linked list by changing various link pointers and then
physically deleting the node from dynamic memory.
Delete can be done at the first node, at the last node or at a specified position of the list.
Traverse List
Algorithms that traverse a list start at the first node and examine each node in succession until the
last node has been processed.
21
OPERATIONS IN LINKED LIST
Search List
Search algorithm is used by several algorithms to locate element in a list.
Sequential search is used because there is no physical relationship among the nodes.
The classics sequential search returns the location of an element when it is found.
Length List
Counts the number of nodes in the list.
22
INSERTION
Example :
Operation: insertion
INSERTION
24
INSERT IN THE MIDDLE LINKED LIST
INSERT AN ELEMENT AT THE BEGINNING
Create(newnode);
newnode.next = header;
header = newnode;
CODE FRAGMENT TO INSERT IN FRONT
public void inserthead(Object obj) {
Node newNode = new Node(obj); // create new Node
27
CODE FRAGMENT TO INSERT AT A TAIL
public void insertLast(Object obj) {
Node newNode = new Node(obj); // create new node
if( isEmpty() ) // if empty list,
head = newNode; // first --> newNode
else
tail.next = newNode; // old tail --> newNode
tail = newNode; // newNode <-- tail
}
28
DELETION
Example:
• Remove node X
30
DELETION
CODE FRAGMENT TO DELETE FIRST NODE
32
CODE FRAGMENT TO DELETE AT A TAIL
first
link 0
data 20 10 30 80 11
34
TRAVERSAL
public int countNodes(){
int count = 0;
Element e = head;
while (e != null) {
count++;
e = e.next;
}
return count;
}
A method that computes the number of elements in a linked list.
35
FIND -NODE
a node:
1) element A3
2) next link
A0, A1, A2, ..., AN-1
Operation: FindKth
EXERCISE
Programs that use chains can be simplified or run faster by doing one or both of
the following:
1. Represent the linear list as a single linked circular list rather than as a chain
2. Add an additional node, called the head node, at the front
39
CIRCULAR LIST REPRESENTATION
40
DOUBLY
LINKED
LISTS
DOUBLE LINKED LIST REPRESENTATION
An ordered sequence of nodes in which each node has two pointers: left and right.
42
CLASS ‘DOUBLENODE’
44
NODE DATA
public DoublyLinkedList() {
pre = new Node();
post = new Node();
pre.next = post;
post.back = pre;
} // linked list node helper data type
1. newNode.back = location.back;
2. newNode.next = location;
3. location.back.next = newNode;
4. location.back = newNode;
FIND_ITEM(LISTDATA, ITEM, LOCATION, FOUND)