polynomial multiplication using linked list in c not working - c

#include<stdio.h>
#include<stdlib.h>
struct node{
float coeff;
int exp;
struct node *next;
};
struct node*inserts(struct node *start,float co,int ex){
struct node*newnode;
struct node*ptr;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->exp=ex;
newnode->coeff=co;
if(start==NULL||start->exp<ex){
newnode->next=start;
start=newnode;
}
else{
ptr=start;
while(ptr->next!=NULL&&ptr->exp>ex){
ptr=ptr->next;
}
newnode->next=ptr->next;
ptr->next=newnode;
}
return start;
}
struct node*create(struct node *start){
int ex,n;
float co;
printf("enter the number of terms\n");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("enter your coefficient\n");
scanf("%f",&co);
printf("enter your exponent\n");
scanf("%d",&ex);
start=inserts(start,co,ex);
}
return start;
}
struct node* polymulti(struct node *start1,struct node *start2){
struct node*p2;
struct node *p1;
struct node *start3;
p1=start1;
p2=start2;
while(p1!=NULL){
while(p2!=NULL){
start3=inserts(start3,p2->coeff*p1->coeff,p2->exp*p1->exp);
p2=p2->next;
}
p1=p1->next;
}
return start3;
}
void display(struct node*st){
struct node *temp;
temp=st;
while(temp->next!=NULL){
printf("%.1fx^%d",temp->coeff,temp->exp);
printf("+");
temp=temp->next;
}
printf("%.1fx^%d",temp->coeff,temp->exp);
}
int main(){
struct node*start1;
struct node*start2;
struct node*start3;
start1=create(start1);
start2=create(start2);
start3=polymulti(start1,start2);
display(start3);
return 0;
}
i am working on a program that accepts two polynomials and then displays the product of the two polynomials .i have created three linked list ,two of which are used to input the two polynoimals and third is used to display the product. However when i was inserting values into the 2nd linked list ,the program abruptly ended without giving any errors or warning. can anybody tell whats the problem here?

Related

How to solve the pointer issue in the double linked list below?

Recently I learnt about doubly linked lists, and I have tried to write some code in C to implement them. The program below is supposed to receive integer inputs form the user and put them into a list with a maximum of ten integers. However, when I input the values and then print them, only the first and last values are output. How would one solve this issue?
Here is the code:
`#include <stdio.h>
#include <stdlib.h>
struct node {
struct node* prev;
int num;
struct node* next;
};
struct node *head,*p;
struct node* create_first_node(int x)
{
struct node *new = (struct node*)malloc(sizeof(struct node));
new->num=x;
new->prev=NULL;
new->next=NULL;
head=new;
p=head;
return new;
}
void add_node(int x)
{
struct node*new=(struct node*)malloc(sizeof(struct node));
new->num=x;
new->prev=p;
p->next=new;
(new->next)=NULL;
new=p;
}
void print_list(){
struct node *temp= head;
printf("\nThe list is:\n");
while (temp!=NULL)
{
printf("%d\t",temp->num);
temp=temp->next;
}
}
int main(){
int in[10];
int len,i;
head=NULL;
printf("How many nodes would you like?(Max=10) \n");
scanf("%d",&len);
for (i = 0; i < len; i++)
{
if (head == NULL && i==0)
{
printf("Enter Value for node %d\n",i+1);
scanf("%d",(in+i));
create_first_node(in[0]);
}
else
{
printf("Enter Value for node %d\n",i+1);
scanf("%d",(in+i));
add_node(in[i]);
}
}
print_list(head);
return 0;
}`

Some of the elements are not getting added to the Binary Search Tree

I'm implementing a binary search tree with doubly-linked-list in C.
I'm not getting expected output.
Some of the elements are not getting added to the tree, Specifically some right childs.
I need help to fix this. Here is my code,
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
struct node *temp=NULL;
struct node* createnode(int data){
struct node *newNode= (struct node*)malloc(sizeof(struct node));
newNode->data= data;
newNode->left= NULL;
newNode->right= NULL;
return newNode;
}
void createBinaryTree(){
int arr[100],size,i;
printf("Enter no.of elements:");
scanf("%d",&size);
printf("\nEnter elements:");
for(i=0;i<size;++i){
scanf("%d",&arr[i]);
}
root= createnode(arr[0]);
for(i=1;i<size;i++){
temp=root;
while(arr[i]>(temp->data)){
if(temp->right==NULL){
temp->right=createnode(arr[i]);
continue;
}
temp=temp->right;
}
while(arr[i]<(temp->data)){
if(temp->left==NULL){
temp->left=createnode(arr[i]);
continue;
}
temp=temp->left;
}
}
}
Your Code:
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
struct node *temp=NULL;
struct node* createnode(int data){
struct node *newNode= (struct node*)malloc(sizeof(struct node));
newNode->data= data;
newNode->left= NULL;
newNode->right= NULL;
return newNode;
}
void createBinaryTree(){
int arr[100],size,i;
printf("Enter no.of elements:");
scanf("%d",&size);
printf("\nEnter elements:");
for(i=0;i<size;++i){
scanf("%d",&arr[i]);
}
root= createnode(arr[0]);
for(i=1;i<size;i++){
temp=root;
while(arr[i]>(temp->data)){
if(temp->right==NULL){
temp->right=createnode(arr[i]);
continue;
}
temp=temp->right;
}
while(arr[i]<(temp->data)){
if(temp->left==NULL){
temp->left=createnode(arr[i]);
continue;
}
temp=temp->left;
}
}
}
The reason some elements are skipped is because you are adding the wrong way. Those 2 while loops are the problem.
Lets say you have the tree:
10
/ \
5 15
/ \ \
2 7 17
And you want to put the number 6.
Your 1st while will be skipped.
Then with your 2nd while you will go to the node with number 5.
After that you will exit that while and nothing will happen.
You will not reach that NULL.
So to add nodes without a problem just use a while loop that will work as long as NULL hasn't been reached and an if statement inside that loop to check which path you will have to follow in each node.

