0% found this document useful (0 votes)
9 views12 pages

PPPP

Uploaded by

prantoriyad713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

PPPP

Uploaded by

prantoriyad713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

8.

Double linked list insert at first node:

#include<stdio.h>

#include<stdlib.h>

struct NODE{

int data;

struct NODE* next;

struct NODE* prev;

};

struct NODE *add(struct NODE *head,int d){

struct NODE *temp=(struct NODE*)malloc(sizeof(struct NODE));

temp->data=d;

temp->prev=NULL;

temp->next=head;

head->prev=temp;

head=temp;

return head;

};

int main(){

struct NODE *head=NULL,*current=NULL,*ptr=NULL;

head= (struct NODE*)malloc(sizeof(struct NODE));

current=(struct NODE*)malloc(sizeof(struct NODE));

ptr=(struct NODE*)malloc(sizeof(struct NODE));

head->data=20;

current->data=30;

ptr->data=40;

head->prev=NULL;

current->prev=head;
ptr->prev=current;

head->next=current;

current->next=ptr;

ptr->next=NULL;

int d=10;

head=add(head,d);

while(head!=NULL){

printf("%d <->",head->data);

head=head->next;

printf("NULL \n");

return 0;

9.Double linked list insert at last node

#include<stdio.h>

#include<stdlib.h>

struct NODE{

int data;

struct NODE* next;

struct NODE* prev;

};

void add(struct NODE* head,int d){

struct NODE *ne=head,* temp=(struct NODE*)malloc(sizeof(struct NODE));

temp->data=d;

temp->next=NULL;

while(ne->next!=NULL){
ne=ne->next;

ne->next=temp;

temp->prev=ne;

int main(){

struct NODE *head=NULL,*current=NULL,*ptr=NULL;

head= (struct NODE*)malloc(sizeof(struct NODE));

current=(struct NODE*)malloc(sizeof(struct NODE));

ptr=(struct NODE*)malloc(sizeof(struct NODE));

head->data=20;

current->data=30;

ptr->data=40;

head->prev=NULL;

current->prev=head;

ptr->prev=current;

head->next=current;

current->next=ptr;

ptr->next=NULL;

int d=10;

add(head,d);

while(head!=NULL){

printf("%d<->",head->data);

head=head->next;

printf("NULL");

return 0;
}

10.double linked list insert ta middle

#include<stdio.h>

#include<stdlib.h>

struct NODE{

int data;

struct NODE* next;

struct NODE* prev;

};

void add(struct NODE* head,int d){

struct NODE *ne=head,* temp=(struct NODE*)malloc(sizeof(struct NODE));

temp->data=d;

temp->next=NULL;

while(ne->data!=30){

ne=ne->next;

temp->prev=ne;

temp->next=ne->next;

ne->next=temp;

int main(){

struct NODE *head=NULL,*current=NULL,*ptr=NULL;

head= (struct NODE*)malloc(sizeof(struct NODE));

current=(struct NODE*)malloc(sizeof(struct NODE));

ptr=(struct NODE*)malloc(sizeof(struct NODE));

head->data=20;

current->data=30;
ptr->data=40;

head->prev=NULL;

current->prev=head;

ptr->prev=current;

head->next=current;

current->next=ptr;

ptr->next=NULL;

int d=10;

add(head,d);

while(head!=NULL){

printf("%d<->",head->data);

head=head->next;

printf("NULL");

return 0;

11.double linked list delation at first node

#include<stdio.h>

#include<stdlib.h>

struct NODE{

int data;

struct NODE* next;

struct NODE* prev;

};

struct NODE *delet(struct NODE *head){

struct NODE *temp=head;

if(temp->next==NULL){
printf("THE LIST IS EMPTY!");

}else{

struct NODE* p=temp;

temp=temp->next;

free(p);

p=NULL;

temp->prev=NULL;

head=temp;

return head;

};

int main(){

struct NODE *head=NULL,*current=NULL,*ptr=NULL;

head= (struct NODE*)malloc(sizeof(struct NODE));

current=(struct NODE*)malloc(sizeof(struct NODE));

ptr=(struct NODE*)malloc(sizeof(struct NODE));

head->data=20;

current->data=30;

ptr->data=40;

head->prev=NULL;

current->prev=head;

ptr->prev=current;

head->next=current;

current->next=ptr;

ptr->next=NULL;

head=delet(head);

while(head!=NULL){
printf("%d<->",head->data);

head=head->next;

printf("NULL");

return 0;

11.double linked list deletion at last node

#include<stdio.h>

#include<stdlib.h>

struct NODE{

int data;

struct NODE* next;

struct NODE* prev;

};

struct NODE *delet(struct NODE *head){

struct NODE *temp=head;

if(temp->next==NULL){

printf("THE LIST IS EMPTY!");

}else{

struct NODE* temp1=temp;

struct NODE* temp2=temp;

while(temp2->next!=NULL){

temp1=temp2;

temp2=temp2->next;

temp1->next=temp2->next;

temp2->prev=NULL;
free(temp2);

head=temp;

return head;

};

int main(){

struct NODE *head=NULL,*current=NULL,*ptr=NULL,*top=NULL;

head= (struct NODE*)malloc(sizeof(struct NODE));

current=(struct NODE*)malloc(sizeof(struct NODE));

ptr=(struct NODE*)malloc(sizeof(struct NODE));

top=(struct NODE*)malloc(sizeof(struct NODE));

head->data=20;

current->data=30;

ptr->data=40;

top->data=50;

head->prev=NULL;

current->prev=head;

ptr->prev=current;

top->prev=ptr;

head->next=current;

current->next=ptr;

ptr->next=top;

top->next=NULL;

head=delet(head);

while(head!=NULL){

printf("%d<->",head->data);

head=head->next;
}

printf("NULL");

return 0;

13.double linked list delation at middle node

#include<stdio.h>

#include<stdlib.h>

struct NODE{

int data;

struct NODE* next;

struct NODE* prev;

};

struct NODE *delet(struct NODE *head){

struct NODE *temp=head;

if(temp->next==NULL){

printf("THE LIST IS EMPTY!");

}else{

struct NODE* temp1=temp;

struct NODE* temp2=temp;

while(temp2->data!=40){

temp1=temp2;

temp2=temp2->next;

temp1->next=temp2->next;

temp2->prev=NULL;

free(temp2);

}
head=temp;

return head;

};

int main(){

struct NODE *head=NULL,*current=NULL,*ptr=NULL,*top=NULL;

head= (struct NODE*)malloc(sizeof(struct NODE));

current=(struct NODE*)malloc(sizeof(struct NODE));

ptr=(struct NODE*)malloc(sizeof(struct NODE));

top=(struct NODE*)malloc(sizeof(struct NODE));

head->data=20;

current->data=30;

ptr->data=40;

top->data=50;

head->prev=NULL;

current->prev=head;

ptr->prev=current;

top->prev=ptr;

head->next=current;

current->next=ptr;

ptr->next=top;

top->next=NULL;

head=delet(head);

while(head!=NULL){

printf("%d<->",head->data);

head=head->next;

printf("NULL");
return 0;

14.

Merged two linked list

#include<stdio.h>

#include<stdlib.h>

struct Node{

int data;

struct Node*next;

};

struct Node*createNode(int d)

struct Node*newNode=(struct Node*)malloc (sizeof(struct Node));

newNode->data=d;

newNode->next=NULL;

return newNode;

struct Node*MergeList(struct Node*list1,struct Node*list2){

if(!list1)return list2;

if(!list2)return list1;

if(list1->data<list2->data){

list1->next=MergeList(list1->next,list2);

return list1;

}else{

list2->next=MergeList(list1,list2->next);
return list2;

void PrintList(struct Node*head){

while(head!=NULL){

printf("%d",head->data);

head=head->next;

int main (){

struct Node*list1=(struct Node*)malloc (sizeof(struct Node));

list1=createNode(1);

list1->next=createNode(3);

list1->next->next=createNode(5);

struct Node*list2=(struct Node*)malloc (sizeof(struct Node));

list2=createNode(2);

list2->next=createNode(4);

list2->next->next=createNode(6);

struct Node*MergedList=MergeList(list1,list2);

PrintList(MergedList);

You might also like