Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views41 pages

DS - Unit - 1 - Doubly LL Programs

Uploaded by

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

DS - Unit - 1 - Doubly LL Programs

Uploaded by

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

Doubly LinkedList Programs

Program 1: WAP in C to Create and Display a Doubly LinkedList

Program
#include <stdio.h>

#include <stdlib.h>

/** Basic structure of Node */

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

/** Function used in this program */

void createList(int n);

void displayList();

int main()

int numnode;

printf("enter the number of nodes in doubly linkedlist");

scanf("%d",&numnode);

createList(numnode);
displayList();

return 0;

/** Creates a doubly linked list of n nodes. Number of nodes to be created */

void createList(int n)

int i, data;

struct node *newNode;

/* Initially Create and linked list ony with one node (firstnode) */

firstnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");

scanf("%d", &data);

firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/* * Create and link rest of the n-1 nodes */

for(i=2; i<=n; i++)

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

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;
// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node

printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;
printf("Linked LIST Is:\n");

while(temp != NULL)

printf("%d\t", temp->data);

/* Move the current pointer to next node */

temp = temp->next;

Output
enter the number of nodes in doubly linkedlist 4

Enter data of 1 node: 22

Enter data of 2 node: 23

Enter data of 3 node: 24

Enter data of 4 node: 25

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

22 23 24 25
Program 2 – WAP in C to Insert a new node at the beginning of DLL

Program
#include <stdio.h>

#include <stdlib.h>

/** Basic structure of Node */

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

void createList(int n);

void displayList();

void insertAtBeginning(int data);

int main()

int numnode;

int value;

printf("enter the number of nodes in doubly linkedlist");

scanf("%d",&numnode);

createList(numnode);

displayList();
printf("\n enter the data filed value new node to be inseerted at begining");

scanf("%d",&value);

insertAtBeginning(value);

displayList();

return 0;

/**

* Creates a doubly linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

int i, data;

struct node *newNode;

if(n >= 1)

/*

* Initiallt Create and linked list ony with one node (firstnode)

*/

firstnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");


scanf("%d", &data);

firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/*

* Create and link rest of the n-1 nodes

*/

for(i=2; i<=n; i++)

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

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node


}

printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

int n = 1;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked LIST Is:\n");

while(temp != NULL)
{

printf("%d\t", temp->data);

n++;

/* Move the current pointer to next node */

temp = temp->next;

/** Pseudo code or Function to insert a new node at the beginning of doubly
Linked List is given */
void insertAtBeginning(int data)

struct node * newNode ;

if(head == NULL)

printf("Error, List is Empty!\n");

else

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

newNode->data = data;

newNode->next = head; // Point to next node which is currently head


newNode->prev = NULL; // Previous node of first node is NULL

/* Link previous address field of head with newnode */

firstnode->prev = newNode;

/* Make the new node as head node */

head = newNode;

printf("NODE INSERTED SUCCESSFULLY AT THE BEGINNING OF THE


LIST\n");

OUTPUT

enter the number of nodes in doubly linkedlist 3

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

21 22 23

enter the data filed value new node to be inseerted at begining

25

NODE INSERTED SUCCESSFULLY AT THE BEGINNING OF THE LIST

Linked LIST Is:

25 21 22 23
Program 3 – WAP in C to insert a newnode at the end of Doubly Linked List.

Program

#include <stdio.h>

#include <stdlib.h>

/*

* Basic structure of Node

*/

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

void createList(int n);

void displayList();

void insertAtEnd(int data);

int main()

int numnode;

int value;

printf("enter the number of nodes in doubly linkedlist");


scanf("%d", &numnode);

createList(numnode);

displayList();

printf("\n enter the data filed value new node to be inseerted at the end of doubly linked list");

scanf("%d",&value);

insertAtEnd(value);

displayList();

return 0;

/**

* Creates a doubly linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

int i, data;

struct node *newNode;

/*

* Initiallt Create and linked list ony with one node (firstnode)

*/

firstnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");

scanf("%d", &data);
firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/*

* Create and link rest of the n-1 nodes

*/

for(i=2; i<=n; i++)

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

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node

}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked LIST Is:\n");

while(temp != NULL)

printf("%d\t", temp->data);
/* Move the current pointer to next node */

temp = temp->next;

/** Pseudo Code or Function to Insert newnode at the end of Doubly Linked List*/

void insertAtEnd(int data)

struct node * newNode;

if( head == NULL)

printf("Error, List is empty!\n");

else

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

newNode->data = data;

struct node *temp;

temp=head;

while(temp->next != NULL)

temp=temp->next;

}
temp->next=newNode;

newNode->prev=temp;

