Create a doubly linked list & insert a node in any position
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
struct list *prev;
int info;
struct list *next;
};
typedef struct list node;
node *start;
main()
{
void create();
void show();
int m,data;
void insert(int,int);
start=NULL;
create();
printf("\nThe Linked List :\n");
show();
printf("\nEnter the node no.:");
scanf("%d",&m);
printf("\nEnter the value for new node :");
scanf("%d",&data);
insert(m,data);
printf("\nThe Linked List after insertion:\n");
show();
getch();
}
void create()
{
node *ptr,*temp,*new_node;
int n,val,i;
printf("\nEnter the no. of nodes :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\nOverflow");
return;
}
printf("\nEnter the value for node %d :",i);
scanf("%d",&val);
new_node->info=val;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
ptr=new_node;
start=ptr;
}
else
{
ptr->next=new_node;
new_node->prev=ptr;
ptr=ptr->next;
}
}
return;
}
void show()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr->info);
else
printf("%d-->",ptr->info);
ptr=ptr->next;
}
return;
}
void insert(int p,int item)
{
node *ptr,*temp,*new_node;
int i;
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\nOverflow");
return;
}
new_node->info=item;
ptr=start;
temp=NULL;
for(i=1;i<p;i++)
{
temp=ptr;
ptr=ptr->next;
}
new_node->prev=temp;
temp->next=new_node;
new_node->next=ptr;
ptr->prev=new_node;
return;
}
#include<conio.h>
#include<stdlib.h>
struct list
{
struct list *prev;
int info;
struct list *next;
};
typedef struct list node;
node *start;
main()
{
void create();
void show();
int m,data;
void insert(int,int);
start=NULL;
create();
printf("\nThe Linked List :\n");
show();
printf("\nEnter the node no.:");
scanf("%d",&m);
printf("\nEnter the value for new node :");
scanf("%d",&data);
insert(m,data);
printf("\nThe Linked List after insertion:\n");
show();
getch();
}
void create()
{
node *ptr,*temp,*new_node;
int n,val,i;
printf("\nEnter the no. of nodes :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\nOverflow");
return;
}
printf("\nEnter the value for node %d :",i);
scanf("%d",&val);
new_node->info=val;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
ptr=new_node;
start=ptr;
}
else
{
ptr->next=new_node;
new_node->prev=ptr;
ptr=ptr->next;
}
}
return;
}
void show()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
if(ptr->next==NULL)
printf("%d",ptr->info);
else
printf("%d-->",ptr->info);
ptr=ptr->next;
}
return;
}
void insert(int p,int item)
{
node *ptr,*temp,*new_node;
int i;
new_node=(node *)malloc(sizeof(node));
if(new_node==NULL)
{
printf("\nOverflow");
return;
}
new_node->info=item;
ptr=start;
temp=NULL;
for(i=1;i<p;i++)
{
temp=ptr;
ptr=ptr->next;
}
new_node->prev=temp;
temp->next=new_node;
new_node->next=ptr;
ptr->prev=new_node;
return;
}
No comments