0% found this document useful (0 votes)
8 views45 pages

Sachin DS Assignment Linked List

The document contains a practical assignment for a Data Structure course, submitted by Sachin Nayal to Dr. Chetna Laroiya. It includes implementations of various operations for singly linked lists, circular linked lists, and doubly linked lists using C programming. The code features functions for creating nodes, adding, deleting, and displaying nodes in these data structures.

Uploaded by

try.alex.id
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)
8 views45 pages

Sachin DS Assignment Linked List

The document contains a practical assignment for a Data Structure course, submitted by Sachin Nayal to Dr. Chetna Laroiya. It includes implementations of various operations for singly linked lists, circular linked lists, and doubly linked lists using C programming. The code features functions for creating nodes, adding, deleting, and displaying nodes in these data structures.

Uploaded by

try.alex.id
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/ 45

(Affiliated to)

GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY


SECTOR – 16 C, DWARKA, NEW DELHI

Data Structure Practical File

Assignment -1

Submitted To: Dr. Chetna Laroiya Submitted By: SACHIN NAYAL

Enrollment No.: 00750404423


MCA 1st Year (Section - B)
2st Semester

SACHIN NAYAL ENROLL_NO:-00750404423


Q1 Write a menu driven program to implement following operations in Singly Linked List.

#include <stdio.h>

#include <stdlib.h>

struct student{

char name[20];

int rollno;

struct student* next;

}*head;

void createNodelist(int r)

struct student *newnode,*temp;

head=(struct student *)malloc(sizeof(struct student));

//search if the first node is created or not

if(head==NULL)

printf(" Memory not allocated ");

exit(0);

printf("Enter student name : ");

scanf("%s",&head->name);

printf("Enter student Rollno :");

scanf("%d",&head->rollno);

head->next=NULL;

SACHIN NAYAL ENROLL_NO:-00750404423


temp =head;