newNode->next=NULL;

printf("NODE INSERTED SUCCESSFULLY AT THE END OF LIST\n");

OUTPUT

enter the number of nodes in doubly linkedlist

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

21 22 23

enter the data filed value new node to be inserted at the end of doubly linked list

24

NODE INSERTED SUCCESSFULLY AT THE END OF LIST

Linked LIST Is:

21 22 23 24
Program 4: WAP in C to Insert a newnode after a given node in Doubly Linked List

Program

#include <stdio.h>

#include <stdlib.h>

/*

* Basic structure of Node

*/

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

void createList(int n);

void displayList();

void insertAfter(int data1,int data);

int main()

int numnode;

int value,value1;

printf("enter the number of nodes in doubly linkedlist");

scanf("%d",&numnode);
createList(numnode);

displayList();

printf("\n enter the data filed value new node to be inserted in doubly linked list");

scanf("%d",&value);

printf("enter the data field value of that node after which you want to insert new node");

scanf("%d", &value1);

insertAfter(value1,value);

displayList();

return 0;

/**

* Creates a doubly linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

int i, data;

struct node *newNode;

/*

* Initiallt Create and linked list ony with one node (firstnode)

*/

firstnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");

scanf("%d", &data);
firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/*

* Create and link rest of the n-1 nodes

*/

for(i=2; i<=n; i++)

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

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node

}
printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked LIST Is:\n");

while(temp != NULL)

printf("%d\t", temp->data);

n++;
/* Move the current pointer to next node */

temp = temp->next;

/** PseudoCode or Function to Insert newnode after a given node of the doubly linked list
is given below*/

void insertAfter(int data1,int data2)

struct node *newnode = (struct node *)malloc(sizeof(struct node));

struct node *temp1,*temp2;

temp1=head;

temp2=temp1;

while(temp2->data!=data1)

temp2=temp1;

temp1=temp1->next;

newnode->data = data2;

newnode->next = temp1;

newnode-> prev = temp2;

temp2->next = newnode;

temp1->prev=newnode;

printf("Node Inserted\n");

}
Note – In this program variable data1 is used for the value of that node after which you want to
insert new node and variable data2 is used for the data field value of newnode to be inserted.

OUTPUT

enter the number of nodes in doubly linkedlist 4

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

Enter data of 4 node: 24

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

21 22 23 24

enter the data filed value new node to be inserted in doubly linked list 25

enter the data field value of that node after which you want to insert new node

23

Node Inserted

Linked LIST Is:

21 22 23 25 24
DELETION OPERATION IN DOUBLY LINKED LIST
Program 5 – WAP in C to delete first node of doubly Linked List or Delete node from
beginning.

Program

#include <stdio.h>

#include <stdlib.h>

/** Basic structure of Node */

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

void createList(int n);

void displayList();

void DeleteBeginning();

int main()

int numnode;

int value;

printf("enter the number of nodes in doubly linkedlist");

scanf("%d",&numnode);
createList(numnode);

displayList() ;

DeleteBeginning();

displayList();

return 0;

/**

* Creates a doubly linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

int i, data;

struct node *newNode;

/*

* Initiallt Create and linked list ony with one node (firstnode)

*/

firstnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");

scanf("%d", &data);

firstnode->data = data;
firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/*

* Create and link rest of the n-1 nodes

*/

for(i=2; i<=n; i++)

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

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node

printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");


}

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked LIST Is:\n");

while(temp != NULL)

printf("%d\t", temp->data);

n++;

/* Move the current pointer to next node */


temp = temp->next;

/** Pseudo code or Function to delete a new node at the beginning of doubly Linked List is
given */

void DeleteBeginning( )

struct node * newNode ;

if(head == NULL)

printf("Error, List is Empty!\n");

else

struct node *temp;

temp=head;

head=head->next;

head->prev=NULL;

free(temp);

printf("First Node Deleted SUCCESSFULLY AT THE BEGINNING OF THE


LIST\n");

}
OUTPUT

enter the number of nodes in doubly linkedlist 4

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

Enter data of 4 node: 24

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

21 22 23 24 First Node Deleted SUCCESSFULLY AT THE BEGINNING OF


THE LIST

Linked LIST Is:

22 23 24

Program 6 – WAP in C to Delete the Last node of Doubly Linked List

Program

#include <stdio.h>

#include <stdlib.h>

/** Basic structure of Node */

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;


struct node *head;

void createList(int n);

void displayList();

void DeletefromEnd();

int main()

int numnode;

int value;

printf("enter the number of nodes in doubly linkedlist");

scanf("%d",&numnode);

createList(numnode);

displayList();

DeletefromEnd();

displayList();

