Showing posts with label linked list. Show all posts
Showing posts with label linked list. Show all posts

Thursday, 1 September 2022

POLYNOMIAL ADDITION USING LINKED LIST

 IMPLEMENTATION OF POLYNOMIAL ADDITION USING LINKED LIST

Exercise.No:5 

TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : 

NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "co eff" remove the space between "co" and "eff".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

            4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".

            5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".

            6.)In "p add" remove the space between "p" and "add".

            7.)In "size of" remove the space between "size" and "of".

    * For any queries comment below ,instantly the solution will be posted*

AIM: 

To implement the program for polynomial addition using linked List. 

ALGORITHM: 

1. Using the function poly1() read the coefficient and exponent terms of the first polynomial 

 until exponent term is zero 

2. Using the function poly2() read the coefficient and exponent terms of the second 

 polynomial until exponent term is zero 

3. Using the function polyadd() add the two polynomials with the following comparisons 

4. If the exponent term in the first polynomial is greater than the exponent in the second 

 polynomial, add the node of the first polynomial with the resultant polynomial 

5. If the exponent term in the first polynomial is lees than the exponent in the second 

 polynomial, add the node of the second polynomial with the resultant polynomial 

6. If the exponent term in the first polynomial is equal to the exponent in the second 

 polynomial, add both the coefficient of the first and second polynomial and the node to the 

 resultant polynomial 

7. Traverse both the polynomial according to the above comparison up to the NULL value of 

 both the polynomials are reached. 

8. Display the resultant polynomial 

PROGRAM :

#include <std io. h>

#include <con io. h>

#include <std lib. h>

#include <math. h>

typedef struct node

{ int power;

float co eff;

struct node *next;

}node;

node * insert(node *head, int power, float co eff);

node * create();

node * p add(node *head1,node *head2);

void print(node *head);

int main()

{

node *head1,*head2,*head3;

int op;

float value, x;

print f("\n Enter 1st Polynomial : ");

head1=create();

print(head1);

print f("\n Enter 2nd Polynomial : ");

head2=create();

print(head2);

head3=p add(head1,head2); 

print f("\n Addition of two polynomials : ");

print(head3);

}

node * insert(node *head, int power, float co eff)

{ node *p,*q;

p=(node*)malloc(size of(node));

p->power=power; p->co eff=co eff;

p->next=NULL;

if(head==NULL)

return(p);

else

if(power<head->power)

{ p->next=head;

return(p);

}

else

{ q=head;

while(q->next!=NULL && power>=q->next->power)

q=q->next;

p->next=q->next;

q->next=p;

if(q->power==p->power)

{

q->co eff=q->co eff +p->co eff;

q->next=p->next;

free(p);

}

return(head);

}

}

node * create()

{

int n, I, power;

float co eff;

node *head;

head=NULL;

print f("\n Enter No. of Terms:");

scan f("%d", &n);

print f("\n enter a term as a tuple of (power, coefficient)");

for(I=1;I<=n; I++)

{

scan f("%d %f", &power, &co eff);

head=insert(head, power, co eff);

}

return(head);

}

node * p add(node *head1,node *head2)

{

node *head=NULL;

int power; float co eff;

while(head1 != NULL && head2 != NULL)

{

if(head1->power < head2->power)

{

head=insert(head,head1->power,head1->co eff);

head1=head1->next;

continue;

}

if(head2->power < head1->power)

{ head=insert(head,head2->power,head2->co eff);

head2=head2->next;

continue;

}

head=insert(head,head1->power,head1->coeff+head2->co eff);

head1=head1->next;

head2=head2->next;

}

while(head1!=NULL)

{head=insert(head,head1->power,head1->co eff);

head1=head1->next;

}

while(head2!=NULL)

{head=insert(head,head2->power,head2->co eff);

head2=head2->next;

}

return(head);

}

void print(node *head)

{ print f("\n");

while(head!=NULL)

{

print f("%6.2fX^%d ",head->co eff, head->power);

head=head->next;

}}

OUTPUT :

Enter 1st Polynomial :