for(int i=1 ;i<r;i++){

//allocated memory to the next node

newnode=(struct student *) malloc(sizeof(struct student)) ;

if(newnode == NULL ){

printf("Memory can't be allocated \n") ;

break;

printf("Enter student name : ");

scanf("%s",&newnode->name);

printf("Enter student Rollno :");

scanf("%d",&newnode->rollno);

newnode->next=NULL;

temp->next=newnode;

temp=temp->next;

void add_at_the_end(char name[],int rollno)

struct student *ptr,*end_node;

if (head== NULL)

printf("memory not allocated");

SACHIN NAYAL ENROLL_NO:-00750404423


exit(0);

ptr= head;

end_node = (struct student *)malloc(sizeof(struct student));

strcpy(end_node->name, name);

end_node->rollno=rollno ;

end_node->next=NULL;

while(ptr->next !=NULL)

ptr=ptr->next;

// end_node->name[]=name; cannot directly copy an array to another

ptr->next=end_node;

void insert_at_begining(char name[], int rollno){

struct student *newnode=(struct student*) malloc(sizeof(struct student)) ;

strcpy(newnode ->name , name );

newnode->rollno=rollno;

newnode->next=head;

head=newnode;

void insert_before(char name[], int rollno,int before){

struct student *ptr,*newnode,*prev;

SACHIN NAYAL ENROLL_NO:-00750404423


ptr=head;

newnode= (struct student*)malloc(sizeof(struct student));

strcpy(newnode->name , name ) ;

newnode->rollno=rollno;

newnode->next=NULL;

while(ptr !=NULL && ptr->rollno != before){

prev=ptr;

ptr=ptr->next;

newnode->next=prev->next;

prev-> next=newnode;

void delete_first_node(){

// deletes the first node

if(head== NULL)

printf("List is empty\n");

else{

struct student *temp = head;

head = head->next;

free(temp); // name and rollno is deleted but it still holds the address so we set it to null

temp=NULL;// for safety reason

void delete_last(){

SACHIN NAYAL ENROLL_NO:-00750404423


//deleting last node

struct student *curr,*prev;

curr=head;

prev=NULL;

while((curr->next !=NULL)) {

prev = curr;

curr = curr->next;

prev->next=NULL ;

free(curr);

curr=NULL;

void delete_posi(){

struct student *del , *prev;

int pos;

printf("\nEnter position of node to be deleted : ");

scanf("%d",&pos);

del=head;

prev=NULL;

if (head == NULL || pos<1){

printf("Invalid Position \n");

return;

for (int i=0; i<pos-1; i++) {

prev = del;

del = del->next;

if(del==NULL)return;

if(prev!=NULL)

SACHIN NAYAL ENROLL_NO:-00750404423


prev->next=del->next;

else

head = del->next; /* If the node to be deleted is head then point*/

/* head to the next node */

free(del); /* Free the memory occupied by the node */

void printList() {

struct student *ptr;

//checks if linked list is empty

if(head ==NULL){

printf("Linkedlist is Empty. ");

return;

ptr= head;

while(ptr != NULL) //iterates till the last node

printf("\n Name :%s and Rollno :%d ",ptr->name,ptr->rollno );

ptr=ptr->next;

int main() {

int choice, rollno, before,no_of_student;

char name[20];

printf("\nCircular Linked List Operations:\n");

printf("1. Create linked list\n");

SACHIN NAYAL ENROLL_NO:-00750404423


printf("2. Add node at the end\n");

printf("3. Insert node at the beginning\n");

printf("4. Insert node before a specific roll number\n");

printf("5. Delete first node\n");

printf("6. Delete last node\n");

printf("7. Delete node from a specific position\n");

printf("8. Display list\n");

printf("9. Exit\n");

while (1) {

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the number of students: ");

scanf("%d", &no_of_student);

createNodelist(no_of_student);

break;

case 2:

printf("Enter name and roll number to add at the end: ");

scanf("%s %d", name, &rollno);

add_at_the_end(name, rollno);

break;

case 3:

printf("Enter name and roll number to insert at the beginning: ");

scanf("%s %d", name, &rollno);

insert_at_begining(name, rollno);

break;

case 4:

printf("Enter name, roll number, and roll number before which to insert: ");

scanf("%s %d %d", name, &rollno, &before);

SACHIN NAYAL ENROLL_NO:-00750404423


insert_before(name, rollno, before);

break;

case 5:

printf("Deleting first node...\n");

delete_first_node();

break;

case 6:

printf("Deleting last node...\n");

delete_last();

break;

case 7:

printf("Deleting node from specific position...\n");

delete_posi();

break;

case 8:

printf("Displaying list:\n");

printList();

break;

case 9:

printf("Exiting program...\n");

exit(0);

default:

printf("Invalid choice! Please enter a valid option.\n");

return 0;

SACHIN NAYAL ENROLL_NO:-00750404423


SACHIN NAYAL ENROLL_NO:-00750404423
Q2Write a menu driven program to implement following operations in Circular Linked List.

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

struct node{

int data;

struct node* next;

};

struct node* single_node(){

int data =56;

struct node* temp=(struct node* )malloc(sizeof(struct node));

temp->data=data;

temp->next=temp; //as node has only 1 node it's next points to itself

return temp;

struct node* add_at_beg(struct node *tail,int data)

struct node* newnode=(struct node*) malloc(sizeof(struct node));

newnode->data=data;

newnode->next=tail->next; //newnode next will contain address previously first node address
sotred in tail-> next

tail->next=newnode; //update the new first node address with newnode address

return tail;

struct node* add_at_end(struct node* tail,int data)

SACHIN NAYAL ENROLL_NO:-00750404423


struct node* newnode=(struct node* )malloc(sizeof(struct node));

newnode->data=data;

newnode->next=tail->next;

tail->next=newnode;

tail=tail->next;

return tail;

struct node* add_after_pos(struct node* tail, int data, int pos )

struct node *newnode=(struct node *)malloc(sizeof(struct node));

struct node *p=tail->next;

newnode->data=data;

newnode->next=NULL;

while(pos >1){ //iterate to the (pos-1)th position

p=p->next;

pos--;

newnode->next=p->next;

p->next=newnode;

if(p==tail)//when adding after last node

tail=tail->next;

return tail;

SACHIN NAYAL ENROLL_NO:-00750404423


}

struct node* insert_before(struct node *tail, int value, int data) {

if (tail == NULL) {

printf("Circular linked list is empty!\n");

return NULL;

// Create a new node

struct node *newnode = (struct node *)malloc(sizeof(struct node));

if (newnode == NULL) {

printf("Memory allocation failed!\n");

exit(EXIT_FAILURE);

newnode->data = data;

newnode->next = NULL;

// Find the node with the specified value

struct node *current = tail->next;

struct node *prev = tail;

do {

if (current->data == value) {

// Insert the new node before the current node

newnode->next = current;

prev->next = newnode;

printf("Node with data %d inserted before %d.\n", data, value);

return tail;

prev = current;

current = current->next;

} while (current != tail->next);

SACHIN NAYAL ENROLL_NO:-00750404423


// If the specified value is not found

printf("Value %d not found in the list!\n", value);

return tail;

bool isempty(struct node* tail) {

return tail == NULL;

struct node* del_begin(struct node* tail)

if (tail == NULL)

return tail;

if(tail->next == tail)

free(tail);

tail=NULL;

return tail;

struct node* temp = tail->next;

tail->next=temp->next;

free(temp);

temp=NULL;

return tail;

struct node* del_pos(struct node* tail)

SACHIN NAYAL ENROLL_NO:-00750404423


int pos;

printf("\n enter the position to delete from : ");

struct node* del_node,*temp;

del_node=tail->next;

scanf("%d",&pos);

if(pos == 1)

return del_begin(tail);

else

while(pos > 1 )

temp=del_node;

del_node=del_node->next;

pos--;

temp->next=del_node->next;

return tail;

struct node* del_before_data(struct node* tail,int data)

struct node* prev,*temp;

temp=tail->next;

if(temp->next->data == data)

SACHIN NAYAL ENROLL_NO:-00750404423


{

return del_begin(tail);

while( temp->next->data != data )

prev=temp;

temp=temp->next;

prev->next=temp->next;

free(temp);

temp=NULL;

return tail;

};

struct node* del_end(struct node* tail)

if(tail == NULL)

return tail;

struct node* temp=tail->next;

if(tail->next == tail)

free(tail);

tail=NULL;

return tail;

SACHIN NAYAL ENROLL_NO:-00750404423


while(temp->next!=tail)

temp=temp->next;

temp->next=tail->next;

free(tail);

tail=temp;

return tail;

void printlist(struct node* tail)

if(tail==NULL)

printf("\nNO NODE IN THE LIST");

else

printf("The list is :\n");

struct node* p;

p=tail->next;

do // us do while instead of while bcz for first case p is equal to tail->next

printf("\n%d\n",p->data);

p=p->next;

}while(p!=tail->next);

int main()

SACHIN NAYAL ENROLL_NO:-00750404423


struct node* tail;

printf("A menu driven program to implement following operations in Circular Linked List."

"Enter options 1 t0 8\n1 to Create \n2 to InsLast\n3 to InsBeg\n4 to InsAfter\n"

"5 to InsBefore\n6 to DelLast\n7 to DelBeg\n8 to dDelAfter.\n");

while(1){

int opt,val,pos;

printf("\nEnter your option:");

scanf("%d",&opt);

switch (opt) {

case 1 : tail=single_node(tail); printlist(tail);break;

case 2 :

scanf("%d", &val);

tail = add_at_end(tail, val);printlist(tail); break;

case 3:

scanf("%d", &val);

tail = add_at_beg(tail, val); printlist(tail);break;

case 4:

printf(" Enter value to insert after : ");

scanf("%d",&val);

printf("Position : ");

scanf("%d",&pos);

if(!isempty(tail)){

tail = add_after_pos(tail, val, pos);

printlist(tail);

else{

printf("List is empty. Please create a list first.");

break;

SACHIN NAYAL ENROLL_NO:-00750404423


case 5:

printf(" Value to Insert Before : ");

scanf("%d",&val);

printf("Position : ");

scanf("%d",&pos);

if(!isempty(tail))

{ tail = add_after_pos(tail, val, pos);

printlist(tail);

else{

printf("The list is Empty! Please create the list first.");

break;

case 6:

if (!isempty(tail)){

del_end(tail);

printlist(tail);}

else

printf("The List is already Empty!\n");

break;

case 7:

if (!isempty(tail)){

del_begin(tail);

printlist(tail);}

else

printf("The List is already Empty!\n");

break;

case 8:

printf("Enter value to delete its prior node : ");

del_before_data(tail,val);

SACHIN NAYAL ENROLL_NO:-00750404423


printlist(tail);

break;

default:printf("\n\tInvalid Option!!\n"); exit(0);

return 0;

SACHIN NAYAL ENROLL_NO:-00750404423


SACHIN NAYAL ENROLL_NO:-00750404423
Q3 Write a menu driven program to implement following operations in Doubly Linked List

#include <stdio.h>

#include <stdlib.h>

// Node structure for doubly linked list

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (newNode == NULL) {

printf("Memory allocation failed!\n");

exit(EXIT_FAILURE);

newNode->data = data;

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

// Function to insert a node at the end of the doubly linked list

void insertLast(struct Node** headRef, int data) {

struct Node* newNode = createNode(data);

if (*headRef == NULL) {

*headRef = newNode;

} else {

struct Node* current = *headRef;

SACHIN NAYAL ENROLL_NO:-00750404423


while (current->next != NULL) {

current = current->next;

current->next = newNode;

newNode->prev = current;

printf("Node with data %d inserted at the end.\n", data);

// Function to insert a node at the beginning of the doubly linked list

void insertBegin(struct Node** headRef, int data) {

struct Node* newNode = createNode(data);

if (*headRef == NULL) {

*headRef = newNode;

} else {

newNode->next = *headRef;

(*headRef)->prev = newNode;

*headRef = newNode;

printf("Node with data %d inserted at the beginning.\n", data);

// Function to insert a node after a specified value in the doubly linked list

void insertAfter(struct Node** headRef, int key, int data) {

if (*headRef == NULL) {

printf("List is empty!\n");

return;

struct Node* current = *headRef;

while (current != NULL && current->data != key) {

current = current->next;

SACHIN NAYAL ENROLL_NO:-00750404423


}

if (current == NULL) {

printf("Key %d not found in the list!\n", key);

} else {

struct Node* newNode = createNode(data);

newNode->prev = current;

newNode->next = current->next;

if (current->next != NULL) {

current->next->prev = newNode;

current->next = newNode;

printf("Node with data %d inserted after %d.\n", data, key);

// Function to insert a node before a specified value in the doubly linked list

void insertBefore(struct Node** headRef, int key, int data) {

if (*headRef == NULL) {

printf("List is empty!\n");

return;

struct Node* current = *headRef;

while (current != NULL && current->data != key) {

current = current->next;

if (current == NULL) {

printf("Key %d not found in the list!\n", key);

} else {

struct Node* newNode = createNode(data);

newNode->next = current;

newNode->prev = current->prev;

SACHIN NAYAL ENROLL_NO:-00750404423


if (current->prev != NULL) {

current->prev->next = newNode;

} else {

*headRef = newNode;

current->prev = newNode;

printf("Node with data %d inserted before %d.\n", data, key);

// Function to delete the last node of the doubly linked list

void deleteLast(struct Node** headRef) {

if (*headRef == NULL) {

printf("List is empty!\n");

return;

struct Node* current = *headRef;

while (current->next != NULL) {

current = current->next;

if (current->prev != NULL) {

current->prev->next = NULL;

} else {

*headRef = NULL;

free(current);

printf("Last node deleted.\n");

// Function to delete the first node of the doubly linked list

void deleteBegin(struct Node** headRef) {

SACHIN NAYAL ENROLL_NO:-00750404423


if (*headRef == NULL) {

printf("List is empty!\n");

return;

struct Node* temp = *headRef;

*headRef = (*headRef)->next;

if (*headRef != NULL) {

(*headRef)->prev = NULL;

free(temp);

printf("First node deleted.\n");

void deleteAfter(struct Node** headRef, int key) {

if (*headRef == NULL) {

printf("List is empty!\n");

return;

struct Node* current = *headRef;

while (current != NULL && current->data != key) {

current = current->next;

if (current == NULL || current->next == NULL) {

printf("Key %d not found or does not have a node after it!\n", key);

return;

struct Node* temp = current->next;

current->next = temp->next;

if (temp->next != NULL) {

temp->next->prev = current;

free(temp);

SACHIN NAYAL ENROLL_NO:-00750404423


printf("Node after %d deleted.\n", key);

// Function to delete the node before a specified value in the doubly linked list

void deleteBefore(struct Node** headRef, int key) {

if (*headRef == NULL || (*headRef)->next == NULL) {

printf("List is empty or has only one node!\n");

return;

struct Node* current = *headRef;

while (current != NULL && current->data != key) {

current = current->next;

if (current == NULL || current->prev == NULL) {

printf("Key %d not found or does not have a node before it!\n", key);

return;

struct Node* temp = current->prev;

if (temp->prev != NULL) {

temp->prev->next = current;

} else {

*headRef = current;

current->prev = temp->prev;

free(temp);

printf("Node before %d deleted.\n", key);

// Function to display the doubly linked list

void display(struct Node* head) {

if (head == NULL) {

SACHIN NAYAL ENROLL_NO:-00750404423


printf("List is empty!\n");

return;

printf("Doubly Linked List: ");

struct Node* current = head;

while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

printf("\n");

int main() {

struct Node* head = (struct Node* )malloc(sizeof(struct Node));

int choice, data, key;

printf("\nDoubly Linked List Operations:\n");

printf("1. Create\n");

printf("2. InsLast\n");

printf("3. InsBeg\n");

printf("4. InsAfter\n");

printf("5. InsBefore\n");

printf("6. DelLast\n");

printf("7. DelBeg\n");

printf("8. DelAfter\n");

printf("9. DelBefore\n");

printf("10. Exit\n");

while (1) {

printf("Enter your choice: ");

scanf("%d", &choice);

SACHIN NAYAL ENROLL_NO:-00750404423


switch (choice) {

case 1:

// Create operation

printf("Enter data to create list : ");

scanf("%d", &data);

head=createNode(data);

break;

// You can add code here to create the doubly linked list

case 2:

// Insert at the end operation

printf("Enter data to insert at the end: ");

scanf("%d", &data);

insertLast(&head, data);

break;

case 3:

// Insert at the beginning operation

printf("Enter data to insert at the beginning: ");

scanf("%d", &data);

insertBegin(&head, data);

break;

case 4:

// Insert after a value operation

printf("Enter value after which to insert: ");

scanf("%d", &key);

printf("Enter data to insert: ");

scanf("%d", &data);

insertAfter(&head, key, data);

break;

case 5:

SACHIN NAYAL ENROLL_NO:-00750404423


// Insert before a value operation

printf("Enter value before which to insert: ");

scanf("%d", &key);

printf("Enter data to insert: ");

scanf("%d", &data);

insertBefore(&head, key, data);

break;

case 6:

// Delete last node operation

deleteLast(&head);

break;

case 7:

// Delete first node operation

deleteBegin(&head);

break;

case 8:

printf("Enter value to delete its next node: ");

scanf("%d", &key);

deleteAfter(&head, key);

break;

case 9:

printf("Enter value to delete its previous node: ");

scanf("%d", &key);

deleteBefore(&head, key);

break;

case 10:

// Exit

printf("Exiting program...\n");

exit(0);

default:

printf("Invalid choice! Please enter a valid option.\n");

SACHIN NAYAL ENROLL_NO:-00750404423


}

// Display the doubly linked list after each operation

display(head);

return 0;

SACHIN NAYAL ENROLL_NO:-00750404423


SACHIN NAYAL ENROLL_NO:-00750404423
Q4 WAP to merge 2 sorted Singly Linked List to create a sorted Linked List. ()

#include <stdio.h>

#include <stdlib.h>

struct sorted_list{

int data;

struct sorted_list* next;

};

struct sorted_list* create_list (int value) {

struct sorted_list *head, *ptr ;

head= (struct sorted_list*)malloc(sizeof(struct sorted_list));

if(head==NULL)

printf( "Memory error\n");

exit(0);

printf("Adding element to the list: ");

scanf("%d",&head->data);

head->next= NULL;

ptr=head;

for(int i=1; i<value;i++)

struct sorted_list* newNode=(struct sorted_list *) malloc(sizeof(struct sorted_list)) ;

if(newNode == NULL){

SACHIN NAYAL ENROLL_NO:-00750404423


printf("\n Memory not available \n");

break;

printf("Adding element to the list: ");

scanf("%d",&newNode->data);

newNode->next=NULL;

ptr->next=newNode;

ptr=ptr->next;

return head;

void display(struct sorted_list * head) {

struct sorted_list* ptr;

if(head==NULL) {

printf("List is empty!");

return;

ptr=head;

while (ptr != NULL) {

printf("%d ", ptr->data );

ptr = ptr->next;

struct sorted_list* mergeSortedLists(struct sorted_list* head1, struct sorted_list* head2) {

struct sorted_list* mergedList = NULL;

SACHIN NAYAL ENROLL_NO:-00750404423


if (head1 == NULL)

return head2;

if (head2 == NULL)

return head1;

if (head1->data <= head2->data) {

mergedList = head1;

mergedList->next = mergeSortedLists(head1->next, head2);

} else {

mergedList = head2;

mergedList->next = mergeSortedLists(head1, head2->next);

return mergedList;

int main(){

int num1,num2;

struct sorted_list *sortedhead1,*sortedhead2,*merged;

printf("no.of elements in list1 in sorted order: ");

scanf("%d", &num1); // number of elements in first list

sortedhead1=create_list(num1);

printf("\nno.of elements in list2 in sorted order: ");

scanf("%d", &num2); // number of elements in second list

sortedhead2= create_list(num2);

display(sortedhead1);

SACHIN NAYAL ENROLL_NO:-00750404423


printf("\n");

display(sortedhead2);

printf("\nprinting sorted list :");

merged=mergeSortedLists(sortedhead1, sortedhead2);

display(merged);

SACHIN NAYAL ENROLL_NO:-00750404423


Q7 WAP to concatenate 2 Linked List.

#include <stdio.h>

#include <stdlib.h>

struct sorted_list{

int data;

struct sorted_list* next;

};

struct sorted_list* create_list (int value) {

struct sorted_list *head, *ptr ;

head= (struct sorted_list*)malloc(sizeof(struct sorted_list));

if(head==NULL)

printf( "Memory error\n");

exit(0);

printf("Adding element to the list: ");

scanf("%d",&head->data);

head->next= NULL;

ptr=head;

for(int i=1; i<value;i++)

SACHIN NAYAL ENROLL_NO:-00750404423


struct sorted_list* newNode=(struct sorted_list *) malloc(sizeof(struct sorted_list)) ;

if(newNode == NULL){

printf("\n Memory not available \n");

break;

printf("Adding element to the list: ");

scanf("%d",&newNode->data);

newNode->next=NULL;

ptr->next=newNode;

ptr=ptr->next;

return head;

void display(struct sorted_list * head) {

struct sorted_list* ptr;

if(head==NULL) {

printf("List is empty!");

return;

ptr=head;

while (ptr != NULL) {

printf("%d ", ptr->data );

ptr = ptr->next;

SACHIN NAYAL ENROLL_NO:-00750404423


struct sorted_list * join_list(struct sorted_list *head1,struct sorted_list *head2)

struct sorted_list *ptr=head1;

while(ptr->next!=NULL){

ptr=ptr->next;

ptr->next=head2;

return head1;

int main(){

printf("no.of elements in head1 in sorted order: ");

int num1,num2;

struct sorted_list *sortedhead1,*sortedhead2,*merged;

scanf("%d", &num1); // number of elements in first list

sortedhead1=create_list(num1);

printf("\nPrinting elements of list1 : ");

display(sortedhead1);

printf("\nno.of elements in head2 in sorted order: ");

scanf("%d", &num2); // number of elements in first list

sortedhead2= create_list(num2);

printf("\nprinting elements of list2 : ");

display(sortedhead2);

printf("\nPrinting concatenatedlist :");

SACHIN NAYAL ENROLL_NO:-00750404423


merged=join_list(sortedhead1, sortedhead2);

display(merged);

return 0;

Q8 A polynomial is an expression where each term holds a coefficient and an exponent. Develop and
test the program to perform addition operation, on two different expressions of a single variable,
with a suitable data structure.

#include <stdio.h>

#include <stdlib.h>

// Structure to represent a term in a polynomial

struct Term {

int coefficient;

int exponent;

SACHIN NAYAL ENROLL_NO:-00750404423


struct Term* next;

};

// Function to create a new term

struct Term* createTerm(int coefficient, int exponent) {

struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));

if (newTerm == NULL) {

printf("Memory allocation failed!\n");

exit(EXIT_FAILURE);

newTerm->coefficient = coefficient;

newTerm->exponent = exponent;

newTerm->next = NULL;

return newTerm;

// Function to insert a term at the end of a polynomial

void insertTerm(struct Term** poly, int coefficient, int exponent) {

struct Term* newTerm = createTerm(coefficient, exponent);

if (*poly == NULL) {

*poly = newTerm;

} else {

struct Term* current = *poly;

while (current->next != NULL) {

current = current->next;

current->next = newTerm;

// Function to add two polynomials

SACHIN NAYAL ENROLL_NO:-00750404423


struct Term* addPolynomials(struct Term* poly1, struct Term* poly2) {

struct Term* result = NULL;

struct Term* current1 = poly1;

struct Term* current2 = poly2;

while (current1 != NULL && current2 != NULL) {

if (current1->exponent > current2->exponent) {

insertTerm(&result, current1->coefficient, current1->exponent);

current1 = current1->next;

} else if (current1->exponent < current2->exponent) {

insertTerm(&result, current2->coefficient, current2->exponent);

current2 = current2->next;

} else {

int sum = current1->coefficient + current2->coefficient;

if (sum != 0) {

insertTerm(&result, sum, current1->exponent);

current1 = current1->next;

current2 = current2->next;

// Append remaining terms from poly1, if any

while (current1 != NULL) {

insertTerm(&result, current1->coefficient, current1->exponent);

current1 = current1->next;

// Append remaining terms from poly2, if any

while (current2 != NULL) {

insertTerm(&result, current2->coefficient, current2->exponent);

SACHIN NAYAL ENROLL_NO:-00750404423


current2 = current2->next;

return result;

// Function to display a polynomial

void displayPolynomial(struct Term* poly) {

struct Term* current = poly;

while (current != NULL) {

printf("%dx^%d ", current->coefficient, current->exponent);

current = current->next;

if (current != NULL) {

printf("+ ");

printf("\n");

// Function to free memory allocated for polynomial

void freePolynomial(struct Term* poly) {

struct Term* current = poly;

while (current != NULL) {

struct Term* temp = current;

current = current->next;

free(temp);

int main() {

// Example polynomials: 3x^2 + 2x^1 + 5 and 4x^3 + 2x^2 + 3x^1 + 1

SACHIN NAYAL ENROLL_NO:-00750404423


struct Term* poly1 = NULL;

struct Term* poly2 = NULL;

// Insert terms into the first polynomial

insertTerm(&poly1, 2, 7);

insertTerm(&poly1, 5, 4);

insertTerm(&poly1, 34, 0);

// Insert terms into the second polynomial

insertTerm(&poly2, 2, 2);

insertTerm(&poly2, 12, 1);

insertTerm(&poly2, 21, 0);

printf("First polynomial: ");

displayPolynomial(poly1);

printf("Second polynomial: ");

displayPolynomial(poly2);

// Add the polynomials

struct Term* result = addPolynomials(poly1, poly2);

printf("Sum of polynomials: ");

displayPolynomial(result);

// Free allocated memory

freePolynomial(poly1);

freePolynomial(poly2);

freePolynomial(result);

return 0;

SACHIN NAYAL ENROLL_NO:-00750404423


}

SACHIN NAYAL ENROLL_NO:-00750404423

You might also like