Recent Post

Single Link List

In this list each and every nodes are linked with one another in only one direction from head node to tail node.Here each node has been divided into two parts-
1)Information/Data part- It holds any type of value which may be either numeric or string.
2)Pointer/Link part- It hold the address of successor node. 

Pointer start points first/head node link part of each node points to the next node.last nodes link part is NULL,if contains null in the link part.it means it is last node of the list.

Node of singly linked list

struct node
{
data_type variable_name;
struct node *link;
}
eg:-
struct node
{
int info;
struct node *link;
}


Singly linked list insertion in an empty list
                                  start=null
struct node
{
int info;
struct node*link;
}
struct node *tmp;
tmp=(struct node *)malloc (sizeof(struct node))
tmp--->info=10;
tmp--->link=null;
tmp--->info=info;
tmp--->link=link;
                                 start=tmp

Singly linked list insertion at the beginning
first check node is empty or not

struct node
{
int info;
struct node *link'
}
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node))
tmp--->info=1;
tmp--->link=start;
start=tmp;

Traversing a single linked list-
 
struct node *P;
P=start;
while(P!=null)
{
print P--->info;
P=P--->link;
}
 
Singly linked list insertion at the end-

struct node
{
int info;
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
struct node *P;
P=start;
while(P!=null)
P=P--->link;
P--->link=tmp;
tmp--->link=null;

Insertion at nth position in singly linked list-
P=start
for(i=1;i<position-1&&P!=null;i++)
P=P--->link
tmp--->link=P--->link;
P--->link=tmp;
 Deletion in singly linked list
  • Deletion of the only node
  • Deletion at the end (last node)
  • Deletion of the nth node
Deletion of the only node-

struct node *tmp;
tmp=start;
start=null;
free(tmp);
Deletion at the beginning of first node
struct node *tmp;
tmp=start;
start=start--->;
free(tmp);

Deletion at the end node (last node)
struct node *P;
P=start;
while(P--->link!=null)
{
f(P--->link--->info==data)
{
tmp=P--->;
P--->link=tmp--->;
free(tmp);
}
P=P--->link
}

No comments