[go: up one dir, main page]

0% found this document useful (0 votes)
40 views9 pages

Program

Uploaded by

Seralathan R
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)
40 views9 pages

Program

Uploaded by

Seralathan R
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/ 9

1.

A hospital wants to maintain a real-time list of patients currently waiting in the emergency room
(ER). Patients arrive continuously, and each patient is assigned a severity level (an integer where a
higher number means more critical). The hospital wants to keep the list sorted in descending order
of severity so that the most critical patients are always at the front of the list.
Task:
• Implement a singly linked list to store patients in the ER.
• Each node contains the patient’s ID and severity level.

Problem recap:

• Each node stores:


o Patient ID (int)
o Severity level (int)
• Insert patients continuously, but the list remains sorted in descending order by severity.
• Use a singly linked list.

Code:
#include <stdio.h>
#include <stdlib.h>
// Define patient node structure
struct Patient {
int patient_id;
int severity;
struct Patient* next;
};
// Create a new patient node
struct Patient* create_patient(int id, int severity) {
struct Patient* new_patient = (struct Patient*)malloc(sizeof(struct Patient));
new_patient->patient_id = id;
new_patient->severity = severity;
new_patient->next = NULL;
return new_patient;
}
// Insert patient in descending order of severity
void insert_patient(struct Patient** head, int id, int severity) {
struct Patient* new_patient = create_patient(id, severity);
if (*head == NULL || severity > (*head)->severity) {
new_patient->next = *head;
*head = new_patient;
return;
}
struct Patient* current = *head;
while (current->next != NULL && current->next->severity >= severity) {
current = current->next;
}
new_patient->next = current->next;
current->next = new_patient;
}
// Display all patients
void display_patients(struct Patient* head) {
if (head == NULL) {
printf("No patients in the list.\n");
return;
}
printf("\nPatients in ER (most critical first):\n");
struct Patient* current = head;
while (current != NULL) {
printf("Patient ID: %d, Severity: %d\n", current->patient_id, current->severity);
current = current->next;
}
}
// Free the list memory
void free_list(struct Patient* head) {
struct Patient* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
// Main function
int main() {
struct Patient* er_list = NULL;
int choice;
do {
int id, severity;
printf("\nEnter Patient ID: ");
scanf("%d", &id);
printf("Enter Severity (higher means more critical): ");
scanf("%d", &severity);
insert_patient(&er_list, id, severity);
printf("Do you want to add another patient? (1=Yes, 0=No): ");
scanf("%d", &choice);
} while (choice != 0);
display_patients(er_list);
free_list(er_list);
return 0;
}
2. In an online shopping application, customer order values (in rupees) are stored in a doubly linked
list to allow forward and backward navigation. The initial list of order values is: 100, 300, 500,
700, 900. Perform the following operations: a. Insert a new order value 600 in the middle of the
list (between 500 and 700). b. Remove the order value 300 as the transaction was cancelled. c.
Display the remaining order values from beginning to end.
Develop a C routine using doubly linked list in C to perform the above operations and display the
final list.
C program demonstrating a doubly linked list for the described operations:

• Create initial list: 100, 300, 500, 700, 900


• Insert 600 between 500 and 700
• Remove 300
• Display the list from beginning to end

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *prev;
struct Node *next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node *newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to append node at the end
void append(Node **head_ref, int data) {
Node *newNode = createNode(data);
if (*head_ref == NULL) {
*head_ref = newNode;
return;
}
Node *temp = *head_ref;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
// Insert new node with data '600' between 500 and 700
void insertBetween(Node **head_ref, int data, int afterValue) {
Node *temp = *head_ref;
while (temp != NULL && temp->data != afterValue)
temp = temp->next;
if (temp == NULL) {
printf("Node with value %d not found!\n", afterValue);
return;
}
Node *newNode = createNode(data);
newNode->next = temp->next;
if (temp->next != NULL)
temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
}
// Remove node with value 300
void removeNode(Node **head_ref, int data) {
Node *temp = *head_ref;
// Find the node to delete
while (temp != NULL && temp->data != data)
temp = temp->next;
if (temp == NULL) {
printf("Node with value %d not found!\n", data);
return;
}
// If node to be deleted is head
if (temp == *head_ref)
*head_ref = temp->next;
if (temp->next != NULL)
temp->next->prev = temp->prev;
if (temp->prev != NULL)
temp->prev->next = temp->next;
free(temp);
}
// Display the list from head to end
void displayList(Node *head) {
Node *temp = head;
printf("Order values from beginning to end: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
Node *head = NULL;
// Initial list: 100, 300, 500, 700, 900
append(&head, 100);
append(&head, 300);
append(&head, 500);
append(&head, 700);
append(&head, 900);
// a. Insert 600 between 500 and 700
insertBetween(&head, 600, 500);
// b. Remove 300
removeNode(&head, 300);
// c. Display final list
displayList(head);
// Free memory
while (head != NULL) {
Node *next = head->next;
free(head);
head = next;
}
return 0;
}

3. In a smart irrigation system, a set of water valves are opened in a circular sequence to
ensure even watering of fields. These valves are represented using a circular linked list to
allow continuous cycling from the last valve back to the first. Initially, the active valve
IDs are: 301, 303, 305, 307, 309, 310.

To update the irrigation schedule:


a) Insert a new valve with ID 306 after valve 305
b) Delete the valve with ID 303 (damaged and removed)
c) Display the remaining valve IDs in circular order, starting from the head