Enter No. of Terms:3

enter a term as a tuple of (power, coefficient)3 3 2 2 1 1

1.00X^1 2.00X^2 3.00X^3

Enter 2nd Polynomial :

Enter No. of Terms:2

enter a term as a tuple of (power, coefficient)2 2 5 5

2.00X^2 5.00X^5

Addition of two polynomials :

1.00X^1 4.00X^2 3.00X^3 5.00X^5

RESULT: 

Thus the program for polynomial addition using linked list was implemented and it’s 

executed successfully.

            *Let us promote a thing, "Code with us" YOUTUBE channel doing a coding works for cheap price their services are 
                        1.) APP DEVELOPMENT
                        2.)WEB DEVELOPMENT
                        3.)REVERSE ENGINEERING (Penetration testing)
                        4.)TEACHING PROGRAMMING LANGUAGES
           To reach "Code with us" immediately comment below to book your time slot.

Tuesday, 30 August 2022

Single linked list using C programming advance data structure

IMPLEMENTATION OF SINGLY LINKED LIST AND ITS OPERATIONS

           



 TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNELCode with u - YouTube                                                                                                     

 AIM:

     To implement singly linked list and performing insert, search, display and delete operations. 

 ALGORITHM:

     1: CREATION:

                 a. Creating a node b. Reading details for a node from user c. Connect the node with the list 

                                                                    

    

 2. INSERTION:

                 a. Get the node using the structure node (), and read the node details using the new node().

                 b. Check if the list is empty or not 

                 c. FIRST: The binding field of the new node is made to point to the data field of the first node in the list by assigning the first node. 

                 d. The head pointer is made to point the data field of the new one by assigning the address of the new node.

                 e. LAST: The binding field of the last node is made to point to the data field of the first node in the list by assigning the new node. 

                f. The link field of the new node is set to NULL. 

                g. MIDDLE- the binding field of the new node is made to point to the data field of the next node in the list by assigning its address 

                h. The binding field of the next node is made to point to the data field of the new one by assigning the address of the new node. 

                                                                        


    

 3. DELETION:

                 a. Check whether the list is empty or not. 

                 b. FIRST – set the head pointer of the second node in the list 

                 c. Release the memory for the deleted node d. LAST- the link field of the previous node 

                 e. Release the memory for the deleted node 

                 f. MIDDLE- the link field of the previous node 

                 g. Release the memory for the deleted node 

  4. DISPLAY:

                 a. Get the contents from the list 

                 b. If the list is empty print it is empty. 

                 c. If the list is not empty print the entire list. 

PROGRAM : 

 #include<studio. h>

#include<con io. h>

#include<std lib. h>

 typedef struct node 

{

 int data;

 struct node *next;

 }node;

 node *create();

 node *insert b(node *head, int x);

 node *insert e(node *head, int x);

 node *insert in(node *head, int x);

 node *delete b(node *head);

 node *delete e(node *head); 

 node *delete in(node *head);

 void search(node *head);

 void print(node *head);

 int main() 

{

 int op,op1,x;

 node *head=NULL;

 do

 {

 print f("\n\n1)Create\n2)Insert\n3)Delete\n4)Search"); 

print f("\n5)Print\n6)Quit");

 print f("\n Enter your Choice:");

 scan f("%d", &op);

 switch(op)

 {

 case 1:head=create();

 break;

 case 2:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");

 print f("\n Enter your choice : ");

 scan f("%d",&op1);

 print f("\n Enter the data to be inserted : ");

 scan f("%d", &x);

 switch(op1)

 {

 case 1: head=insert b(head, x);

 break; 

case 2: head=insert e(head, x);

 break;

 case 3: head=insert in(head, x);

 break; 

}

 break;

 case 3:printf("\n\t1)Beginning\n\t2)End\n\t3)In between");

 print f("\n Enter your choice : ");

 scan f("%d",&op1);

 switch(op1)

 {

 case 1:head=delete b(head);

 break;

 case 2:head=delete e(head); 

break;

 case 3:head=delete in(head);

 break;

 }

 break;

 case 4:search(head);

 break; 

case 5:print(head);

 break; 

}

 }

while(op<6);

 }

 node *create() 

