Dsa 4
Dsa 4
Set a
a) Implement a list library (singlylist.h) for a singly linked list with the above six operations. Write a
menu driven driver program to call the operations.
#include<stdio.h>
#include<stdlib.h>
int data;
} Node;
typedef struct {
Node* head;
} SinglyLinkedList;
#include "singlylist.h"
list->head = NULL;
newNode->data = data;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
} else {
Node* temp = list->head;
temp = temp->next;
temp->next = newNode;
newNode->data = data;
if (position == 0) {
newNode->next = list->head;
list->head = newNode;
} else {
temp = temp->next;
if (temp == NULL) {
free(newNode);
return;
newNode->next = temp->next;
temp->next = newNode;
prev = temp;
temp = temp->next;
if (temp == NULL) {
return;
if (prev == NULL) {
list->head = temp->next;
} else {
prev->next = temp->next;
free(temp);
if (list->head == NULL) {
printf("List is empty!\n");
return;
if (position == 0) {
list->head = temp->next;
free(temp);
return;
temp = temp->next;
if (temp == NULL) {
return;
prev->next = temp->next;
free(temp);
if (temp->data == data) {
return temp;
temp = temp->next;
if (temp == NULL) {
printf("List is empty!\n");
return;
temp = temp->next;
}
printf("\n");
int main() {
SinglyLinkedList list;
initList(&list);
do {
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
append(&list, data);
break;
case 2:
scanf("%d", &data);
scanf("%d", &position);
break;
case 3:
scanf("%d", &data);
deleteNodeByValue(&list, data);
break;
case 4:
scanf("%d", &position);
deleteNodeAtPosition(&list, position);
break;
case 5:
scanf("%d", &data);
if (foundNode) {
} else {
break;
case 6:
displayList(&list);
break;
case 7:
printf("Exiting...\n");
break;
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 singly linked list of ordered integers(ascending/descending) with insert, search and display
operations.
#include<stdio.h>
#include<stdlib.h>
int data;
} Node;
typedef struct {
Node* head;
} OrderedList;
#include "orderedlist.h"
list->head = NULL;
newNode->data = data;
newNode->next = NULL;
newNode->next = list->head;
list->head = newNode;
return;
}
Node* current = list->head;
current = current->next;
newNode->next = current->next;
current->next = newNode;
if (current->data == data) {
return current;
break;
current = current->next;
return NULL;
if (current == NULL) {
printf("List is empty!\n");
return;
current = current->next;
}
printf("\n");
list->head = NULL;
newNode->data = data;
newNode->next = NULL;
newNode->next = list->head;
list->head = newNode;
return;
current = current->next;
newNode->next = current->next;
current->next = newNode;
if (current->data == data) {
return current;
}
if (current->data > data) {
break;
current = current->next;
return NULL;
if (current == NULL) {
printf("List is empty!\n");
return;
current = current->next;
printf("\n");
int main() {
OrderedList list;
initList(&list);
do {
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
insert(&list, data);
break;
case 2:
scanf("%d", &data);
if (foundNode) {
} else {
break;
case 3:
displayList(&list);
break;
case 4:
printf("Exiting...\n");
break;
default:
return 0;
}
Set c
c) What is the method to reverse a singly linked list in just one traversal?
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
next = current->next;
current->next = prev;
prev = current;
current = next;
*head = prev;
new_node->data = new_data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
last = last->next;
}
last->next = new_node;
node = node->next;
printf("NULL\n");
int main() {
append(&head, 1);
append(&head, 2);
append(&head, 3);
append(&head, 4);
append(&head, 5);
printf("Original list:\n");
display(head);
reverseList(&head);
printf("Reversed list:\n");
display(head);
return 0;