Chapter 3 Linked List
Chapter 3 Linked List
Bedasa Wayessa
[email protected]
LinkedList
Heap
int main(){
int A[4]
int A[4] == {1,
{1,2,
2,3,3,4};
4}
A 1 2 3 4 p Stack
int *p;
pp == new
new int[4];
int[4];
}
N1 next N2 next N3 next null
The first node of the linked list is called “head” while the last node is
called “Tail”.
The last node of the linked list will have its next pointer as NULL
since it will not have any memory address pointed to.
class Node {
public:
int value;
Node * Next;
};
Note that, since we don't initialize the Next pointer for all nodes, it will
be automatically filled with the null pointer (NULL).
7 NULL 14 NULL 21 NULL
7 14 21 NULL
Node *head, *tail; 7 14 Null
• We can define these
tail = head
tail = head->next
head = head->next
• If head has no pointer it is a null or 0.
• To check whether pointer is pointing to next Node
If (head->next != Null){}
Types Details
Single Link List • SLL contains only one link for each node which points to the
next link and therefore can navigate only forward direction.
Double Link List • DLL contains two link for each node.
• The 1st link points to the previous node and the 2nd link
points to the next link.
• Therefore can navigate in bot direction.
Circular Link List • CLL the last node contains link next, which points to the
value field of the 1st node and the 1st node link field has a link
as previous, which points to the last node value field.
Data Structures and Algorithms - CoSc2091 25
Singly Linked List
– The Singly Linked List (also known as the linked list) is a sequence of
items linked with each other.
– It's actually a chaining of nodes, where each node contains the item's
value and the next pointer.
– In other words, each item in the linked list has a link to its next item in
the sequence.
– Traversal is allowed only one way and there is no going back.
– The thing that differs between the linked list and the node chain is
that the linked list has a Head and a Tail pointer.
– The Head informs the first item and the Tail informs the last item in
the linked list.
head = head->next;
head
Output:
9 -> 10 -> 11 -> Null
--------------------------------
LinkedList::~LinkedList() {
Node *p = first;
while (first) {
first = first->next; Note: read cpp scope
delete p; resolution(::).
p = first;
}
– implement all this
}
operation using
scope resolution in
cpp.
else {
p = first;
for (int i = 0; i < index - 1; i++) {
q = p;
p = p->next;
}
q->next = p->next;
x = p->data;
delete p;
}
return x;
}
prev data next prev data next prev data next
Node1 Node2 Node3
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList(int data);
void addNodeHead(int data);
void addNodeEnd(int data);
};
Data Structures and Algorithms - CoSc2091 61
Doubly Linked List
– Representation of a Doubly Linked List:
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList(int data);
void addNodeHead(int data);
void addNodeEnd(int data);
};
class Node {
public:
int data; prev data next
Node * prev;
Node1
Node * next;
};
Node * head;
Node * tail;
prev data next prev data next
DLL();
DLL(int x); Node1 Node2
void fdisplay();
void bdisplay();
void addAtEnd(int x);
void addAtHead(int x);
void insertAtIndex(int pos, int x);
int m_count;
};
DLL::DLL(int x){
head = new Node(); //new Node
head->data = x; //set data to the node
head->next = NULL; //next ptr is null
head->prev = NULL; //prev ptr is null
tail = head; //since 1 node tail&head
m_count++;
}
Data Structures and Algorithms - CoSc2091 76
Doubly Linked List
– Common Doubly Linked List Operations
1. Add a Node in a Doubly Linked List at Head
void DLL::addAtHead(int x){
Node *temp = new Node(); //create temp Node
temp->data = x; //assign data to it
data next data next data next
Node1 Node2 Node3
data next data next data next
Node1 Node2 Node3
int k = 6;
10 next 2 next 6 next
while(k--){
Node1 Node2 Node3 cout<<ptr->data<<" -> ";
ptr = ptr->next;
}
Circular output: return 0;
10 -> 2 -> 6 -> 10 -> 2 -> 6 -> }
--------------------------------
Data Structures and Algorithms - CoSc2091 86
Assignment
• Work on:
– How to count number of Node in LinkedList with example?
– How to Search an Element in LinkedList with example?
– How to sort Elements of a Linked List with example?
– How to add data in LinkedList with example?
• As arrays and linked lists are both used to store items and are linear data
structures, both these structures can be used in similar ways for most of
the applications.