Dsa 5
Dsa 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>
int data;
} Node;
#include "doublylist.h"
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
*head_ref = newNode;
return;
last = last->next;
last->next = newNode;
newNode->prev = last;
if (position == 0) {
newNode->next = *head_ref;
if (*head_ref != NULL) {
(*head_ref)->prev = newNode;
*head_ref = newNode;
return;
temp = temp->next;
if (temp == NULL) {
free(newNode);
return;
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
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);
if (*head_ref == NULL) {
printf("List is empty.\n");
return;
}
Node* temp = *head_ref;
temp = temp->next;
if (temp == NULL) {
return;
deleteNode(head_ref, temp->data);
if (current->data == key) {
return current;
current = current->next;
return NULL;
if (node == NULL) {
printf("List is empty.\n");
return;
node = node->next;
printf("\n");
}
void freeList(Node** head_ref) {
Node* next;
next = current->next;
free(current);
current = next;
*head_ref = NULL;
int main()
while (1) {
printf("1. Append\n");
printf("5. Search\n");
printf("6. Display\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
append(&head, data);
break;
case 2:
scanf("%d", &data);
scanf("%d", &position);
break;
case 3:
scanf("%d", &data);
deleteNode(&head, data);
break;
case 4:
scanf("%d", &position);
deleteAtPosition(&head, position);
break;
case 5:
scanf("%d", &data);
if (foundNode) {
} else {
break;
case 6:
display(head);
break;
case 7:
return 0;
default:
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>
int data;
} Node;
#include "doublylist.h"
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
if (*head_ref == NULL) {
*head_ref = newNode;
return;
last = last->next;
last->next = newNode;
newNode->prev = last;
if (position == 0) {
newNode->next = *head_ref;
if (*head_ref != NULL) {
(*head_ref)->prev = newNode;
*head_ref = newNode;
return;
temp = temp->next;
append(head_ref, new_data);
return;
newNode->next = temp->next;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
temp = temp->next;
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);
if (*head_ref == NULL)
return;
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);
if (current->data == key)
return current;
current = current->next;
return NULL;
temp = temp->next;
printf("\n");
int main() {
while (1) {
printf("\nMenu:\n");
printf("1. Append\n");
printf("2. Insert\n");
printf("5. Search\n");
printf("6. Display\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to append: ");
scanf("%d", &value);
append(&head, value);
break;
case 2:
scanf("%d", &position);
scanf("%d", &value);
break;
case 3:
scanf("%d", &value);
deleteSpecificNode(&head, value);
break;
case 4:
scanf("%d", &position);
deleteAtPosition(&head, position);
break;
case 5:
scanf("%d", &value);
if (result)
else
break;
case 6:
printf("List: ");
display(head);
break;
case 7:
exit(0);
default:
return 0;
}
Set c
b) What is difference between singly circular linked list and doubly circular linked list?