0% found this document useful (0 votes)
15 views15 pages

Dsa 4

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)
15 views15 pages

Dsa 4

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/ 15

Pratcical 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>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct {

Node* head;

} SinglyLinkedList;

void initList(SinglyLinkedList* list);

void append(SinglyLinkedList* list, int data);

void insert(SinglyLinkedList* list, int data, int position);

void deleteNodeByValue(SinglyLinkedList* list, int data);

void deleteNodeAtPosition(SinglyLinkedList* list, int position);

Node* search(SinglyLinkedList* list, int data);

void displayList(SinglyLinkedList* list);

#include "singlylist.h"

void initList(SinglyLinkedList* list) {

list->head = NULL;

void append(SinglyLinkedList* list, int data) {

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

newNode->data = data;

newNode->next = NULL;

if (list->head == NULL) {

list->head = newNode;

} else {
Node* temp = list->head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

printf("Appended: %d\n", data);

void insert(SinglyLinkedList* list, int data, int position) {

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

newNode->data = data;

if (position == 0) {

newNode->next = list->head;

list->head = newNode;

} else {

Node* temp = list->head;

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

temp = temp->next;

if (temp == NULL) {

printf("Position out of bounds!\n");

free(newNode);

return;

newNode->next = temp->next;

temp->next = newNode;

printf("Inserted: %d at position %d\n", data, position);

void deleteNodeByValue(SinglyLinkedList* list, int data) {

Node* temp = list->head;


Node* prev = NULL;

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

prev = temp;

temp = temp->next;

if (temp == NULL) {

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

return;

if (prev == NULL) {

list->head = temp->next;

} else {

prev->next = temp->next;

free(temp);

printf("Deleted node with data: %d\n", data);

void deleteNodeAtPosition(SinglyLinkedList* list, int position) {

if (list->head == NULL) {

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

return;

Node* temp = list->head;

if (position == 0) {

list->head = temp->next;

free(temp);

printf("Deleted node at position %d\n", position);

return;

Node* prev = NULL;

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


prev = temp;

temp = temp->next;

if (temp == NULL) {

printf("Position out of bounds!\n");

return;

prev->next = temp->next;

free(temp);

printf("Deleted node at position %d\n", position);

Node* search(SinglyLinkedList* list, int data) {

Node* temp = list->head;

while (temp != NULL) {

if (temp->data == data) {

return temp;

temp = temp->next;

return NULL; // Not found

void displayList(SinglyLinkedList* list) {

Node* temp = list->head;

if (temp == NULL) {

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

return;

printf("List contents: ");

while (temp != NULL) {

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

temp = temp->next;
}

printf("\n");

int main() {

SinglyLinkedList list;

initList(&list);

int choice, data, position;

do {

printf("\n--- Singly Linked List Operations Menu ---\n");

printf("1. Append an Element\n");

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

printf("3. Delete a Node by Value\n");

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

printf("5. Search for an Element\n");

printf("6. Display the List\n");

printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the data to append: ");

scanf("%d", &data);

append(&list, data);

break;

case 2:

printf("Enter the data to insert: ");

scanf("%d", &data);

printf("Enter the position: ");

scanf("%d", &position);

insert(&list, data, position);

break;
case 3:

printf("Enter the data to delete: ");

scanf("%d", &data);

deleteNodeByValue(&list, data);

break;

case 4:

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

scanf("%d", &position);

deleteNodeAtPosition(&list, position);

break;

case 5:

printf("Enter the data to search: ");

scanf("%d", &data);

Node* foundNode = search(&list, data);

if (foundNode) {

printf("Found node with data: %d\n", foundNode->data);

} else {

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

break;

case 6:

displayList(&list);

break;

case 7:

printf("Exiting...\n");

break;

default:

printf("Invalid choice! Please choose a valid option.\n");

} while (choice != 7);

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>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct {

Node* head;

} OrderedList;

void initList(OrderedList* list);

void insert(OrderedList* list, int data);

Node* search(OrderedList* list, int data);

void displayList(OrderedList* list);

#include "orderedlist.h"

void initList(OrderedList* list) {

list->head = NULL;

void insert(OrderedList* list, int data) {

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

newNode->data = data;

newNode->next = NULL;

if (list->head == NULL || list->head->data > data) {

newNode->next = list->head;

list->head = newNode;

printf("Inserted: %d\n", data);

return;

}
Node* current = list->head;

while (current->next != NULL && current->next->data < data) {

current = current->next;

newNode->next = current->next;

current->next = newNode;

printf("Inserted: %d\n", data);

Node* search(OrderedList* list, int data) {

Node* current = list->head;

while (current != NULL) {

if (current->data == data) {

return current;

if (current->data > data) {

break;

current = current->next;

return NULL;

void displayList(OrderedList* list) {

Node* current = list->head;

if (current == NULL) {

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

return;

printf("List contents: ");

while (current != NULL) {

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

current = current->next;
}

printf("\n");

void initList(OrderedList* list) {

list->head = NULL;

void insert(OrderedList* list, int data) {

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

newNode->data = data;

newNode->next = NULL;

if (list->head == NULL || list->head->data > data) {

newNode->next = list->head;

list->head = newNode;

printf("Inserted: %d\n", data);

return;

Node* current = list->head;

while (current->next != NULL && current->next->data < data) {

current = current->next;

newNode->next = current->next;

current->next = newNode;

printf("Inserted: %d\n", data);

Node* search(OrderedList* list, int data) {

Node* current = list->head;

while (current != NULL) {

if (current->data == data) {

return current;

}
if (current->data > data) {

break;

current = current->next;

return NULL;

void displayList(OrderedList* list) {

Node* current = list->head;

if (current == NULL) {

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

return;

printf("List contents: ");

while (current != NULL) {

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

current = current->next;

printf("\n");

int main() {

OrderedList list;

initList(&list);

int choice, data;

do {

printf("\n--- Ordered Singly Linked List Operations Menu ---\n");

printf("1. Insert an Element\n");

printf("2. Search for an Element\n");

printf("3. Display the List\n");

printf("4. Exit\n");
printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the data to insert: ");

scanf("%d", &data);

insert(&list, data);

break;

case 2:

printf("Enter the data to search: ");

scanf("%d", &data);

Node* foundNode = search(&list, data);

if (foundNode) {

printf("Found node with data: %d\n", foundNode->data);

} else {

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

break;

case 3:

displayList(&list);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice! Please choose a valid option.\n");

} while (choice != 4);

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>

typedef struct Node {

int data;

struct Node* next;

} Node;

void reverseList(Node** head) {

Node* prev = NULL;

Node* current = *head;

Node* next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

*head = prev;

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

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

Node* last = *head;

new_node->data = new_data;

new_node->next = NULL;

if (*head == NULL) {

*head = new_node;

return;

while (last->next != NULL) {

last = last->next;
}

last->next = new_node;

void display(Node* node) {

while (node != NULL) {

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

node = node->next;

printf("NULL\n");

int main() {

Node* head = NULL;

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;

You might also like