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

Doubly Linked List C Program Guide

The document outlines an algorithm and C program for implementing a doubly linked list. It includes a struct definition for nodes, functions for creating, inserting, deleting, searching, and displaying nodes, as well as a main menu for user interaction. The program allows users to perform various operations on the linked list through a simple command-line interface.

Uploaded by

kiran23benny
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 views3 pages

Doubly Linked List C Program Guide

The document outlines an algorithm and C program for implementing a doubly linked list. It includes a struct definition for nodes, functions for creating, inserting, deleting, searching, and displaying nodes, as well as a main menu for user interaction. The program allows users to perform various operations on the linked list through a simple command-line interface.

Uploaded by

kiran23benny
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

Doubly Linked List Algorithm & C Program

Algorithm (with code hints) 1. Start 2. Define a struct Node with data, prev, next // struct
Node { int data; struct Node *prev, *next; }; 3. Initialize head = NULL 4. Define
functions (all input taken inside functions): a) createNode() → read value from user and allocate
node // scanf("%d",&val;); b) insertAtBeginning() → add node in front c) insertAtEnd() →
traverse till last and insert d) deleteNode() → search value, adjust links, free memory e)
searchNode() → traverse and compare values f) displayList() → print all node values 5. In main(),
show menu: 1 → Insert at Beginning 2 → Insert at End 3 → Delete by Value 4 → Search 5 →
Display 6 → Exit 6. Perform operation based on choice 7. Stop

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

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

struct Node *head = NULL;

// Create node with user input


struct Node* createNode() {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
int val;
printf("Enter value: ");
scanf("%d", &val);
newNode->data = val;
newNode->prev = newNode->next = NULL;
return newNode;
}

// Insert at beginning
void insertAtBeginning() {
struct Node *newNode = createNode();
if (head == NULL) head = newNode;
else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
printf("Inserted at beginning.\n");
}

// Insert at end
void insertAtEnd() {
struct Node *newNode = createNode();
if (head == NULL) head = newNode;
else {
struct Node *temp = head;
while (temp->next != NULL) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
printf("Inserted at end.\n");
}

// Delete by value
void deleteNode() {
if (head == NULL) { printf("List empty.\n"); return; }
int val; printf("Enter value to delete: "); scanf("%d", &val);
struct Node *temp = head;
while (temp != NULL && temp->data != val) temp = temp->next;
if (temp == NULL) { printf("Not found.\n"); return; }
if (temp->prev != NULL) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next != NULL) temp->next->prev = temp->prev;
free(temp);
printf("Deleted %d.\n", val);
}

// Search node
void searchNode() {
if (head == NULL) { printf("List empty.\n"); return; }
int val; printf("Enter value to search: "); scanf("%d", &val);
struct Node *temp = head; int pos = 1;
while (temp != NULL) {
if (temp->data == val) { printf("Found %d at position %d.\n", val, pos); return
temp = temp->next; pos++;
}
printf("Not found.\n");
}

// Display list
void displayList() {
if (head == NULL) { printf("List empty.\n"); return; }
struct Node *temp = head;
printf("List: ");
while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; }
printf("\n");
}

// Main menu
int main() {
int choice;
while (1) {
printf("\n--- Doubly Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete by Value\n");
printf("4. Search\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insertAtBeginning(); break;
case 2: insertAtEnd(); break;
case 3: deleteNode(); break;
case 4: searchNode(); break;
case 5: displayList(); break;
case 6: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

You might also like