MODIFIED PROGRAMS
MODIFIED PROGRAMS
7) SLL
#include <stdio.h>
#include <stdlib.h>
// Function declarations
void insertf(); // Insert at the front of the list
void insertr(); // Insert at the rear of the list
void deletef(); // Delete from the front of the list
void deleter(); // Delete from the rear of the list
void display(); // Display all nodes in the list
int main() {
int choice;
while (1) {
// Menu for linked list operations
printf("\n *** Operations on Singly Linked List *** \n");
printf("\n 1: Insert at Front");
printf("\n 2: Insert at Rear");
printf("\n 3: Delete from Front");
printf("\n 4: Delete from Rear");
printf("\n 5: Display");
printf("\n 6: Exit");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
cur = first;
while (cur->link != NULL) { // Traverse to the last node
cur = cur->link;
}
cur->link = temp; // Link the new node at the rear
}
temp = first;
printf("\n The first student details are deleted.\n");
first = first->link; // Update the first pointer
free(temp); // Free memory
}
prev = NULL;
cur = first;
while (cur->link != NULL) { // Traverse to the last node
prev = cur;
cur = cur->link;
}
prev->link = NULL; // Update the link of the second last node
printf("\n The last student details are deleted.\n");
free(cur); // Free memory
}
8) DDL
#include <stdio.h>
#include <stdlib.h>
// Define the structure for an employee node in the doubly linked list
struct employee {
char name[15], ssn[15], dept[15], des[15], sal[15], phno[15];
struct employee *rlink, *llink;
};
// Function declarations
void insertf(); // Insert at the front of the list
void insertr(); // Insert at the rear of the list
void deletef(); // Delete from the front of the list
void deleter(); // Delete from the rear of the list
void display(); // Display all nodes in the list
int main() {
int choice;
while (1) {
// Menu for linked list operations
printf("\n *** Operations on Doubly Linked List *** \n");
printf("\n 1: Insert at Front");
printf("\n 2: Insert at Rear");
printf("\n 3: Delete from Front");
printf("\n 4: Delete from Rear");
printf("\n 5: Display");
printf("\n 6: Exit");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
temp = first;
while (temp != NULL) { // Traverse the list
printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",
temp->name, temp->ssn, temp->dept, temp->des, temp->sal, temp-
>phno);
temp = temp->rlink;
count++;
}
printf("\nTotal number of employees: %d\n", count);
}
temp = first;
printf("\n The first employee details are deleted.\n");
cur = first;
while (cur->rlink != NULL) { // Traverse to the last node
cur = cur->rlink;
}
cur->llink->rlink = NULL; // Update the rlink of the second last node
printf("\n The last employee details are deleted.\n");
free(cur); // Free memory
}
9) Polynomial Operations
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// Define the structure for a polynomial term
struct poly {
int cf, px, py, pz; // Coefficient and powers of x, y, z
struct poly *link; // Link to the next term
};
typedef struct poly *NODE;
// Function prototypes
void read(NODE head);
void display(NODE head);
void eval();
void polysum();
void main() {
int ch;
while (1) {
// Menu for polynomial operations
printf("\n 1: Polynomial Evaluation");
printf("\n 2: Sum of Two Polynomials");
printf("\n 3: Exit");
printf("\n\n Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1: eval(); break;
case 2: polysum(); break;
case 3: exit(0); // Exit the program
default: printf("\n Invalid choice! Try again.\n");
}
}
}
// Function to read a polynomial
void read(NODE head) {
NODE temp, cur;
int n, i;
if (head->link == head) {
printf("\nPolynomial doesn't exist\n");
return;
}
temp = head->link;
while (temp != head) {
if (temp->cf < 0)
printf(" %d", temp->cf);
else
printf(" + %d", temp->cf);
printf("x^%dy^%dz^%d", temp->px, temp->py, temp->pz);
temp = temp->link;
}
printf("\n");
}
temp = head->link;
while (temp != head) {
sum += (temp->cf * pow(x, temp->px) * pow(y, temp->py) * pow(z, temp-
>pz));
temp = temp->link;
}
cur1 = head1->link;
while (cur1 != head1) {
prev = head2;
cur2 = head2->link;