return 0;

/**

* Creates a doubly linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

{
int i, data;

struct node *newNode;

if(n >= 1)

/*

* Initiallt Create and linked list ony with one node (firstnode)

*/

firstnode = (struct node *)malloc(sizeof(struct node));

printf("Enter data of 1 node: ");

scanf("%d", &data);

firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/*

* Create and link rest of the n-1 nodes

*/

for(i=2; i<=n; i++)

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


printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;

lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node

printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

int n = 1;
if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked LIST Is:\n");

while(temp != NULL)

printf("%d\t", temp->data);

n++;

/* Move the current pointer to next node */

temp = temp->next;

}
/** Pseudo code or Function to delete Last node of doubly Linked List is given */

void DeletefromEnd( )

struct node * newNode ;

if(head == NULL)

printf("Error, List is Empty!\n");

else

struct node *temp1,*temp2;

temp1=head;

temp2=head;

while( temp1->next !=NULL)

temp2=temp1;

temp1=temp1->next ;

temp2->next=NULL;

free(temp1);

printf("Last Node Deleted SUCCESSFULLY AT THE BEGINNING OF THE LIST\n");

}
Output

enter the number of nodes in doubly linkedlist 4

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

Enter data of 4 node: 24

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:

21 22 23 24 Last Node Deleted SUCCESSFULLY AT THE BEGINNING OF


THE LIST

Linked LIST Is:

21 22 23
Program 7 – WAP in C to delete a node after a given node in Doubly Linked List

Program

#include <stdio.h>

#include <stdlib.h>

/** Basic structure of Node */

struct node {

int data;

struct node * prev;

struct node * next;

};

struct node *firstnode;

struct node *lastnode;

struct node *head;

void createList(int n);

void displayList();

void DeleteAfter(int value);

int main()

int numnode;

int value1;

printf("enter the number of nodes in doubly linkedlist");

scanf("%d",&numnode);
createList(numnode);

displayList();

printf("enter the data field value of that node after which you want to delete node");

scanf("%d", &value1);

DeleteAfter(value1);

displayList();

return 0;

/**

* Creates a doubly linked list of n nodes.

* @n Number of nodes to be created

*/

void createList(int n)

int i, data;

struct node *newNode;

if(n >= 1)

/*

* Initiallt Create and linked list ony with one node (firstnode)

*/

firstnode = (struct node *)malloc(sizeof(struct node));


printf("Enter data of 1 node: ");

scanf("%d", &data);

firstnode->data = data;

firstnode->prev = NULL;

firstnode->next = NULL;

lastnode = firstnode;

head=firstnode;

/*

* Create and link rest of the n-1 nodes

*/

for(i=2; i<=n; i++)

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

printf("Enter data of %d node: ", i);

scanf("%d", &data);

newNode->data = data;

// add newly creted node in the end of linkedlist

newNode->prev = lastnode; // Link new node with the previous node

newNode->next = NULL;
lastnode->next = newNode; // Link previous node with the new node

lastnode = newNode; // Make new node as last/previous node

printf("\nDOUBLY LINKED LIST CREATED SUCCESSFULLY\n");

/**

* Display content of the list from beginning to end

*/

void displayList()

struct node * temp;

int n = 1;

if(head == NULL)

printf("List is empty.\n");

else

temp = head;

printf("Linked LIST Is:\n");


while(temp != NULL)

printf("%d\t", temp->data);

n++;

/* Move the current pointer to next node */

temp = temp->next;

/** Pseudo code or Function to delete a node after a given node in doubly Linked List is
given Below */

void DeleteAfter( int value)

struct node * newNode ;

if(head == NULL)

printf("Error, List is Empty!\n");

else

struct node *temp1,*temp2;


temp1=head;

temp2=head;

while( temp2->data !=value)

temp2=temp1;

temp1=temp1->next ;

temp2->next=temp1->next;

(temp1->next) -> prev= temp2;

free(temp1);

printf("Requested Node After a Given node Deleted SUCCESSFULLY from the


LIST\n");

Note – In the above program when while loop conditions become false at that time temp1
represents the node to be deleted and temp2 represents that node after which we have to
delete the node . So we have to change next field of temp2 and previous field of node which
is after temp1 or which is next to temp1.

Output

enter the number of nodes in doubly linkedlist 5

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

Enter data of 4 node: 24

Enter data of 5 node: 25

DOUBLY LINKED LIST CREATED SUCCESSFULLY

Linked LIST Is:


21 22 23 24 25 enter the data field value of that node after which you want to
delete node

23

Requested Node After a Given node Deleted SUCCESSFULLY from the LIST

Linked LIST Is:

21 22 23 25

You might also like