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

Dslab 10-14

The document contains multiple C programs demonstrating data structures and algorithms, including a library catalog system using a doubly linked list, polynomial addition and multiplication using linked lists, and implementations of stack and queue using singly linked lists. Each program features a menu-driven interface for user interaction, allowing operations such as adding, removing, and displaying elements. Additionally, there are functions for reversing linked lists and adding two numbers represented as linked lists.

Uploaded by

mittunavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Dslab 10-14

The document contains multiple C programs demonstrating data structures and algorithms, including a library catalog system using a doubly linked list, polynomial addition and multiplication using linked lists, and implementations of stack and queue using singly linked lists. Each program features a menu-driven interface for user interaction, allowing operations such as adding, removing, and displaying elements. Additionally, there are functions for reversing linked lists and adding two numbers represented as linked lists.

Uploaded by

mittunavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

10)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Book {
char title[100];
char author[100];
char edition[20];
char subject[100];
int shelfNumber;
struct Book *prev;
struct Book *next;
};
struct Book *head = NULL;
struct Book* createBook(char *title, char *author, char *edition, char *subject,
int shelfNumber) {
struct Book *newBook = (struct Book*)malloc(sizeof(struct Book));
strcpy(newBook->title, title);
strcpy(newBook->author, author);
strcpy(newBook->edition, edition);
strcpy(newBook->subject, subject);
newBook->shelfNumber = shelfNumber;
newBook->prev = NULL;
newBook->next = NULL;
return newBook;
}

int addBook(char *title, char *author, char *edition, char *subject, int
shelfNumber) {
struct Book *newBook = createBook(title, author, edition, subject,
shelfNumber);
if (head == NULL) {
head = newBook;
return 1;
}
struct Book *current = head->next;
while (current != NULL && strcmp(current->subject, subject) == 0 &&
strcmp(current->author, author) < 0) {
current = current->next;
}
if (current == NULL) {
struct Book *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newBook;
newBook->prev = temp;
} else {
newBook->prev = current->prev;
newBook->next = current;
current->prev->next = newBook;
current->prev = newBook;
}
}
int removeBook(char *title, char *author) {
struct Book *current = head;
while (current != NULL) {
if (strcmp(current->title, title) == 0 && strcmp(current->author, author)
== 0) {
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
head = current->next;
}

if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
printf("Book '%s' by '%s' removed.\n", title, author);
return 1;
}
current = current->next;
}
printf("Book not found.\n");
}

