0% found this document useful (0 votes)
2 views

Link List

A linked list is a data structure consisting of nodes that reference the next node, with the first node as the head and the last as the tail. There are various types of linked lists, including single, doubly, and circular linked lists, each with different navigation capabilities. Operations such as insertion and deletion in linked lists can be more efficient than in arrays, particularly at the beginning of the list.

Uploaded by

smaugqwer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Link List

A linked list is a data structure consisting of nodes that reference the next node, with the first node as the head and the last as the tail. There are various types of linked lists, including single, doubly, and circular linked lists, each with different navigation capabilities. Operations such as insertion and deletion in linked lists can be more efficient than in arrays, particularly at the beginning of the list.

Uploaded by

smaugqwer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

LINKED LIST

LINKED LIST

• A linked list is a data structure that stores a sequence of elements.

• Each element in the list is called a node, and each node has a reference
to the next node in the list.

• The first node in the list is called the head, and the last node in the list
is called the tail.
Arrange the name of the students according to the first letter of their names

Billy

Neil
Vincent Aaron

Terrence
Harold
John
Arrange the name of the students according to the first letter of their names

Aaron Billy Harold John Neil Terrence Vincent

How to maintain the list in the memory?


Ways to maintain a list in a memory

Two (2) Ways

Arrays Linked List

1 2 3 4 5 data addr data addr


TYPES OF LINKED LIST

Single linked list: Navigation is forward only

Doubly linked list: Forward and backward navigation is possible

Circular linked list: Forward and backward navigation is possible


Single Linked List

A single linked list is made up of nodes that consists of two parts:

• Data
• Link
Node

Data Link

A Linked list is made up of nodes


Representation of a single linked list

Suppose we want to store the list of numbers: 15, 29, 54, 60

Node 15 1652 29 1503 54 2001 60 null


Memory address 1000 1001 1652 1653 1503 1504 2001 2002

1000
Pointer
LINKED LIST OPERATION

Reading
Read the value at index 2

19 1032 54 1040 29 1074 38 null


Memory
Address 1021 1022 1032 1033 1040 1041 1074 1075

1. To find the index, the program must begin at index 0 of the linked list.
2. Next follow the link from index 0 to index 1
3. Next follow the link from index 1 to index 2
Searching

Linked list has the same efficiency as the array.

19 1032 54 1040 29 1074 38 null


Memory
Address 1021 1022 1032 1033 1040 1041 1074 1075

• The program needs to start at the first cell and look through each and
every cell until it finds the value it’s searching for.

• In a worst-case scenario where the value we’re searching for is in the


final cell or not in the list altogether, it would take O(N) steps.
Algorithm of a linked list search

def index_of(value)

// We begin at the first node of the list:


current_node = first_node
current_index = 0

begin // If we find the data we're looking for, we return it:


If current_node.data == value
return current_index
end
// Otherwise, we move on the next node:
current_node = current_node.next_node
current_index += 1
end while current_node
//If we get through the entire list, without finding the data we will return nil

return nil
end
Insertion

• Insertion is one operation in which linked lists can have a distinct


advantage over arrays.

• Comparing with arrays, insertion in a linked list will only take one
step O(1).

“blue” “green” “red”


If we wanted to add "yellow" to the beginning of the list, all we would have
to do is create a new node and have it link to the node containing "blue":

“yellow” “blue” “green” “red”

A linked list provides the flexibility of inserting data to the front of the list
without requiring the shifting of any other data.

“yellow” “blue” “green” “red”


Supposing we want to inset “purple” to index 2. The actual insertion will
take one step.

“yellow” “blue” “green” “red”

“purple”
To be able to do this, it first needs to find the node at index 1 ("blue") so it
can modify its link to point to the newly created node.

“yellow” “blue” “green” “red”

“purple”

Inserting into the middle of a linked list takes O(N), just as it does for an
array.
Insertion comparison between Linked List and Array

Scenario Array Linked List


Insert at beginning Worst case Best case

Insert at middle Average case Average case

Insert at end Best case Worst case


Deletion

• Deletion is very similar to insertion in terms of efficiency.


• To delete a node from the beginning of a linked list, all we need to do is
perform one step: we change the first node of the linked list to now point
to the second node.

“yellow” “blue” “green” “red”

“purple”
Deletion comparison between Linked List and Array

Scenario Array Linked List


Delete at beginning Worst case Best case

Delete at middle Average case Average case

Delete at end Best case Worst case


Summary comparison between Linked List and Array

Operation Array Linked List


Read O(1) O(N)
Search O(N) O(N)
O(N) O(1) at
Insertion O(N) O(1) at end beginning

O(N) O(1) at
O(N) O(1) at end
Deletion beginning
Doubly Linked List

A doubly linked list is like a linked list, except that each node has two
links—one that points to the next node, and one that points to the
preceding node.

null “Joe” “Bob” “Ace” “Zoe” null

First Link to Link to next Last


node preceding node node
node
Doubly Linked list algorithm
class Node
attr_accessor :data, :next_node, :previous_node

def initialize(data)
@data = data
end
end

class DoublyLinkedList
attr_accessor :first_node, :last_node
def initialize(first_node=nil, last_node=nil)
@first_node = first_node
@last_node = last_node
end
end

Since a doubly linked list always knows where both its first and last nodes
are, we can access each of them in a single step, or O(1).
Insertion/Deletion

null “Joe” “Bob” “Ace” “Zoe” null “Sue” null

1. Create a new node (“Sue”) and have its previous node point to the last
node of the linked list (“Zoe”).
2. Change the next node of the last node (“Zoe”) to point to the new
node (“Sue”).
3. Declare the new node (‘Sue”) to be the last node in the linked list.

Inserting data at the end of a doubly linked list is just one step
class Node def remove_from_front
attr_accessor :data, :next_node, :previous_node removed_node = @first_node
def initialize(data) @first_node = @first_node.next_node
@data = data return removed_node
end end
end end

class DoublyLinkedList class Queue


attr_accessor :first_node, :last_node attr_accessor :queue
def initialize(first_node=nil, last_node=nil) def initialize
@first_node = first_node @queue = DoublyLinkedList.new
@last_node = last_node end
end
def enque(value)
def insert_at_end(value) queue.insert_at_end(value)
new_node = Node.new(value) end
# If there are no elements yet in the linked list:
if !first_node def deque
@first_node = new_node removed_node = @queue.remove_from_front
@last_node = new_node return removed_node.data
else end
new_node.previous_node = @last_node def tail
@last_node.next_node = new_node return @queue.last_node.data
@last_node = new_node end
end end
end

You might also like