Based on the above operation implement ·

• Pictorial Representation (before and after operations) ·


• C syntax/code snippets for each operation
Final state display

We have valve IDs in a circular linked list: 301 -> 303 -> 305 -> 307 -> 309
-> 310 -> back to 301.

Operations:

1. Insert valve with ID 306 after valve 305.


2. Delete valve with ID 303.
3. Display the list starting from the head.

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int valveID;
struct Node *next;
struct Node *prev;
} Node;
// Create a new node
Node* createNode(int id) {
Node *newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->valveID = id;
newNode->next = newNode->prev = NULL;
return newNode;
}
// Insert node at end to form initial circular list
void insertEnd(Node **head_ref, int id) {
Node *newNode = createNode(id);
if (*head_ref == NULL) {
newNode->next = newNode->prev = newNode;
*head_ref = newNode;
return;
}
Node *tail = (*head_ref)->prev;
tail->next = newNode;
newNode->prev = tail;
newNode->next = *head_ref;
(*head_ref)->prev = newNode;
}
// Insert after a node with given valveID
void insertAfter(Node **head_ref, int afterID, int newID) {
if (*head_ref == NULL) return;

Node *temp = *head_ref;


do {
if (temp->valveID == afterID) {
Node *newNode = createNode(newID);
newNode->next = temp->next;
newNode->prev = temp;
temp->next->prev = newNode;
temp->next = newNode;
printf("Inserted valve %d after valve %d\n", newID, afterID);
return;
}
temp = temp->next;
} while (temp != *head_ref);

printf("Valve %d not found. Insert failed.\n", afterID);


}
// Delete node by valveID
void deleteNode(Node **head_ref, int delID) {
if (*head_ref == NULL) return;
Node *temp = *head_ref;
Node *toDelete = NULL;
do {
if (temp->valveID == delID) {
toDelete = temp;
break;
}
temp = temp->next;
} while (temp != *head_ref);
if (toDelete == NULL) {
printf("Valve %d not found. Deletion failed.\n", delID);
return;
}
// If only one node
if (toDelete->next == toDelete) {
*head_ref = NULL;
free(toDelete);
printf("Deleted valve %d. List is now empty.\n", delID);
return;
}
// If head node is to be deleted
if (toDelete == *head_ref)
*head_ref = toDelete->next;
toDelete->prev->next = toDelete->next;
toDelete->next->prev = toDelete->prev;
free(toDelete);
printf("Deleted valve %d\n", delID);
}
// Display the circular list starting from head
void displayList(Node *head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
Node *temp = head;
printf("Valve IDs in circular order: ");
do {
printf("%d ", temp->valveID);
temp = temp->next;
} while (temp != head);
printf("\n");
}
int main() {
Node *head = NULL;
// Initial valves
int valves[] = {301, 303, 305, 307, 309, 310};
int n = sizeof(valves)/sizeof(valves[0]);
for (int i = 0; i < n; i++) {
insertEnd(&head, valves[i]);
}
printf("Initial valve list:\n");
displayList(head);
// a) Insert 306 after 305
insertAfter(&head, 305, 306);
// b) Delete 303
deleteNode(&head, 303);

// c) Display final list


printf("Final valve list after operations:\n");
displayList(head);
// Free list memory
if (head != NULL) {
Node *current = head->next;
while (current != head) {
Node *next = current->next;
free(current);
current = next;
}
free(head);
}
return 0;
}

You might also like