Structure, pointer and binary search tree in c

The below code is able to add only one element to tree.
Please check what is wrong with this code.
I tried creating a binary search tree with 8 nodes with values taken from array, and when I print the tree only the first element is printed i.e the first element of array.
May be I am not good with working of structure and pointer. Can you say more about pointers and structure in c.
I thank for your help.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
void add(struct node**,struct node*);
void createtree(struct node **root){
int arr[8]={20,45,17,56,40,32,25,48};
int size=sizeof(arr)/sizeof(int);
struct node *temp=NULL;
for(int i=0;i<size;i++)
{
temp=(struct node *)malloc(sizeof(
struct node));
temp->data=arr[i];
temp->left=NULL;
temp->right=NULL;
add(root,temp);
}
}
void add(struct node **root,struct node *temp){
if(*root==NULL){
*root=temp;
return;
}
struct node *curr=*root;
if(temp->data< curr->data){
struct node *temper=curr->left;
add(&temper,temp);
}
else
{
struct node *temper=curr->right;
add(&temper,temp);
}
}
void print(struct node *root){
if(root!=NULL){
print(root->left);
printf("%d ",root->data);
print(root->right);
}
}
int main(){
createtree(&root);
print(root);
return 0;
}

Deleting a nth node in a doubly linked list

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node* HEAD;
void Deleteatn(int c)
{
struct node *store,*store1,*temp=HEAD;
if(c==1)
{
store=temp->next;
store->prev=NULL;
HEAD=store;
free(temp);
}
else
{
for(int i=1;i<c-1;i++)
temp=temp->next;
store=temp->next;
store1=store->next;
temp->next=store->next;
//store1->prev=temp;//DOUBT
free(store);
}
}
void print()
{
struct node *temp=HEAD;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void Insertatend(int b)
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
n->data=b;
n->prev=NULL;
n->next=NULL;
if(HEAD==NULL)
HEAD=n;
else
{
struct node *store,*temp=HEAD;
while(temp!=NULL)
{
store=temp;
temp=temp->next;
}
store->next=n;
n->prev=store;
}
}
int main()
{
int a,b,c;
printf("How many Numbers need to be inserted?\n");
scanf("%d",&a);
for(int i=0;i<a;i++)
{
printf("Enter a number\n");
scanf("%d",&b);
Insertatend(b);
}
printf("The List is\n");
print();
printf("Enter which node need to be deleted?\n");
scanf("%d",&c);
Deleteatn(c);
printf("The List After Deletion is\n");
print();
return 0;
}
Here I have written a program to delete a nth node in a doubly linked list ,I have a doubt that without making a Backward link,how does the output come correctly.So in doubly linked list is it not mandatory to make both forward and backward link?
I have mentioned the code line as doubt,without that code,how is it working???
You can find the issue when you traverse back.
Since you are traversing in forward direction its working fine like a singly linked list.

Linked list basic advice for beginner is needed

Teacher said write program with four functions (print size or add, remove and print node). In line 17 error comes (Cannot convert int* into node). I can't find other way to represent this line so please help. Since it's my first experience with linked list, you can expect tons of error.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{ // Declaring the node (2 way)
int n;
struct node *next;
struct node *prev;
};
int *header; //Declaring header and size counter
int size;
int *finder(int number) //Function that return address of selected node
{
int *adr;
int c;
struct node *temp;
temp=header;
c=0;
while(c==number)
{
temp=temp->next;
c=c+1;
}
return adr;
}
void insert(int data,int number) //insert new node after specified node
{
int *adr;
adr=finder(number);
struct node *current;
struct node *previous;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
current=adr;
previous=current->prev;
temp->n=data;
temp->next=current;
temp->prev=current->prev;
previous->next=temp;
current->prev=temp;
size=size+1;
}
void remove(int number) //remove node after selected one
{
int *adr;
adr=finder(number);
struct node *current;
struct node *previous;
struct node *neeext;
current=adr;
previous= current->prev;
neeext= current->next;
previous->next= neeext;
neeext->prev= previous;
}
void print(int number) //print data of node
{
int *adr;
adr=finder(number);
struct node *current;
printf("%d",current->n);
}
int main() //main function
{
int i,j,d;
size=0;
for(i=1;i<5;i=i+0)
{
if(i==1)
{
printf("%d",size);
}
scanf("%d",&j);
if(i==2)
{
scanf("%d",&d);
insert(d,j);
printf("inserted");
}
if(i==3)
{
remove(j);
printf("removed");
}
if(i==4)
{
print(j);
}
if(i==5)
{
return 0;
}
scanf("%d",&i);
printf("\n");
}
}
You should declare your header variable as struct node *header;, not as int*

Resources