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

Name: Aishwary Bharat Bhati Branch: DS - B2 Roll No.: 27 Code

This document contains code to implement a doubly linked list. It defines a Node struct with data and next/prev pointers. Functions are defined to create nodes, append nodes, display the list, and traverse the list in the forward and backward directions from a selected node. The code takes input to build an 8 node doubly linked list, displays it, takes a target value, and traverses the list forward and backward from the node containing that value if found.

Uploaded by

vikbose0315
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)
16 views5 pages

Name: Aishwary Bharat Bhati Branch: DS - B2 Roll No.: 27 Code

This document contains code to implement a doubly linked list. It defines a Node struct with data and next/prev pointers. Functions are defined to create nodes, append nodes, display the list, and traverse the list in the forward and backward directions from a selected node. The code takes input to build an 8 node doubly linked list, displays it, takes a target value, and traverses the list forward and backward from the node containing that value if found.

Uploaded by

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

Name : Aishwary Bharat Bhati

Branch : DS - B2

Roll No. : 27

Code:
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

struct Node* createNode(int data) {

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

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

void displayList(struct Node* head) {

struct Node* temp = head;

1
printf("Doubly linked list: ");

while (temp != NULL) {

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

temp = temp->next;

printf("\n");

void append(struct Node** headRef, int data) {

struct Node* newNode = createNode(data);

struct Node* last = *headRef;

if (*headRef == NULL) {

*headRef = newNode;

return;

while (last->next != NULL) {

last = last->next;

last->next = newNode;

newNode->prev = last;

void forwardPath(struct Node* selectedNode) {

printf("Forward Path from Node %d: ", selectedNode->data);

2
while (selectedNode != NULL) {

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

selectedNode = selectedNode->next;

printf("\n");

void backwardPath(struct Node* selectedNode) {

printf("Backward Path from Node %d: ", selectedNode->data);

while (selectedNode != NULL) {

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

selectedNode = selectedNode->prev;

printf("\n");

int main() {

struct Node* head = NULL;

int i, value;

printf("Enter 8 values to create a doubly linked list:\n");

for (i = 0; i < 8; ++i) {

printf("Enter value %d: ", i + 1);

scanf("%d", &value);

append(&head, value);

3
}

displayList(head);

int selectedValue;

printf("Enter the value of node : ");

scanf("%d", &selectedValue);

struct Node* currentNode = head;

while (currentNode != NULL && currentNode->data != selectedValue) {

currentNode = currentNode->next;

if (currentNode == NULL) {

printf("Node not found in the list.\n");

else {

forwardPath(currentNode);

backwardPath(currentNode);

return 0;

Output 1 :

4
Output 2 :

You might also like