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

Lecture 5 - Linked List Variations

data structures concept

Uploaded by

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

Lecture 5 - Linked List Variations

data structures concept

Uploaded by

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

CS-2001

Data Structures
Fall 2022
Link List – Variations and Applications

Mr. Muhammad Usman Joyia


National University of Computer
and Emerging Sciences,
Faisalabad, Pakistan.
Roadmap

Previous Lecture
• Introduction to linked lists
• A pointer-based implementation in C++

Today
• Pors and Cons of linked lists
• Variations of linked lists
– Doubly linked lists
– Circular Linked lists

2
Linked List – Advantages
• Access any item as long as link to first item is maintained

• Insert new item without shifting

• Delete existing item without shifting

• Can expand/contract (flexible) as necessary

3
Linked List – Disadvantages (1)
• Overhead of links
– Extra space for pointers with each item-node, pure overhead

• Dynamic, must provide


– Destructor (to destroy all the dynamic allocations one-by-one)

• No longer have direct access to each element of the list


– Many sorting algorithms need direct access
– Binary search needs direct access

• Access of nth item now less efficient


– Must go through first element, then second, and then third,
etc.

4
Linked List – Disadvantages (2)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list

Array Linked List


a[size++] = value;

5
Linked List – Disadvantages (3)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list

Array Linked List


a[size++] = value; Get a new node;
Set data part = value
next part = null_value

6
Linked List – Disadvantages (4)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list

Array Linked List


a[size++] = value; Get a new node;
Set data part = value
next part = null_value
If list is empty
Set head to point to new node

7
Linked List – Disadvantages (5)
• List-processing algorithms that require fast access to each
element cannot be done as efficiently with linked lists
• Consider adding an element at the end of the list

Array Linked List


a[size++] = value; Get a new node;
Set data part = value
next part = null_value
If list is empty
Set head to point to new node
Else
Traverse list to find last node
Set next part of last node to
This is the inefficient part point
to new node

8
Some Applications
• Main: Implement many other abstract datatypes such as
stacks, queues, binary trees, and fib. heaps etc.

• Applications that maintain a Most Recently Used (MRU) list


– For example, a linked list of file names
• Cache in the browser that allows to hit the BACK button
– A linked list of URLs
• Undo functionality in Photoshop or Word processor
– A linked list of state
• A list in the GPS of the turns along your route

Can we traverse the linked list in the reverse


direction!

9
Reverse the list

• Input: head ->30->25->20->15->10->5

• Reversed : head ->5->10->15->20->25->30

• Use three pointer (Current, previous, Next)


• While traversing the list just invert the links
• When final node reaches then store the address of end
node in the head pointer

10
void List :: Reverse(){

1. Node *upcoming,*prev, *current;


2. current = head;
3. prev = NULL;
4. while (current!=NULL)
5. {
6. upcoming = current->next;
7. current->next=prev;
8. prev = current;
9. current = upcoming;
10. }
11. head = prev;
}
11
Doubly Linked List

12
Doubly Linked List
• Every node contains the address of the previous node
except the first node
– Both forward and backward traversal of the list is possible

next

x a b c

hea prev tail


d

13
Node Class
• Node class contains three data members
– data: double-type data in this example
– next: a pointer to the next node in the list
– Prev: a pointer to the pervious node in the list

class Node {
public:
double data; // data
Node* next; // pointer to next
Node* prev; // pointer to previous
};

14
List Class
• List class contains two pointers
– head: a pointer to the first node in the list
– tail: a pointer to the last node in the list
– Since the list is empty initially, head and tail are set to NULL

class List {
public:
List(void) { head = NULL; tail = NULL; } //
constructor
~List(void); //
destructor

. . .

private:
Node* head;
Node* tail;
};
15
Adding First Node

hea a tail
d

// Adding first node


head = new Node;
head->next = null;
head->prev = null;
tail = head;

16
Inserting a Node in Doubly Linked List (1)
• To add a new item after the linked list node pointed by
current

x a c

hea tail
d current

newNode = new Node


newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

17
Inserting a Node in Doubly Linked List (2)
• To add a new item after the linked list node pointed by
current

x a c

hea b
d tail
current

newNode = new DoublyLinkedListNode


newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

18
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current

x a c

hea b tail
d current

newNode = new DoublyLinkedListNode


newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

19
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current

x a c

hea b tail
d current

newNode = new DoublyLinkedListNode


newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

20
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current

x a c

hea b
d current tail

newNode = new DoublyLinkedListNode


newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode; //Current->next = newNode
newNode->next->prev = newNode;
current = newNode;

21
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current

x a c

hea b tail
d current

newNode = new DoublyLinkedListNode


newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

22
Inserting a Node in Doubly Linked List (3)
• To add a new item after the linked list node pointed by
current

x a c

hea b tail
d current
newNode = new DoublyLinkedListNode
newNode->prev = current;
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

23
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from
the list

x a c

hea b
d current

oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
24
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list

x a c

hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
25
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list

x a c

hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
26
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list

x a c

hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
27
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list

x a c

hea b
d current
oldNode
oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
28
Deleting a Node From Doubly Linked List
• Suppose current points to the node to be deleted from the
list

x a c

hea
d current

oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
current = oldNode->prev;
delete oldNode;
29
Any Question So Far?

33

You might also like