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

Dsa 5

Uploaded by

deshmukhrushang
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 views14 pages

Dsa 5

Uploaded by

deshmukhrushang
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/ 14

Practical 5

Set a

a)Implement a list library (doublylist.h) for a doubly linked list with the above four operations. Write
a menu driven driver program to call the operationsappend, insert, delete specific node, delete at
position, search, display.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* prev;

struct Node* next;

} Node;

Node* createNode(int data);

void append(Node** head_ref, int new_data);

void insert(Node** head_ref, int new_data, int position);

void deleteNode(Node** head_ref, int key);

void deleteAtPosition(Node** head_ref, int position);

Node* search(Node* head, int key);

void display(Node* node);

void freeList(Node** head_ref);

#include "doublylist.h"

Node* createNode(int data) {

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

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

void append(Node** head_ref, int new_data) {

Node* newNode = createNode(new_data);

Node* last = *head_ref;


if (*head_ref == NULL)

*head_ref = newNode;

return;

while (last->next != NULL) {

last = last->next;

last->next = newNode;

newNode->prev = last;

void insert(Node** head_ref, int new_data, int position) {

Node* newNode = createNode(new_data);

if (position == 0) {

newNode->next = *head_ref;

if (*head_ref != NULL) {

(*head_ref)->prev = newNode;

*head_ref = newNode;

return;

Node* temp = *head_ref;

for (int i = 0; temp != NULL && i < position - 1; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Position is out of bounds.\n");

free(newNode);

return;

newNode->next = temp->next;
if (temp->next != NULL) {

temp->next->prev = newNode;

temp->next = newNode;

newNode->prev = temp;

void deleteNode(Node** head_ref, int key) {

Node* temp = *head_ref;

while (temp != NULL && temp->data != key) {

temp = temp->next;

if (temp == NULL) {

printf("Node with value %d not found.\n", key);

return;

if (*head_ref == temp) {

*head_ref = temp->next;

if (temp->next != NULL) {

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

if (temp->prev != NULL) {

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

free(temp);

void deleteAtPosition(Node** head_ref, int position) {

if (*head_ref == NULL) {

printf("List is empty.\n");

return;

}
Node* temp = *head_ref;

for (int i = 0; temp != NULL && i < position; i++) {

temp = temp->next;

if (temp == NULL) {

printf("Position is out of bounds.\n");

return;

deleteNode(head_ref, temp->data);

Node* search(Node* head, int key) {

Node* current = head;

while (current != NULL) {

if (current->data == key) {

return current;

current = current->next;

return NULL;

void display(Node* node) {

if (node == NULL) {

printf("List is empty.\n");

return;

while (node != NULL) {

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

node = node->next;

printf("\n");

}
void freeList(Node** head_ref) {

Node* current = *head_ref;

Node* next;

while (current != NULL) {

next = current->next;

free(current);

current = next;

*head_ref = NULL;

int main()

Node* head = NULL;

int choice, data, position;

while (1) {

printf("\nDoubly Linked List Operations:\n");

printf("1. Append\n");

printf("2. Insert at Position\n");

printf("3. Delete Specific Node\n");

printf("4. Delete at Position\n");

printf("5. Search\n");

printf("6. Display\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter data to append: ");

scanf("%d", &data);

append(&head, data);

break;
case 2:

printf("Enter data to insert: ");

scanf("%d", &data);

printf("Enter position to insert at: ");

scanf("%d", &position);

insert(&head, data, position);

break;

case 3:

printf("Enter data to delete: ");

scanf("%d", &data);

deleteNode(&head, data);

break;

case 4:

printf("Enter position to delete at: ");

scanf("%d", &position);

deleteAtPosition(&head, position);

break;

case 5:

printf("Enter data to search: ");

scanf("%d", &data);

Node* foundNode = search(head, data);

if (foundNode) {

printf("Node with value %d found.\n", foundNode->data);

} else {

printf("Node with value %d not found.\n", data);

break;

case 6:

printf("Doubly Linked List: ");

display(head);

break;
case 7:

freeList(&head); // Free memory before exiting

return 0;

default:

printf("Invalid choice! Please try again.\n");

Return 0;

}
Set b

a) There are lists where insertion should ensure the ordering of data elements. Since the elements
are in ascending order the search can terminate once equal or greater element is found. Implement
a doubly linked list of ordered integers(ascending/descending) with insert, search and display
operations

#include<stdio.h>

#include<stdlib.h>

typedef struct Node {

int data;

struct Node* prev;

struct Node* next;

} Node;

Node* createNode(int data);

void append(Node** head_ref, int new_data);

void insert(Node** head_ref, int position, int new_data);

void deleteSpecificNode(Node** head_ref, int key);

void deleteAtPosition(Node** head_ref, int position);

Node* search(Node* head, int key);

void display(Node* head);

#include "doublylist.h"

Node* createNode(int data) {

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

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

void append(Node** head_ref, int new_data) {

Node* newNode = createNode(new_data);

Node* last = *head_ref;

if (*head_ref == NULL) {

*head_ref = newNode;
return;

while (last->next != NULL)

last = last->next;

last->next = newNode;

newNode->prev = last;

void insert(Node** head_ref, int position, int new_data) {

Node* newNode = createNode(new_data);

Node* temp = *head_ref;

if (position == 0) {

newNode->next = *head_ref;

if (*head_ref != NULL) {

(*head_ref)->prev = newNode;

*head_ref = newNode;

return;

for (int i = 0; temp != NULL && i < position - 1; i++)

temp = temp->next;

if (temp == NULL || temp->next == NULL) {

append(head_ref, new_data);

return;

newNode->next = temp->next;

if (temp->next != NULL)

temp->next->prev = newNode;

temp->next = newNode;

newNode->prev = temp;

void deleteSpecificNode(Node** head_ref, int key) {


Node* temp = *head_ref;

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

temp = temp->next;

if (temp == NULL) return;

if (*head_ref == temp)

*head_ref = temp->next;

if (temp->next != NULL)

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

if (temp->prev != NULL)

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

free(temp);

void deleteAtPosition(Node** head_ref, int position) {

Node* temp = *head_ref;

if (*head_ref == NULL)

return;

for (int i = 0; temp != NULL && i < position; i++)

temp = temp->next;

if (temp == NULL)

return;

if (*head_ref == temp)

*head_ref = temp->next;

if (temp->next != NULL)

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

if (temp->prev != NULL)

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

free(temp);

Node* search(Node* head, int key) {

Node* current = head;


while (current != NULL) {

if (current->data == key)

return current;

current = current->next;

return NULL;

void display(Node* head) {

Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

printf("\n");

int main() {

Node* head = NULL;

int choice, value, position;

while (1) {

printf("\nMenu:\n");

printf("1. Append\n");

printf("2. Insert\n");

printf("3. Delete Specific Node\n");

printf("4. Delete at Position\n");

printf("5. Search\n");

printf("6. Display\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:
printf("Enter value to append: ");

scanf("%d", &value);

append(&head, value);

break;

case 2:

printf("Enter position to insert at: ");

scanf("%d", &position);

printf("Enter value to insert: ");

scanf("%d", &value);

insert(&head, position, value);

break;

case 3:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteSpecificNode(&head, value);

break;

case 4:

printf("Enter position to delete: ");

scanf("%d", &position);

deleteAtPosition(&head, position);

break;

case 5:

printf("Enter value to search: ");

scanf("%d", &value);

Node* result = search(head, value);

if (result)

printf("Node found with value %d\n", result->data);

else

printf("Node not found.\n");

break;

case 6:
printf("List: ");

display(head);

break;

case 7:

exit(0);

default:

printf("Invalid choice. Try again.\n");

return 0;

}
Set c

b) What is difference between singly circular linked list and doubly circular linked list?

You might also like