HTML

html content help to improve the coding

Wednesday, 28 March 2012

SINGLY LINKED LISTSingly linked list


hat is a circular linked list?
In Circular Linked List, the list element are arranged in the same way as in the singly linked list having only one difference that the last element of the circular linked list always point to the first node of the list i.e. it means that last node does not point to NULL. In other words, we can say that the circular list is a list which does not have an end.
Thus, it is necessary in case of  circular linked to establish the first node and last node. It is useful if we set external pointer i.e. head to point to the last node in the list.
 We declare the structure for the circular linked list in the same way as we declare it for the singly or linear linked lists.
#include <stdio.h>

struct node
{
int num;
node*next;
};

typedef struct node NODE;
NODE *head,*first,*last;
How can we delete elements from a (singly) linked list ?
Similarly, like insertion we can  perform deletion in the following three manners :
  1. Deleting element from the front.
  2. Deleting element from the end.
  3. Deleting element from the specified position.
Following is a small code snippet which suggest how we can perform deletion in a singly linked list :

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
    {
int num;
struct node *next;
};                           /* declaring a global node of type struct */
typedef struct node NODE;    /* providing a type definition to the above created structure */
NODE *head=NULL;            /* declaring some of the global variable that would be used throughtout the program */
NODE *temp, *first;
int info;
void display();
void create_list();
void del_at_begin();
void del_at_end();
void del_at_specifiedpos();
void main()                 /* starting the main method() */
{
int i;
clrscr();
printf("\nprogram for deletion in a singly linked list :\n");
do
{
printf("\nEnter your choice :\n");        /*    creating menu for various deletion operations on the list */
printf("\n1.Create a linked list :");
printf("\n2.Delete from the front in the linked list :");
printf("\n3.Delete from the end position in the linked list :");
printf("\n4.Delete at any specified position in the linked list :");
printf("\n5.Exit\n");
fflush(stdin);
scanf("\n%d",&i);
switch(i)
{
case 1:
create_list();
display();
case 2:
del_at_begin();
display();
break;
case 3:
del_at_end();
display();
break;
case 4:
del_at_specifiedpos();
display();
break;
case 5:
exit(0);
}
}
while(i!=5);
getch();
}
void create_list()
{
printf("\nEnter your element in the linked list :");
scanf("%d",&info);
temp=(NODE *)malloc(sizeof(NODE));         /* allocating memory for the node to be inserted */
temp->num=info;
temp->next=NULL;
if(head==NULL)                            /* checking whether list is empty */
{
head=temp;
}
else
{
first=head;
while(first->next!=NULL)
{
first=first->next;
}
first->next=temp;

}
}
void display()
{
first=head;
printf("\nStatus of the linked list is as follows :\n");
while(first->next!=NULL)                           /* traversing the linked list */
{
printf("\n%d",first->num);
first=first->next;
}
}
void del_at_begin()
{
 if(head==NULL)
{
     printf("linked list is empty");
}
 else
{
    temp=head;
head=temp->next;
free(temp);
}

}
void del_at_end()
{
 if(head==NULL)

 {
  printf("\nlinked list is empty");
 }
else
{
     head=temp;
 while(temp->next!=NULL)
{
     first=temp;
 temp=temp->next;
}
    first->next=NULL;
free(temp);
}
}
void del_at_specifiedpos()
{
 int node_num;
 if(head==NULL)
{
    printf("\nlinked list is empty");
}
else
{
    temp=head;
 printf("\nEnter the position of the node you want to delete");
 scanf("\n%d",&node_num);
    for(int i=1;i<node_num;i++)
{
     first=temp;
 temp=temp->next;
}
first->next=temp->next;
free(temp);
}
}

Insertion in the (singly) linked list :
Based upon various type of insertions, the insertion in a singly linked list can be done in three ways :
  1. Insertion at the end of the list.
  2. Insertion at the beginning of the list.
  3. Insertion at the specified position in the list.
Now, following below is a program that incorporates all the tree types of insertion methods and shows how to do all these types of insertions.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
    {
int num;
struct node *next;
};                            /* declaring a global node of type struct */
typedef struct node NODE;    /* providing a type definition to the above created structure */
NODE *head=NULL;             /* declaring some of the global variable that would be used thought out the program */
NODE *temp, *first;
int info;
void display();
void insert_at_end();
void insert_at_begin();
void insert_at_specifiedpos();
void main()                                 /* starting the main method() */
{
int i;
clrscr();
printf("\nprogram for insertion in a singly linked list :\n");
do
{
printf("\nEnter your choice :\n");      /* creating menu for various insertion operations on the list */
printf("\n1.Insert element at the end of the linklist :");
printf("\n2.Insert element at the begin of the linklist :");
printf("\n3.Insert at any specified position in the linked list :");
printf("\n4.Exit\n");
fflush(stdin);
scanf("\n%d",&i);
switch(i)
{
case 1:
insert_at_end();
display();
break;
case 2:
insert_at_begin();
display();
break;
case 3:
insert_at_specifiedpos();
display();
break;
case 4:
exit(0);
}
}
while(i<=4);
getch();
}
void insert_at_end()
{
printf("\nEnter your element in the linked list :");
scanf("%d",&info);
temp=(NODE *)malloc(sizeof(NODE));         /* allocating memory for the node to be inserted */
temp->num=info;
temp->next=NULL;
if(head==NULL)                            /* checking whether list is empty */
{
head=temp;
}
else
{
first=head;
while(first->next!=NULL)
{
first=first->next;
}
first->next=temp;
}
}
void display()
{
first=head;
printf("\nStatus of the linked list is as follows :\n");
while(first->next!=NULL)                    /* traversing the linked list */
{
printf("\n%d",first->num);
first=first->next;
}
}
void insert_at_begin()
{
printf("\nEnter the value which do you want to insert at begining\n");
scanf("\n%d"&info);
first=head;
temp=(NODE *)malloc(sizeof(NODE));
temp->num=info;
temp->next=first;
head=temp;
}
void insert_at_specifiedpos()
{
int info1,info2;
printf("\nEnter the value after which you want to insert new node\n");
scanf("%d",&info1);
printf("\nEnter the value of new node\n");
scanf("\n%d",&info2);
temp=(NODE *)malloc(sizeof(NODE));
temp->num=info2;
temp->next=NULL;
first=head;
while(first->num!=info1)
{
first=first->next;
}
temp->next=first->next;
first->next=temp;
}
}



No comments:

Post a Comment