0% found this document useful (0 votes)
3 views9 pages

Document

The document contains a C program that implements a doubly linked list with various functionalities such as insertion, deletion, traversal, and reversal. It includes functions to create nodes, insert at the beginning or end, delete nodes by value, and convert the list into a circular doubly linked list. The main function demonstrates these operations using a sample student ID.

Uploaded by

Owalid Ovi
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)
3 views9 pages

Document

The document contains a C program that implements a doubly linked list with various functionalities such as insertion, deletion, traversal, and reversal. It includes functions to create nodes, insert at the beginning or end, delete nodes by value, and convert the list into a circular doubly linked list. The main function demonstrates these operations using a sample student ID.

Uploaded by

Owalid Ovi
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

#include <stdio.

h>

#include <stdlib.h>

struct Node

int data;

struct Node *prev;

struct Node *next;

};

struct Node *head = NULL;

// Create a new node

struct Node* createNode(int value)

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

newNode->data = value;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

// Insert at end

void insertEnd(int value)

struct Node *newNode = createNode(value);


if(head == NULL)

head = newNode;

return;

struct Node *temp = head;

while(temp->next != NULL)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

// Forward Traversal

void forwardTraversal()

struct Node *temp = head;

while(temp != NULL)

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

temp = temp->next;

printf("\n");

// Backward Traversal

void backwardTraversal()
{

if(head == NULL)

return;

struct Node *temp = head;

while(temp->next != NULL)

temp = temp->next;

while(temp != NULL)

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

temp = temp->prev;

printf("\n");

// Insert at beginning

void insertBeginning(int value)

struct Node *newNode = createNode(value);

newNode->next = head;

if(head != NULL)

head->prev = newNode;

head = newNode;

// Insert before 4th node

void insertBeforeFourth(int value)


{

if(head == NULL)

return;

struct Node *temp = head;

int count = 1;

while(temp != NULL && count < 4)

temp = temp->next;

count++;

if(temp == NULL)

return;

struct Node *newNode = createNode(value);

newNode->next = temp;

newNode->prev = temp->prev;

if(temp->prev != NULL)

temp->prev->next = newNode;

temp->prev = newNode;

if(temp == head)

head = newNode;

// Delete from beginning

void deleteBeginning()

if(head == NULL)
return;

struct Node *temp = head;

head = head->next;

if(head != NULL)

head->prev = NULL;

free(temp);

// Delete by value

void deleteValue(int value)

if(head == NULL)

return;

struct Node *temp = head;

while(temp != NULL && temp->data != value)

temp = temp->next;

if(temp == NULL)

printf("Value not found!\n");

return;

if(temp == head)

deleteBeginning();

return;

}
if(temp->next != NULL)

temp->next->prev = temp->prev;

if(temp->prev != NULL)

temp->prev->next = temp->next;

free(temp);

// Reverse DLL

void reverseList()

struct Node *current = head;

struct Node *temp = NULL;

while(current != NULL)

temp = current->prev;

current->prev = current->next;

current->next = temp;

current = current->prev;

if(temp != NULL)

head = temp->prev;

// Convert to Circular DLL using Sentinel Node

void makeCircular()

{
struct Node *sentinel = createNode(-1);

struct Node *tail = head;

while(tail->next != NULL)

tail = tail->next;

sentinel->next = head;

sentinel->prev = tail;

head->prev = sentinel;

tail->next = sentinel;

printf("\nCircular Doubly Linked List created using Sentinel Node (-1).\n");

int main()

// Digits of ID: 253-35-078

int id[] = {2, 5, 3, 3, 5, 0, 7, 8};

int i, value;

printf("Student ID: 253-35-078\n");

printf("Digits: ");

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

printf("%d ", id[i]);

printf("\n\n");

// 1 & 2 Create and Insert at End

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

insertEnd(id[i]);
printf("1 & 2. Initial Doubly Linked List:\n");

forwardTraversal();

// 3 Insert 9 at beginning

insertBeginning(9);

printf("\n3. After inserting 9 at beginning:\n");

forwardTraversal();

// 4 Insert 0 before 4th node

insertBeforeFourth(0);

printf("\n4. After inserting 0 before the 4th node:\n");

forwardTraversal();

// 5 Delete beginning

deleteBeginning();

printf("\n5. After deleting beginning node:\n");

forwardTraversal();

// 6 Delete by value

printf("\n6. Enter value to delete: ");

scanf("%d", &value);

deleteValue(value);

printf("After deleting %d:\n", value);

forwardTraversal();
// 7 Backward traversal

printf("\n7. Backward Traversal:\n");

backwardTraversal();

// 8 Reverse

reverseList();

printf("\n8. Reversed Doubly Linked List:\n");

forwardTraversal();

// 9 Circular DLL

makeCircular();

return 0;

You might also like