0% found this document useful (0 votes)
8 views

MODIFIED PROGRAMS

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

MODIFIED PROGRAMS

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

MODIFIED PROGRAMS (7,8 & 9TH)

7) SLL

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

// Define the structure for a student node in the linked list


struct student {
char name[15], sem[15], usn[15], branch[15], phone[15];
struct student *link;
};

// Global pointers for the linked list


struct student *first = NULL, *temp, *cur, *prev;

// 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);

// Switch case to handle user's choice


switch (choice) {
case 1: insertf(); break;
case 2: insertr(); break;
case 3: deletef(); break;
case 4: deleter(); break;
case 5: display(); break;
case 6: exit(0); // Exit the program
default: printf("\n Invalid choice! Try again.\n");
}
}
return 0;
}

// Function to insert a student at the front of the list


void insertf() {
temp = (struct student *)malloc(sizeof(struct student)); // Allocate memory
printf("\nEnter the Name, Semester, USN, Branch, and Phone Number: ");
scanf("%s%s%s%s%s", temp->name, temp->sem, temp->usn, temp->branch,
temp->phone);
temp->link = first; // Link the new node to the current first node
first = temp; // Update the first pointer
}

// Function to insert a student at the rear of the list


void insertr() {
temp = (struct student *)malloc(sizeof(struct student)); // Allocate memory
printf("\nEnter the Name, Semester, USN, Branch, and Phone Number: ");
scanf("%s%s%s%s%s", temp->name, temp->sem, temp->usn, temp->branch,
temp->phone);
temp->link = NULL;

if (first == NULL) { // If the list is empty


first = temp;
return;
}

cur = first;
while (cur->link != NULL) { // Traverse to the last node
cur = cur->link;
}
cur->link = temp; // Link the new node at the rear
}

// Function to display all student details in the list


void display() {
int count = 0;

if (first == NULL) { // If the list is empty


printf("\n The list is empty!\n");
return;
}

printf("\n The contents of the linked list are: \n");


printf("Name\t\tSem\t\tUSN\t\tBranch\t\tPhone\n");
printf("-----------------------------------------------------------\n");
temp = first;
while (temp != NULL) { // Traverse the list
printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\n",
temp->name, temp->sem, temp->usn, temp->branch, temp->phone);
temp = temp->link;
count++;
}
printf("\nTotal number of students: %d\n", count);
}

// Function to delete a student from the front of the list


void deletef() {
if (first == NULL) { // If the list is empty
printf("\n The list is empty!\n");
return;
}

temp = first;
printf("\n The first student details are deleted.\n");
first = first->link; // Update the first pointer
free(temp); // Free memory
}

// Function to delete a student from the rear of the list


void deleter() {
if (first == NULL) { // If the list is empty
printf("\n The list is empty!\n");
return;
}

if (first->link == NULL) { // If there is only one node


printf("\n The last student details are deleted.\n");
free(first);
first = NULL;
return;
}

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;
};

// Global pointers for the linked list


struct employee *first = NULL, *temp, *cur, *prev;

// 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);

// Switch case to handle user's choice


switch (choice) {
case 1: insertf(); break;
case 2: insertr(); break;
case 3: deletef(); break;
case 4: deleter(); break;
case 5: display(); break;
case 6: exit(0); // Exit the program
default: printf("\n Invalid choice! Try again.\n");
}
}
return 0;
}

// Function to insert an employee at the front of the list


