0% found this document useful (0 votes)
20 views54 pages

W3 ArraynLinkedlist

The document discusses arrays and linked lists as implementations of linear lists. It describes: 1) Arrays allow constant-time access to elements but require shifting elements to insert or delete in the middle, making those operations O(n) time. 2) Linked lists avoid shifting elements by linking nodes through references, allowing constant-time insertion and deletion anywhere. 3) Common linked list operations include insertion and deletion at the head, tail, or middle using pointer adjustments between nodes.

Uploaded by

dastanktl26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views54 pages

W3 ArraynLinkedlist

The document discusses arrays and linked lists as implementations of linear lists. It describes: 1) Arrays allow constant-time access to elements but require shifting elements to insert or delete in the middle, making those operations O(n) time. 2) Linked lists avoid shifting elements by linking nodes through references, allowing constant-time insertion and deletion anywhere. 3) Common linked list operations include insertion and deletion at the head, tail, or middle using pointer adjustments between nodes.

Uploaded by

dastanktl26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 54

INTRODUCTION TO Week 3: Array & LinkedList

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

Figure 1: A Linear List


3
IMPLEMENTATION 1:
A LIST ADT USING ARRAY

 Example:

A0, A1, A2, ...,


AN-1

A0 A1 A2 AN-1

Figure1: Array
INSERTION

 Insert item at the back is easy if there is a space.


 Insert item in the middle requires the movement of all elements to the right as in
Figure 2.
Array 0 1 2 3 4 k (BEFORE) MAX_LIST-1
indexes
12 3 19 100 … 5 10 18 ? … ?

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 ? … ?

Figure 2: Shifting items for insertion at position 3


5
INSERTION ALGORITHM
Example algorithm:
INSERT(LA, N, K, ITEM)
//LA is a linear array with N element
//K is integer positive where K < N
//Insert an element, ITEM in index K
1. [Assign counter]
J = N – 1;
2. Repeat step 2.1 and 2.2 while J >= K
2.1 [shift to the right all elements from J]
LA[J+1] = LA[J]
2.2 [decrement counter] J = J – 1
3. [Stop repeat step 2]
4. [Insert element] LA[K] = ITEM
5. [Reset N] N = N + 1
6. Exit
 Worst case of these operation O(N)
 Running time for insert and delete operation is slow and list size must be known in advance,
simple array is not suitable. 6
FIND -NODE
Example :

A0, A1, A2, ..., AN-1

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 A1 A2 AN-1

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.

 Write an algorithm to delete the element at the -position in an array list.


IMPLEMENTATION 2:
A LIST ADT USING LINKED LIST

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.

Structure of a node Data Link

first Node Node Node Node


A0 A1 A2 A3

10
STRUCTURE OF NODE

11
CHARACTERISTICS

Insert and delete nodes in any order


The nodes are connected
Each node has two components
 Information (data)
 Link to the next node (reference)

The nodes are accessed through the links between them

first
"Ali" "Musa" "Kamel"

12
CHARACTERISTICS (CONT)

Head / Predecessor Node X Successor tail


of X of X
first

• For each node:


• The node that is before X is called Predecessor of X.

• The node that is after X is called successor of X

13
TERMINOLOGY

Head (front, first node):


 The node without predecessor, the node that starts the lists.
Tail (end, last node):
 The node that has no successor, the last node in the list.
Current node: The node being processed.
 From the current node we can access the next node.
Empty list: No nodes exist

14
THE STRUCTURE OF A LINKED LIST

15
TYPE OF LINKED LIST

1. Single linked lists :


• Each node contains a link only to the next node
2. Double linked lists :
• Each node contains two links – to the
previous and to the next node
3. Circular lists:
• The tail is linked to the head.

16
SINGLE LINKED LIST PROPERTIES

 Stores a collection of items non-contiguously.


 Each item in the list is stored with an indication of where the next item is.
 The first item of the linked list must be pointed.
 The list will be a chain of objects, that contain the data and a reference to the next
Node in the list.
 Allows addition or deletion of items in the middle of the collection with only a
constant amount of data movement.

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’

public class Node {


Object data;
Node next;
Node(Object obj, Node element) {
data = obj;
next = element;
}
}

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 :

A0, A1, A2, ..., AN-1

 Operation: insertion
INSERTION

To insert a node X between the nodes A and B:


1. Create a link from X to B, then
2. Create a link from A to X,

24
INSERT IN THE MIDDLE LINKED LIST
INSERT AN ELEMENT AT THE BEGINNING

Create a new node;


1.Create an element in the new node;
2.Pointer of new node will points to the first element (non-header);
3.Pointer from the header points to new node.

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

newNode.next = head; // newNode --> old head


head = newNode; // head --> newNode
}

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:

A0, A1, A2, ..., AN-1

O(1) running time


 Operation: deletion
DELETION
To delete a node X between A and B:

• Create a link from A to B,

• Remove node X

30
DELETION
CODE FRAGMENT TO DELETE FIRST NODE

public Node deleteHead() // delete head item


{ // (assumes list not empty)
Node temp = head; // save reference to link
head = head.next; // delete it: head-->old next
delete temp; // deleted link
}

32
CODE FRAGMENT TO DELETE AT A TAIL

public Node deleteTail( ) // delete at a tail


{
Node current = head; // search for link
while(current.next != null) {
previous = current; // go to next link
current = current.next;
}
if (current == head) // if first link,
head = head.next; // empty list
else
previous.next = current.next; // delete tail
return current;
}
33
DELETE -NODE

To delete the fourth element from the chain:


 locate the third and fourth nodes
 link the third node to the fifth
 free the fourth node so that it becomes available for reuse

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

next next next next null

 Operation: FindKth
EXERCISE

Write an algorithm to find kth nodes in a linked list.


CIRCULAR
LIST
CIRCULAR LIST REPRESENTATION

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’

public class DoubleNode {


public String element;
public DoubleNode prev;
public DoubleNode next;
public DoubleNode(Object obj, DoubleNode p, DoubleNode n) {
element=obj;
prev=p;
next=n;
}
43
CIRCULAR DOUBLE LINKED LIST

Add a head node at the left and/or right ends


In a non-empty circular double linked list:
 LeftEnd->left is a pointer to the right-most node (i.e., it equals RightEnd)
 RightEnd->right is a pointer to the left-most node (i.e., it equals LeftEnd)
Shows as figure below:

44
NODE DATA

info: the user's data


next, back: the address of the next and previous node in the list

.back .info .next


EXAMPLE DOUBLY LINKED LIST
NODE DATA
public class DoublyLinkedList {
private int N; // number of elements on list private
Node pre; // sentinel before first item private
Node post; // sentinel after last item

public DoublyLinkedList() {
pre = new Node();
post = new Node();
pre.next = post;
post.back = pre;
} // linked list node helper data type

private class Node {


private String info ;
private Node next;
private Node back;
}
}
INSERT NEW NODE IN THE LIST
INSERT NEW NODE IN THE LIST
INSERT NEW NODE IN THE LIST (CONT.)

1. newNode.back = location.back;
2. newNode.next = location;
3. location.back.next = newNode;
4. location.back = newNode;
FIND_ITEM(LISTDATA, ITEM, LOCATION, FOUND)

RetrieveItem, InsertItem, and DeleteItem all require a search !


Write a general non-member function FindItem that takes item as a parameter and
returns location and found.
InsertItem and DeleteItem need location
RetrieveItem needs found
EXERCISE: WRITE ALGORITHM TO SOLVE THE
INSERTION BELOW.
DELETING FROM A DOUBLY LINKED LIST

Be careful about the end cases!!


THANK YOU!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy