Singly 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.
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
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
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.