int displayLocation(char *title, char *author) {


struct Book *current = head;

while (current != NULL) {


if (strcmp(current->title, title) == 0 && strcmp(current->author, author)
== 0) {
printf("Book Title: %s\nAuthor: %s\nEdition: %s\nSubject: %s\nShelf
Number: %d\n",
current->title, current->author, current->edition, current-
>subject, current->shelfNumber);
return 1;
}
current = current->next;
}
printf("Book not found.\n");
}
void displayCatalog() {
struct Book *current = head;
while (current != NULL) {
printf("Book Title: %s, Author: %s, Edition: %s, Subject: %s, Shelf Number:
%d\n",
current->title, current->author, current->edition, current->subject,
current->shelfNumber);
current = current->next;
}
}
int main() {
int choice;
char title[100], author[100], edition[20], subject[100];
int shelfNumber;
do {
printf("\nLibrary Catalog Menu\n");
printf("1. Add a new book\n");
printf("2. Remove an existing book\n");
printf("3. Display book location\n");
printf("4. Display entire catalog\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // To clear the newline character from the input buffer
switch (choice) {
case 1:
printf("Enter book title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = 0; // Remove trailing newline
printf("Enter author name: ");
fgets(author, sizeof(author), stdin);
author[strcspn(author, "\n")] = 0;
printf("Enter edition: ");
fgets(edition, sizeof(edition), stdin);
edition[strcspn(edition, "\n")] = 0;
printf("Enter subject: ");
fgets(subject, sizeof(subject), stdin);
subject[strcspn(subject, "\n")] = 0;
printf("Enter shelf number: ");
scanf("%d", &shelfNumber);
addBook(title, author, edition, subject, shelfNumber);
break;

case 2:
printf("Enter book title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = 0;
printf("Enter author name: ");
fgets(author, sizeof(author), stdin);
author[strcspn(author, "\n")] = 0;
removeBook(title, author);
break;
case 3:
printf("Enter book title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = 0;
printf("Enter author name: ");
fgets(author, sizeof(author), stdin);
author[strcspn(author, "\n")] = 0;
displayLocation(title, author);
break;
case 4:
displayCatalog();
break;

case 5:
printf("Exiting...\n");
break;

default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 5);
return 0;
}
11)
#include <stdio.h>
#include <stdlib.h>

struct node {
int exp;
int coeff;
struct node *next;
};

struct node* createnode(int coeff, int exp) {


struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->exp = exp;
newnode->coeff = coeff;
newnode->next = NULL;
return newnode;
}

struct node* insertTerm(struct node* head, int coeff, int exp) {


struct node* newnode = createnode(coeff, exp);
if (!head || head->exp < exp) {
newnode->next = head;
return newnode;
}

struct node* temp = head;


while (temp->next && temp->next->exp >= exp) {
temp = temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
return head;
}

void display(struct node* head) {


if (!head) {
printf("0\n");
return;
}

struct node* p = head;


while (p != NULL) {
printf("%dx^%d", p->coeff, p->exp);
if (p->next != NULL) {
printf(" + ");
}
p = p->next;
}
printf("\n");
}

struct node* addpoly(struct node* poly1, struct node* poly2) {


struct node* result = NULL;

while (poly1 != NULL && poly2 != NULL) {


if (poly1->exp == poly2->exp) {
result = insertTerm(result, poly1->coeff + poly2->coeff, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly2->exp > poly1->exp) {
result = insertTerm(result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
} else {
result = insertTerm(result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
}

while (poly1) {
result = insertTerm(result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
while (poly2) {
result = insertTerm(result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
}

return result;
}

struct node* multipoly(struct node* poly1, struct node* poly2) {


struct node* result = NULL;

struct node* temp1 = poly1;


while (temp1) {
struct node* temp2 = poly2;
while (temp2) {
result = insertTerm(result, temp1->coeff * temp2->coeff, temp1->exp +
temp2->exp);
temp2 = temp2->next;
}
temp1 = temp1->next;
}

return result;
}

struct node* inputpoly() {


int n, coeff, exp;
struct node* poly = NULL;

printf("Enter the number of terms in the polynomial: ");


scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter coefficient and exponent of term %d: ", i + 1);
scanf("%d%d", &coeff, &exp);
poly = insertTerm(poly, coeff, exp);
}

return poly;
}

void menu() {
printf("1. Add 2 polynomials(0 to re-enter)\n");
printf("2. Multiply 2 polynomials\n");
printf("3. Exit\n");
printf("Choose an operation: ");
}

int main() {
int ch,x=0;
struct node *poly1 = NULL, *poly2 = NULL, *result = NULL;

do {
if(x==0){
printf("Input the first polynomial:\n");
poly1 = inputpoly();
printf("First polynomial: ");
display(poly1);

printf("Input the second polynomial:\n");


poly2 = inputpoly();
printf("Second polynomial: ");
display(poly2);

printf("\n");
}
x++;
menu();
scanf("%d", &ch);

switch (ch) {
case 0:
x--;
break;
case 1:
result = addpoly(poly1, poly2);
printf("Resultant polynomial after addition: ");
display(result);
break;
case 2:
result = multipoly(poly1, poly2);
printf("Resultant polynomial after multiplication: ");
display(result);
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (ch != 3);

return 0;
}

12)
#include <stdio.h>
#include <stdlib.h>

// Define a node
struct Node {
int data;
struct Node* next;
};

// Stack implementation using singly linked list


struct Stack {
struct Node* top;
};

// Queue implementation using singly linked list


struct Queue {
struct Node* front;
struct Node* rear;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation error\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Stack functions
void push(struct Stack* stack, int data) {
struct Node* newNode = createNode(data);
newNode->next = stack->top;
stack->top = newNode;
printf("Pushed %d onto the stack.\n", data);
}

int pop(struct Stack* stack) {


if (stack->top == NULL) {
printf("Stack is empty. Nothing to pop.\n");
return -1;
}
struct Node* temp = stack->top;
int poppedData = temp->data;
stack->top = stack->top->next;
free(temp);
return poppedData;
}

void displayStack(struct Stack* stack) {


if (stack->top == NULL) {
printf("Stack is empty.\n");
return;
}
struct Node* temp = stack->top;
printf("Stack elements: ");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Queue functions
void enqueue(struct Queue* queue, int data) {
struct Node* newNode = createNode(data);
if (queue->rear == NULL) {
queue->front = queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
printf("Enqueued %d to the queue.\n", data);
}

int dequeue(struct Queue* queue) {


if (queue->front == NULL) {
printf("Queue is empty. Nothing to dequeue.\n");
return -1;
}
struct Node* temp = queue->front;
int dequeuedData = temp->data;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
return dequeuedData;
}

void displayQueue(struct Queue* queue) {


if (queue->front == NULL) {
printf("Queue is empty.\n");
return;
}
struct Node* temp = queue->front;
printf("Queue elements: ");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

// Main menu-driven program


int main() {
struct Stack stack;
stack.top = NULL;

struct Queue queue;


queue.front = queue.rear = NULL;

int choice, data;

while (1) {
printf("\nMenu:\n");
printf("1. Push to Stack\n");
printf("2. Pop from Stack\n");
printf("3. Display Stack\n");
printf("4. Enqueue to Queue\n");
printf("5. Dequeue from Queue\n");
printf("6. Display Queue\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to push onto the stack: ");
scanf("%d", &data);
push(&stack, data);
break;
case 2:
data = pop(&stack);
if (data != -1) {
printf("Popped %d from the stack.\n", data);
}
break;
case 3:
displayStack(&stack);
break;
case 4:
printf("Enter data to enqueue to the queue: ");
scanf("%d", &data);
enqueue(&queue, data);
break;
case 5:
data = dequeue(&queue);
if (data != -1) {
printf("Dequeued %d from the queue.\n", data);
}
break;
case 6:
displayQueue(&queue);
break;
case 7:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}
13)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Node* insertEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
// Function to reverse the linked list
struct Node* reverseList(struct Node* head) {
struct Node *prev = NULL, *current = head, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
struct Node* addLists(struct Node* num1, struct Node* num2) {
struct Node *result = NULL, *temp;
int carry = 0, sum;
num1 = reverseList(num1);
num2 = reverseList(num2);
while (num1 != NULL || num2 != NULL || carry != 0) {
sum = carry;
if (num1 != NULL) {
sum += num1->data;
num1 = num1->next;
}
if (num2 != NULL) {
sum += num2->data;
num2 = num2->next;
}
carry = sum / 10;
sum = sum % 10;
result = insertEnd(result, sum);
}
result = reverseList(result);
return result;
}
void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
while (head != NULL) {
printf("%d", head->data);
head = head->next;
}
printf("\n");
}

// Main program
int main() {
struct Node *num1 = NULL, *num2 = NULL, *result;
int digit, n1, n2, i;

// Input the first huge number digit by digit


printf("Enter the number of digits in the first huge number: ");
scanf("%d", &n1);
printf("Enter the digits of the first huge number (one by one): ");
for (i = 0; i < n1; i++) {
scanf("%d", &digit);
num1 = insertEnd(num1, digit);
}
// Input the second huge number digit by digit
printf("Enter the number of digits in the second huge number: ");
scanf("%d", &n2);
printf("Enter the digits of the second huge number (one by one): ");
for (i = 0; i < n2; i++) {
scanf("%d", &digit);
num2 = insertEnd(num2, digit);
}

// Add the two lists


result = addLists(num1, num2);

// Display the result


printf("The sum is: ");
displayList(result);

return 0;
}
14)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Define a structure for each partition node in the circular linked list
struct Partition {
int size; // Size of the partition
int is_free; // 1 if the partition is free, 0 if it's occupied
int process_id; // Process ID occupying this partition (-1 if empty)
struct Partition* next; // Pointer to the next partition (for circular list)
};
// Function to create a new partition node
struct Partition* createPartition(int size) {
struct Partition* newPartition = (struct Partition*)malloc(sizeof(struct
Partition));
newPartition->size = size;
newPartition->is_free = 1; // Initially, all partitions are free
newPartition->process_id = -1;
newPartition->next = NULL;
return newPartition;
}

void insertPartition(struct Partition** head, int size) {


struct Partition* newPartition = createPartition(size);
if (*head == NULL) {
*head = newPartition;
newPartition->next = *head; // Circular link
} else {
struct Partition* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newPartition;
newPartition->next = *head;
}
}

void displayPartitions(struct Partition* head) {


if (head == NULL) return;
struct Partition* temp = head;
printf("Memory partitions:\n");
do {
printf("Partition Size: %d KB, ", temp->size);
if (temp->is_free)
printf("Free\n");
else
printf("Occupied by Process %d\n", temp->process_id);
temp = temp->next;
} while (temp != head);
printf("\n");
}

struct Partition* firstFit(struct Partition* head, int process_id, int size) {


struct Partition* temp = head;
do {
if (temp->is_free && temp->size >= size) {
return temp; // First fit found
}
temp = temp->next;
} while (temp != head);
return NULL; // No fit found
}

// Function to find the best fit for a memory request


struct Partition* bestFit(struct Partition* head, int process_id, int size) {
struct Partition* temp = head;
struct Partition* best = NULL;
do {
if (temp->is_free && temp->size >= size) {
if (best == NULL || temp->size < best->size) {
best = temp; // Best fit is the smallest fitting partition
}
}
temp = temp->next;
} while (temp != head);
return best; // Best fit found, or NULL if no fit found
}

// Function to find the next fit for a memory request (from last allocated)
struct Partition* nextFit(struct Partition* head, struct Partition**
last_allocated, int process_id, int size) {
struct Partition* temp = (*last_allocated == NULL) ? head : (*last_allocated)-
>next;
struct Partition* start = temp; // To prevent infinite loop
do {
if (temp->is_free && temp->size >= size) {
*last_allocated = temp;
return temp;
}
temp = temp->next;
} while (temp != start);
return NULL;
}
void allocateMemory(struct Partition* partition, int process_id, int size) {
partition->is_free = 0;
partition->process_id = process_id;
printf("Allocated %d KB to Process %d\n", size, process_id);
}
void deallocateMemory(struct Partition* head, int process_id) {
struct Partition* temp = head;
bool found = false;
do {
if (temp->process_id == process_id) {
temp->is_free = 1;
temp->process_id = -1;
printf("Process %d completed and memory deallocated\n", process_id);
found = true;
}
temp = temp->next;
} while (temp != head);
if (!found) {
printf("Process %d not found in memory\n", process_id);
}
}
void simulateMemoryManagement(const char* filename, int strategy) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error: Cannot open file %s\n", filename);
return;
}

struct Partition* head = NULL;


int num_partitions, partition_size, process_id, request_type, size;

// Read partition sizes from file


fscanf(file, "%d", &num_partitions);
for (int i = 0; i < num_partitions; i++) {
fscanf(file, "%d", &partition_size);
insertPartition(&head, partition_size);
}

struct Partition* last_allocated = NULL;

while (fscanf(file, "%d %d %d", &process_id, &request_type, &size) != EOF) {


if (request_type == 1) { // Allocation request
struct Partition* partition = NULL;
if (strategy == 1) {
partition = firstFit(head, process_id, size); // First fit
} else if (strategy == 2) {
partition = bestFit(head, process_id, size); // Best fit
} else if (strategy == 3) {
partition = nextFit(head, &last_allocated, process_id, size); //
Next fit
}

if (partition != NULL) {
allocateMemory(partition, process_id, size);
} else {
printf("Memory allocation failed for Process %d (Requested %d KB)\
n", process_id, size);
}
} else if (request_type == 0) { // Deallocation request
deallocateMemory(head, process_id);
}
displayPartitions(head); // Show memory status after each operation
}
fclose(file);
}

int main() {
int strategy;
printf("Choose allocation strategy:\n1. First Fit\n2. Best Fit\n3. Next Fit\
n");
scanf("%d", &strategy);
simulateMemoryManagement("requests.txt", strategy);
return 0;
}

You might also like