Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Rahul Bameta MCA2B 60

Que :Assuming that we have single linked list with pointer head. WAP to delete
a key and update this linked list key will be supplied by the user.

#include <stdio.h>
#include <stdlib.h>
typedef struct queue{
int info;
struct queue *next;
} node_type;
void insert(node_type **);
void search(node_type **);
void display(node_type *);
int main(){
int n, i;
node_type *head = NULL;
printf("How many values do you want to insert: ");
scanf("%d", &n);
for (i = 0; i < n; i++){
printf("Enter the value: ");
insert(&head);
}
printf("The linked list elements are: ");
display(head);
search(&head);
printf("\nThe linked list elements after operations: ");
display(head);
return 0;
}
Rahul Bameta MCA2B 60

void insert(node_type **head){


int num;
node_type *p = (node_type *)malloc(sizeof(node_type));
if (p != NULL){
scanf("%d", &num);
p->info = num;
p->next = NULL;
if (*head == NULL){
*head = p;
}
else{
node_type *temp = *head;
while (temp->next != NULL){
temp = temp->next;
}
temp->next = p;
}
}
}
void search(node_type **head){
int choice, num, key;
node_type *p = *head;
while (1){
int counter = 0;
printf("\nEnter the key to search: ");
scanf("%d", &key);
while (p != NULL && p->info != key){
Rahul Bameta MCA2B 60

counter++;
p = p->next;
}
if (p == NULL){
printf("Key not found\n");
return;
}
else{
printf("\nKey found at index %d\n", counter);
printf("Do you want to delete or update the value of this key?\n1.
Delete\n2. Update\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1){
if (p == *head){
*head = p->next;
free(p);
}
else{
node_type *q = *head;
while (q->next != p){
q = q->next;
}
q->next = p->next;
free(p);
}
printf("Node deleted successfully\n");
}
else if (choice == 2){
Rahul Bameta MCA2B 60

printf("Enter the new element: ");


scanf("%d", &num);
p->info = num;
printf("Node updated successfully\n");
}
else if (choice == 3){
printf("Exiting...");
return;
}else{
printf("Wrong choice. Try again: ");
}
}
p = *head;
}
}
void display(node_type *head){
if (head == NULL){
printf("The queue is empty\n");
}else{
printf("Elements are: ");
while (head != NULL){
printf("%d ", head->info);
head = head->next;
}
printf("\n");
}
}
Rahul Bameta MCA2B 60
Rahul Bameta MCA2B 60

Que : Using linked list find addition of two quadratic equations.

#include <stdio.h>
#include <stdlib.h>
typedef struct Equ {
int multi;
int pow;
struct Equ* next;
} Equ;

void insertEqu(Equ** head, int coeff, int exp);


void displayEquation(Equ* head);
Equ* addEquations(Equ* head1, Equ* head2);

int main() {
Equ* equation1 = NULL;
Equ* equation2 = NULL;
Equ* result;
int choice, mul, pow;
printf("Enter the values for the first equation:\n");
while (1) {
printf("Press 1 for input or any other key to exit: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter the multiplier: ");
scanf("%d", &mul);
printf("Enter the power: ");
Rahul Bameta MCA2B 60

scanf("%d", &pow);
insertEqu(&equation1, mul, pow);
}
else {
printf("\nExiting...\n");
break;
}
}
printf("\nEnter the values for the second equation:\n");
while (1) {
printf("Press 1 for input or any other key to exit: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter the multiplier: ");
scanf("%d", &mul);
printf("Enter the power: ");
scanf("%d", &pow);
insertEqu(&equation2, mul, pow);
}
else {
printf("\nExiting...\n");
break;
}
}
printf("\nEquation 1: ");
displayEquation(equation1);
printf("\nEquation 2: ");
Rahul Bameta MCA2B 60

displayEquation(equation2);
printf("\n");
result = addEquations(equation1, equation2);
printf("Sum of the equations: ");
displayEquation(result);
printf("\n");
return 0;
}
void insertEqu(Equ** head, int coeff, int exp) {
Equ* newEqu = (Equ*)malloc(sizeof(Equ));
newEqu->multi = coeff;
newEqu->pow = exp;
newEqu->next = NULL;
if (*head == NULL) {
*head = newEqu;
}
else {
Equ* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newEqu;
}
}
void displayEquation(Equ* head) {
if (head == NULL) {
printf("0");
Rahul Bameta MCA2B 60

}
else {
while (head != NULL) {
if (head->multi != 0) {
if (head->pow == 0) {
printf("%d", head->multi);
} else {
printf("%dx^%d", head->multi, head->pow);
}
if (head->next != NULL && head->next->multi > 0) {
printf(" + ");
}
}
head = head->next;
}
}
}
Equ* addEquations(Equ* head1, Equ* head2) {
Equ* result = NULL;
while (head1 != NULL || head2 != NULL) {
int coeff = 0, exp = 0;
if (head1 != NULL && head2 != NULL) {
if (head1->pow == head2->pow) {
coeff += head1->multi + head2->multi;
exp = head1->pow;
head1 = head1->next;
head2 = head2->next;
Rahul Bameta MCA2B 60

}
else if (head1->pow > head2->pow) {
coeff += head1->multi;
exp = head1->pow;
head1 = head1->next;
}
else {
coeff += head2->multi;
exp = head2->pow;
head2 = head2->next;
}
}
else if (head1 != NULL) {
coeff += head1->multi;
exp = head1->pow;
head1 = head1->next;
}
else {
coeff += head2->multi;
exp = head2->pow;
head2 = head2->next;
}
insertEqu(&result, coeff, exp);
}
return result;
}
Rahul Bameta MCA2B 60

You might also like