0% found this document useful (0 votes)
5 views5 pages

Expt 2

The document provides a C program that implements a doubly linked list of integers, allowing for the insertion, deletion, and display of elements. It includes functions to create the list, delete a specified integer, and display the contents of the list after deletion. The program demonstrates its functionality by inserting several integers, deleting one, and displaying the updated list.

Uploaded by

darkside9547
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)
5 views5 pages

Expt 2

The document provides a C program that implements a doubly linked list of integers, allowing for the insertion, deletion, and display of elements. It includes functions to create the list, delete a specified integer, and display the contents of the list after deletion. The program demonstrates its functionality by inserting several integers, deleting one, and displaying the updated list.

Uploaded by

darkside9547
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/ 5

2.

Write a C program that uses functions to perform the following:

a. Create a doubly linked list of integers.

b. Delete a given integer from the above doubly linked list.

c. Display the contents of the above list after deletion.

Program:

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
struct Node *prev;
};

void insert(struct Node** head_ref, int data)


{

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

newNode->data = data;
newNode->next = *head_ref;
newNode->prev = NULL;

//If the linked list already had atleast 1 node

if(*head_ref !=NULL)

(*head_ref)->prev = newNode;

//changing the new head to this freshly entered node

*head_ref = newNode;
}

void delete(struct Node** head_ref, int delVal)


{
//start from the first link

struct Node* current = *head_ref;


struct Node* firstnode = *head_ref;
struct Node* temp=NULL;

//if list is empty

if(current == NULL)
{
printf("List is Empty");
return ;
}

//navigate through list

while(current-> data != delVal)


{
//if it is last node

if(current->next == NULL)
{
printf(" value %d , not found \n",delVal);

return ;
}

else

{
//store reference to current link
temp = current;
//move to next link
current = current->next;
}
}

//found a match, update the link


if(current == firstnode && current->next==NULL )
{
*head_ref=NULL;

printf("value % d ,deleted \n" , delVal);

free(current);

return ;
}

if(current == firstnode )

{
//change first to point to next link

*head_ref = firstnode->next;
firstnode->next->prev =NULL;
}
else

{
//bypass the current link

temp->next = current->next;

printf("value %d ,deleted \n" , delVal);


free(current);

//function to print the doubly linked list


void display(struct Node* node)

{
printf("List :");
while (node != NULL)

{
printf(" %d ", node->data);
node = node->next;

}
printf("\n");

int main()
{

struct Node* head = NULL;


insert(&head,12);
insert(&head,16);
insert(&head,20);
insert(&head,24);
insert(&head,30);
insert(&head,22);

display(head);

delete(&head,12);

display(head);

return 0;
}
Output:

List : 22 30 24 20 16 12

value 12 ,deleted

List : 22 30 24 20 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy