0% found this document useful (0 votes)
100 views29 pages

Singly Linked List

A linked list is a linear data structure where each element called a node contains a data field and a pointer to the next node. Nodes are linked together to form a chain by using the pointer in each node. Each node points to the next node by referencing its memory address. Linked lists allow for efficient insertion and removal of nodes throughout the list, but random access is slower than arrays as nodes must be accessed sequentially.

Uploaded by

Maham urooj
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)
100 views29 pages

Singly Linked List

A linked list is a linear data structure where each element called a node contains a data field and a pointer to the next node. Nodes are linked together to form a chain by using the pointer in each node. Each node points to the next node by referencing its memory address. Linked lists allow for efficient insertion and removal of nodes throughout the list, but random access is slower than arrays as nodes must be accessed sequentially.

Uploaded by

Maham urooj
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/ 29

LINKED LIST Singly linked list

LINKED LISTS
A B C 

Head
A linked list, or one-way list is a linear collection of data elements
called nodes, where linear order is given by means of pointer.

A linked list is a series of connected nodes


Each node contains at least
 A piece of data (any type)
 Pointer to the next node in the list
Head: pointer to the first node node
The last node points to NULL A

data pointer
WHAT IS LINK LIST??
A linked list is a way to store a collection of elements. Like an array these can be
character or integers. Each element in a linked list is stored in the form of a node.
Linked list is one of the most important data structures. We often face situations,
where the data is dynamic in nature and number of data can’t be predicted or the
number of data keeps changing during program execution. Linked lists are very useful
in this type of situations.
A node is a collection of two sub-elements or parts. A data part that stores the
element and a next part that stores the link to the next node.
LINKED LIST:

A linked list is formed when many such nodes are linked together to form a
chain. Each node points to the next node present in the order. The first
node is always used as a reference to traverse the list and is called HEAD.
The last node points to NULL.
LINKED LIST
Now let’s consider our previous list, used with an array
i.e. 2, 6, 8, 7, 1. Following is the figure which represents
the list stored as a linked list.
We also have a pointer current to point to the current
node of the list. We need this pointer to add or remove
current node from the list.
In the linked list, the current is a pointer and not an
index as we used while using an array.

2 6 8 7 1 

Head
Current
REPRESENTATION OF LINK
LIST IN COMPUTER MEMORY
BASIC OPERATIONS

Following are the basic operations supported by a list.


Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
INSERTING A NEW NODE
Possible cases of Insertion
1. Insert into an empty list
2. Insert in front
3. Insert at back
4. Insert in middle
But, in fact, only need to handle two cases
 Insert as the first node (Case 1 and Case 2)
 Insert in the middle or at the end of the list (Case 3 and Case
4)
INSERTING TO THE
FRONT
head 93
head 48 17 142

1. Allocate a new node


2. Insert new element
3. Have new node point to old head
4. Update head to point to new node
INSERTING TO THE END
head 48 17 142
//93 //

1. Allocate a new node


2. Insert new element
3. Have new node point to null
4. Have old last node point to new node
5. Update tail to point to new node
INSERTING TO THE
MIDDLE
head 17 48 142
93
//142 //

1. Allocate a new node


2. Insert new element
3. Go to the node that should follow the one to add
4. Have that node point to the new node
5. Have new node point to node next node to the found node
SEARCHING A LINKED LIST
Searching refers to finding a particular ITEM in the data.
Binary search algorithm can not be applied to sorted link list since there is no way
indexing the middle element in the list. This property is one of the main drawbacks
in using a linked list as a data structure
DELETING A NODE
 Possible cases of Deletion
1. Delete in front
2. Delete at back
3. Delete in middle

There are two special cases


 Delete first node
 Delete the node in middle or at the end of the list
THE SCENARIO
head

6 4 17 42

head

6 4 17 42

head

4 17 42

head

4 17 42
STRUCTURE
struct node node
{
A
int info;
struct node *next; data pointer
} *start;
CREATING NODES
void Create(int num)
{ int data;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
for(int i=1;i<=num;i++)
{
if(start==NULL) A Next NULL
{
cout<<"enter data for node"<<i<<": ";
cin>>data;
temp->info=data;
temp->next=NULL;
start=temp;
}
else
{
cout<<"enter data for node"<<": ";
cin>>data;
InsertEnd(data);
}
}
}
INSERT AT END
void InsertEnd(int data)
{
struct node* ptr,*tempnode;
ptr=start;
while(1)
{ if(ptr->next!=NULL)
ptr=ptr->next;
else
break;}
tempnode=(struct node*)malloc(sizeof(struct node));
tempnode->info=data; A Next B Next NULL

tempnode->next=NULL;
ptr->next=tempnode;}
//Insert node at start
newNode
void insertNode(int n){
//creating a newNode
struct Node *newNode=new Node;
5 Null

5000
//placing n at num of newnode
newNode->num=n;
//newnode’s next will save head Head
newNode->next=head;
//head now become new node 4400

}
head=newNode; 10 5000

4400
INSERT AT BEGINNING
void InsertBegin( int data)
{ struct node* tempnode;
tempnode=(struct node*)malloc(sizeof(struct node))
tempnode->info=data;
temonode->next=start;
start=tempnode;
}
A Next B Next C Next D Next Null

E Next

tempnode
INSERT AT NTH POSITION
In this example we will
void InsertPos(int data,int pos) assume that start node is at
{ position 0

struct node*tempnode,*ptr;
ptr=start;
for(int i=0;i< pos-1;i++)
A
{ Next B Next C Next D Next

if(ptr==NULL)
{cout<<"Invalid position";
return;
E Next
}
ptr=ptr->next;
}
if(ptr->next==NULL)
InsertEnd(data);
else
{tempnode=(struct node*)malloc(sizeof(struct node));
tempnode->info=data;
tempnode->next=ptr->next;
ptr->next=tempnode;
}
}
TRAVERSING A LINK LIST

Traversal means “visiting” or examining each node.


Simple linked list
 Start at the beginning
 Go one node at a time until the end
TRAVERSING
void PrintList()
{
node *ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->next;
}
cout<<endl;
}
DELETION
void Delete(int n)
{
struct node*temp1=start;
if(n==1)
{
start=temp1->next;
free(temp1);return;
}
for(int i=0;i<n-2;i++)
{
temp1=temp1->next;
struct node *temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);}
VARIATIONS OF LINKED
LISTS
Circular linked lists
 The last node points to the first node of the list

A B C

Head

 How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal
to the head.)
ADVANTAGES OF LINKED
LISTS
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
DISADVANTAGES OF LINKED
LISTS
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
ARRAY VERSUS LINKED
LISTS
Linked lists are more complex to code and manage than
arrays, but they have some distinct advantages.
Dynamic: a linked list can easily grow and shrink in size.
 We don’t need to know how many nodes will be in the list. They are created in memory
as needed.
 In contrast, the size of a C++ array is fixed at compilation time.
Easy and fast insertions and deletions
 To insert or delete an element in an array, we need to copy to temporary variables to
make room for new elements or close the gap caused by deleted elements.
 With a linked list, no need to move other nodes. Only need to reset some pointers.

You might also like