0% found this document useful (0 votes)
8 views11 pages

Ds Lab Manual

The document provides a C program that implements a doubly linked list with functions for creation, insertion, deletion, and traversal. It includes options for inserting and deleting nodes at the beginning, end, or a specific position, as well as displaying the list and searching for elements. The program features a menu-driven interface for user interaction.

Uploaded by

kausarabdul58
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)
8 views11 pages

Ds Lab Manual

The document provides a C program that implements a doubly linked list with functions for creation, insertion, deletion, and traversal. It includes options for inserting and deleting nodes at the beginning, end, or a specific position, as well as displaying the list and searching for elements. The program features a menu-driven interface for user interaction.

Uploaded by

kausarabdul58
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

2.

Write a program that uses functions to perform the following operations on


doubly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct node {

int data;

struct node *prev;

struct node *next;

};

struct node *head = NULL;

// Insert at beginning

void insert_begin() {

struct node *newnode;

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

printf("Enter data: ");

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

newnode->prev = NULL;

newnode->next = head;

if (head != NULL)

head->prev = newnode;
head = newnode;

// Insert at end

void insert_end() {

struct node *newnode, *temp;

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

printf("Enter data: ");

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

newnode->next = NULL;

if (head == NULL) {

newnode->prev = NULL;

head = newnode;

} else {

temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newnode;

newnode->prev = temp;

}
// Insert at position

void insert_pos() {

int pos, i = 1;

struct node *newnode, *temp;

printf("Enter position: ");

scanf("%d", &pos);

if (pos == 1) {

insert_begin();

return;

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

printf("Enter data: ");

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

temp = head;

while (i < pos - 1 && temp != NULL) {

temp = temp->next;

i++;

if (temp == NULL) {

printf("Invalid position\n");

return;

}
newnode->next = temp->next;

newnode->prev = temp;

if (temp->next != NULL)

temp->next->prev = newnode;

temp->next = newnode;

// Delete from beginning

void delete_begin() {

struct node *temp;

if (head == NULL) {

printf("List is empty\n");

return;

temp = head;

head = head->next;

if (head != NULL)

head->prev = NULL;

free(temp);

printf("Deleted from beginning\n");


}

// Delete from end

void delete_end() {

struct node *temp;

if (head == NULL) {

printf("List is empty\n");

return;

temp = head;

while (temp->next != NULL)

temp = temp->next;

if (temp->prev != NULL)

temp->prev->next = NULL;

else

head = NULL;

free(temp);

printf("Deleted from end\n");

// Delete from position

void delete_pos() {

int pos, i = 1;
struct node *temp;

if (head == NULL) {

printf("List is empty\n");

return;

printf("Enter position: ");

scanf("%d", &pos);

if (pos == 1) {

delete_begin();

return;

temp = head;

while (i < pos && temp != NULL) {

temp = temp->next;

i++;

if (temp == NULL) {

printf("Invalid position\n");

return;

temp->prev->next = temp->next;
if (temp->next != NULL)

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

free(temp);

printf("Deleted from position %d\n", pos);

// Display list

void display() {

struct node *temp = head;

if (head == NULL) {

printf("List is empty\n");

return;

printf("Doubly Linked List: ");

while (temp != NULL) {

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

temp = temp->next;

printf("\n");

// Search operation

void search() {
struct node *temp;

int key, pos = 1, found = 0;

if (head == NULL) {

printf("List is empty\n");

return;

printf("Enter element to search: ");

scanf("%d", &key);

temp = head;

while (temp != NULL) {

if (temp->data == key) {

printf("Element %d found at position %d\n", key, pos);

found = 1;

break;

temp = temp->next;

pos++;

if (!found)

printf("Element not found\n");

// Main function
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. Insert at Position\n");

printf("4. Delete from Beginning\n");

printf("5. Delete from End\n");

printf("6. Delete from Position\n");

printf("7. Display\n");

printf("8. Search\n");

printf("9. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: insert_begin(); break;

case 2: insert_end(); break;

case 3: insert_pos(); break;

case 4: delete_begin(); break;

case 5: delete_end(); break;

case 6: delete_pos(); break;

case 7: display(); break;

case 8: search(); break;


case 9: exit(0);

default: printf("Invalid choice\n");

Output:

--- DOUBLY LINKED LIST MENU ---

1. Insert at Beginning

2. Insert at End

3. Insert at Position

4. Delete from Beginning

5. Delete from End

6. Delete from Position

7. Display

8. Search

9. Exit

Enter your choice: 1

Enter data: 10

--- DOUBLY LINKED LIST MENU ---

Enter your choice: 2

Enter data: 20

--- DOUBLY LINKED LIST MENU ---

Enter your choice: 2

Enter data: 30
--- DOUBLY LINKED LIST MENU ---

Enter your choice: 7

Doubly Linked List: 10 20 30

--- DOUBLY LINKED LIST MENU ---

Enter your choice: 8

Enter element to search: 20

Element 20 found at position 2

--- DOUBLY LINKED LIST MENU ---

Enter your choice: 6

Enter position: 2

Deleted from position 2

--- DOUBLY LINKED LIST MENU ---

Enter your choice: 7

Doubly Linked List: 10 30

--- DOUBLY LINKED LIST MENU ---

Enter your choice: 9

You might also like