{

 node *head,*p;

 int I, n; head=NULL;

 print f("\n Enter no of data:");

 scan f("%d", &n);

 print f("\n Enter the data:"); 

for(I=0;

index=(node*)malloc(size of(node)); 

p=p->next;

 }

 p->next=NULL;

 scan f("%d",&(p->data));

 }

 return(head);

 }

 node *insert b(node *head, int x)

 { 

node *p;

 p=(node*)malloc(size of(node));

 p->data=x;

 p->next=head; 

head=p; 

return(head); 

}

 node *insert e(node *head, int x)

 {

 node *p,*q; p=(node*)malloc(size of(node));

 p->data=x;

 p->next=NULL; 

if(head==NULL) 

return(p); //locate the last node 

for(q=head ;q->next!=NULL ;q=q->next) ;

 q->next=p; return(head);

 }

 node *insert in(node *head, int x)

 { 

node *p,*q;

 int y;

 p=(node*)malloc(size of(node));

 p->data=x;

 p->next=NULL;

 print f("\n Insert after which number ? : ");

 scan f("%d", &y); //locate the data 'y' 

for(q=head ; q != NULL && q->data != y ; q=q->next) ;

 if(q!=NULL)

 {

 p->next=q->next; 

q->next=p;

 }

 else

 print f("\n Data not found ");

 return(head);

 }

 node *delete b(node *head)

 {

 node *p,*q; 

if(head==NULL)

 {

 print f("\n Underflow....Empty Linked List");

 return(head);

 }

 p=head;

 head=head->next;

 free(p);

 return(head);

 }

 node *delete e(node *head)

 {

 node *p,*q; 

if(head==NULL) 

{

 print f("\n Underflow....Empty Linked List");

 return(head);

 }

 p=head;

 if(head->next==NULL) 

{

 // Delete the only element 

head=NULL; 

free(p);

 return(head);

 } 

//Locate the last but one node 

for(q=head ;q->next->next !=NULL ;q=q->next) ;

 p=q->next;

 q->next=NULL;

 free(p);

 return(head);

 }

 node *delete in(node *head)

 {

 node *p,*q; int x, I;

 if(head==NULL)

 {

 print f("\n Underflow....Empty Linked List");

 return(head); 

}

 print f("\n Enter the data to be deleted : "); 

scan f("%d", &x);

 if(head->data==x) 

{

 // Delete the first element 

p=head; 

head=head->next;

 free(p);

 return(head);

 }

 //Locate the node previous to one to be deleted

 for(q=head ;q->next->data!=x && q->next !=NULL ;q=q->next ) ;

 if(q->next==NULL)

 {

 print f("\n Underflow.....data not found"); 

return(head);

 }

 p=q->next; 

q->next=q->next->next;

 free(p);

 return(head);

 }

 void search(node *head)

 {

 node *p;

 int data, loc=1;

 print f("\n Enter the data to be searched: ");

 scan f("%d", &data);

 p=head; 

while(p!=NULL && p->data != data)

 {

 loc++;

 p=p->next; 

}

 if(p==NULL) 

print f("\n Not found:");

 else 

print f("\n Found at location=%d", loc);

 }

 void print(node *head)

 {

 node *p;

 print f("\n\n");

 for(p=head ;p!=NULL ;p=p->next) 

print f("%d ",p->data);

 }

 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit 

Enter your Choice:1

 Enter no of data:5

 Enter the data:1 4 7 2 6 


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:6 

1 4 7 2 6 


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:

5 6 2 7 4 1


 OUTPUT :

 1)create

 2)Insert 

3)Delete

 4)Search

 5)Reverse 

6)Print 

7)Quit

 Enter your Choice:7 


RESULT: 

             Thus the program for singly linked List and its operations was implemented and it’s executed successfully.

FRIENDSHIP & GOALS

WHAT IS FRIENDSHIP:  For all the attention we pay to love stories, some of the most compelling stories (in fiction or not) are about best fr...