You are on page 1of 4

0000-7b3f-1660-1149-c7e.

txt
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
struct node *pre;
int info;
struct node *next;
}node;

void insert_at_pos(node **head,int pos,int data)


{
node *temp,*q;
q=*head;
temp=(node *)malloc(sizeof(node));
temp->info=data;
temp->pre=temp->next=NULL;
if(pos==1)
{ *head=temp;
temp->next=q;
q->pre=temp;
q=temp;
printf("\n node is inserted %d",data );
}
else
{
int i=1;
while(q->next!=NULL&&i<pos-1)
{
q=q->next;
i++;
}
if(i!=pos-1)
{
printf("\n invalid position ");
return;
}
temp->pre=q;
temp->next=q->next;
q->next->pre=temp;
q->next=temp;
printf("\n node is inserted : %d",data);
}
}

第 1 页
0000-7b3f-1660-1149-c7e.txt

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

void delete_at_position(node **head,int pos)


{ node *q;
node *temp;
q=*head;
temp=*head;
if(pos==1)
{
// temp=*head;
*head=q->next;
q->pre=NULL;
printf("\n VALue is deleted : ");
free(temp);
}
else
{ int i=1;
while(q->next!=NULL&&i<pos-1)
{
q=q->next;
i++;
}

if(i!=pos-1)
{
printf("\n Invalid position : ");
return;
}
temp=q->next;
q->next=temp->next;
q->next->pre=temp;
free(temp);
printf("\n value is deleted : ");

}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void merge(node **head,node **head1)
{
node *q,*temp;
q=*head;
temp=*head1;
第 2 页
0000-7b3f-1660-1149-c7e.txt
if(*head==NULL||*head1==NULL)
printf("\nLIST is empty");
else
{
while(q->next!=NULL)
q=q->next;
q->next=*head1;
temp->pre=q;

void display(node *head)


{
node *q;
q=head;
if(head==NULL)
{
printf("\n list is empty");
}
else
{
while(q!=NULL)
{
printf("\n value is %d ",q->info);
q=q->next;
}
}
}

// #ravibisht
main(/*RAVI BISHT*/)
{
int i;
node *start=NULL,*start1=NULL;
clrscr();
insert_at_pos(&start,1,10);
insert_at_pos(&start,2,20);
/*insert_at_pos(&start,3,30);
insert_at_pos(&start,4,40);
第 3 页
0000-7b3f-1660-1149-c7e.txt
insert_at_pos(&start,5,50);
*/
insert_at_pos(&start1,1,50);
insert_at_pos(&start1,2,60);
/* insert_at_pos(&start1,2,70);
insert_at_pos(&start1,3,80);
insert_at_pos(&start1,4,90);
insert_at_pos(&start1,5,100); */
display(start1);
display(start);
merge(&start,&start1);
display(start);
//delete_at_position(&start,1);
// for(i=1;i<=5;i++)
delete_at_position(&start,1);
delete_at_position(&start1,2);
display(start);
//printf("\n now values are deleteing : ");
// display(start);

getch();
return;
}

第 4 页

You might also like