Linear Single Linked Lists
Linear Single Linked Lists
Linked List can be defined as collection of objects called nodes that are randomly stored in the
memory.
A node contains two fields i.e. data stored at that particular address and the pointer which contains
the address of the next node in the memory.
The last node of the list contains pointer to the null.
Linked list is the data structure which can overcome all the limitations of an array. Using linked list is
useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available memory
space.
Each node divided into two parts: the first part contains data field and the 2 nd part called the „Link‟ filed
contains the address of the next node in the list. Such an address, which is used to access a particular
node, is known as a pointer. The entire linked list is accessed from an external pointer that points to
(contains the address of) the first node in the list. (By an “external” pointer, we mean one that is not
included within the node. Rather its value can be accessed directly by referencing a variable). Internal
pointer on the other hand is a pointer within the node. „next‟ is the internal pointer in the next example.
The above declaration defines a new data type, whose each element is of type node type.
data next
print(list->next);
}
C code:
node *insert_b4first(node *head)
{ node *new1;
new1=(node *)malloc(sizeof(node));
printf("\n Enter data for the new node");
scanf("%d",&new1->data);
new1->next=head;
return(new1);
}
list->next=new1;
new1->next=NULL;
}
Program:
node *insert(node *list)
{
int key, item;
node *new1,*n1;
printf(“\ n Enter key element after which you want to insert:”);
scanf(“%d”,&key);
n1=finda(list, key);
if(n1= =NULL)
printf(“\n key is not found”);
else
{ new1=(node *)malloc(sizeof(node));
printf(“\ n Enter new element to insert:”);
scanf(“%d”,&item);
new1->data=item;
new1->next=n1->next;
n1->next=new1;
}
return(list);
}
if(key= =list->data)
{ new1->next=list;
list=new1;
return(list);
}
else
{ n1=find b(list,key);
if(n1= =NULL)
printf(“\ n key is not found”);
else
{ new1->next=n1->next;
n1->next=new1;
}
}
return(list);
}
node *find b(node *l1,int key)
{
while(l1!=NULL)
{
if(l1->next->data= =key)
return(l1);
l1=l1->next;
}
return(NULL);
}
4. Deletion of node
Deletion of a node from linked list has the following three instances:
a) Deletion of head
b) Deletion of the last node
c) Deletion of a specified node.
void merge()
{
node *11, *list1,*list2;
list1=(node *)malloc(sizeof(nodel));
list2=(node *)malloc (sizof(node));
printf(“\n Enter data for List:”);
create(list);
printf(“\n Enter data for list2:”);
create(list2);
11=list;
while(11->next!=NULL)
11=11->next;
11->next=list2;
print(list);
}
char ch;
void create(node *);
void print (node *);
node *insert(node *);
node *inserta(node *);
node *inserta(node *);
node *finda(node *,int);
node *findb(node *,int);
node *del(node *);
void main()
{
node *head;
node *list,*new1,*n1;
char ans;
int opt, item;
while(1)
{
printf(“\n Press 1 to create a Linear Link List:”);
printf(“\n Press 2 to display Link List:”);
print(“\n Press 3 to insert a new node”);
print(“\n Press 4 to delete a node”);
print(“\n Press 5 to exit”);
print(“\n Choose your choice:”);
scanf(“%d”,&opt);
switch(opt)
{
case 1:
head=(node *)malloc(sizeof(node));
create(head);
}
}
Advantages:
1. Accessibility of a node in the forward direction is easier.
2. Insertion and deletion of nodes are easier.
Disadvantages:
1. Accessing the preceding node of a current node is not possible as there is no backward
traversal.
2. Accessing a node is time-consuming.