Lecture # 4 Link List Lecture - PPTX (Autosaved)
Lecture # 4 Link List Lecture - PPTX (Autosaved)
University of Sialkot
Before Link List
• Pointers
• Classes & objects
• Structures
• Arrays
• Memory allocation
Linked List
A linked list is a data structure which is built from struct
and pointers.
It forms a chain of "nodes" with pointers representing
links of the chain and holding the entire thing together.
A linked list can be represented by a diagram as follows
Start
NULL
This linked list has four nodes in it, each with a link to
next node in the series.
he last node has a link to the special value NULL, which a
pointer (whatever its type) can point to, to show that it
ast link in the chain.
There is also another special pointer, called Start, which
points to the first link in the chain so that we can keep
of it.
head
head
e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to
h e a d : T h e firs t n o d e o f a lin k e d lis t
ta il: T h e la s t n o d e o f a lin k e d lis t - it h a s a n u ll n e x t re fe re n c e .
head tail
e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to
• Singly
• Double
• Circular
Structure of linked list
data = val;
int data; Next= NULL;
node* next;
}
// node pointer to the next node in
the list };
Create object
int main()
{
Data : 1
Address: 0x0
Traversal in Singly Linked List
head A0 A1 A2
temp
tail
next
temp = temp->next;
Single linked list Operation:
• Traversing
• Deletion
• Insertion
• Searching
• Sorting
Traversing Single Linked List:
• Visited from beginning to end of the list
with Null occurs in the pointer object of the
list element.
• Display its elements or to count the number
of items or nodes in it.
Task
• Write a C++ code to count the number of
nodes in the linked list.
}
Member function to insert node
Specific Position
Deletion
Assignment # 2