void insertf() {
temp = (struct employee *)malloc(sizeof(struct employee)); // Allocate
memory
printf("\nEnter the Name, SSN, Dept, Designation, Salary, and Phone Number:
");
scanf("%s%s%s%s%s%s", temp->name, temp->ssn, temp->dept, temp->des,
temp->sal, temp->phno);
temp->llink = temp->rlink = NULL;

if (first == NULL) { // If the list is empty


first = temp;
return;
}
temp->rlink = first; // Link the new node to the current first node
first->llink = temp; // Update the llink of the current first node
first = temp; // Update the first pointer
}

// Function to insert an employee at the rear of the list


void insertr() {
temp = (struct employee *)malloc(sizeof(struct employee)); // Allocate
memory
printf("\nEnter the Name, SSN, Dept, Designation, Salary, and Phone Number:
");
scanf("%s%s%s%s%s%s", temp->name, temp->ssn, temp->dept, temp->des,
temp->sal, temp->phno);
temp->llink = temp->rlink = NULL;

if (first == NULL) { // If the list is empty


first = temp;
return;
}
cur = first;
while (cur->rlink != NULL) { // Traverse to the last node
cur = cur->rlink;
}
cur->rlink = temp; // Link the new node at the rear
temp->llink = cur; // Update the llink of the new node
}

// Function to display all employee details in the list


void display() {
int count = 0;

if (first == NULL) { // If the list is empty


printf("\n The list is empty!\n");
return;
}
printf("\n The contents of the doubly linked list are: \n");
printf("Name\t\tSSN\t\tDept\t\tDesignation\t\tSalary\t\tPhone\n");
printf("----------------------------------------------------------------------\n");

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);
}

// Function to delete an employee from the front of the list


void deletef() {
if (first == NULL) { // If the list is empty
printf("\n The list is empty!\n");
return;
}

temp = first;
printf("\n The first employee details are deleted.\n");

first = first->rlink; // Update the first pointer


if (first != NULL) { // If the list is not empty after deletion
first->llink = NULL;
}
free(temp); // Free memory
}
// Function to delete an employee from the rear of the list
void deleter() {
if (first == NULL) { // If the list is empty
printf("\n The list is empty!\n");
return;
}

if (first->rlink == NULL) { // If there is only one node


printf("\n The last employee details are deleted.\n");
free(first);
first = NULL;
return;
}

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;

printf("\nEnter the number of terms: ");


scanf("%d", &n);

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


// Allocate memory for a new node
temp = (NODE)malloc(sizeof(struct poly));
printf("\n Enter coefficient, powers of x, y, z for term %d: ", i + 1);
scanf("%d%d%d%d", &temp->cf, &temp->px, &temp->py, &temp->pz);

// Insert the term at the end of the circular linked list


cur = head;
while (cur->link != head) {
cur = cur->link;
}
cur->link = temp;
temp->link = head;
}
}

// Function to display a polynomial


void display(NODE head) {
NODE temp;

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");
}

// Function to evaluate a polynomial for given values of x, y, z


void eval() {
int sum = 0, x, y, z;
NODE head, temp;

// Create a circular linked list for the polynomial


head = (NODE)malloc(sizeof(struct poly));
head->link = head;

printf("\nEnter the polynomial to evaluate:\n");


read(head);
printf("Polynomial: ");
display(head);

printf("\nEnter the values of x, y, and z: ");


scanf("%d%d%d", &x, &y, &z);

temp = head->link;
while (temp != head) {
sum += (temp->cf * pow(x, temp->px) * pow(y, temp->py) * pow(z, temp-
>pz));
temp = temp->link;
}

printf("\nResult of evaluation: %d\n", sum);


}

// Function to find the sum of two polynomials


void polysum() {
NODE temp, cur1, cur2, prev, head1, head2;

// Create circular linked lists for the two polynomials


head1 = (NODE)malloc(sizeof(struct poly));
head2 = (NODE)malloc(sizeof(struct poly));
head1->link = head1;
head2->link = head2;

printf("\nEnter the first polynomial:\n");


read(head1);
printf("\nEnter the second polynomial:\n");
read(head2);

printf("\nFirst polynomial: ");


display(head1);
printf("\nSecond polynomial: ");
display(head2);

cur1 = head1->link;
while (cur1 != head1) {
prev = head2;
cur2 = head2->link;

while (cur2 != head2) {


// Combine terms with the same powers of x, y, z
if (cur1->px == cur2->px && cur1->py == cur2->py && cur1->pz ==
cur2->pz) {
cur1->cf += cur2->cf;
prev->link = cur2->link;
free(cur2);
break;
}
prev = cur2;
cur2 = cur2->link;
}
cur1 = cur1->link;
}

// Append the remaining terms of the second polynomial to the first


temp = head1->link;
while (temp->link != head1) {
temp = temp->link;
}
temp->link = head2->link;

printf("\nResultant polynomial: ");


display(head1);
}

You might also like