Linked list exercise in C, what is wrong? - linked-list

The code below does compile, but it doesn't run as it should.
I'm not sure what am I doing wrong, so would someone be willing to tell me what I did wrong and what I should have done better.
What do I need to change to make it run properly?
#include<stdio.h>
#include<stdlib.h>
typedef struct sub_Node
{
int value;
struct sub_Node *next;
}sub_Node;
typedef struct Node
{
char *name;
struct Node *next;
struct sub_Node *sub_start;
}Node;
Node *start;
void add_player(char *name)
{
Node *temp;
temp = (Node *)malloc(sizeof(Node));
temp->next = start;
temp->name = name;
temp->sub_start = (sub_Node *)malloc(sizeof(sub_Node));
temp->sub_start->next = NULL;
temp->sub_start->value = -1;
start = temp;
}
void initialize()
{
char *p;
p = "\0";
add_player(p);
}
void remove_player(char *name)
{
Node *p;
for(p = start; p!= NULL; p = p->next)
if(p->name == name)
{
p->name = p->next->name;
p->next = p->next->next;
}
}
sub_Node* add_descending(sub_Node* sub_start, int piece_value)
{
sub_Node *temp, *prev, *next;
temp = (sub_Node *)malloc(sizeof(sub_Node));
temp->value = piece_value;
temp->next = NULL;
prev = NULL;
next = sub_start;
while(next && next->value >= piece_value)
{
prev = next;
next = next->next;
}
if(!next)
{
prev->next = temp;
}
else
{
if(prev)
{
temp->next = prev->next;
prev->next = temp;
}
else
{
temp->next = sub_start;
sub_start = temp;
}
}
return sub_start;
}
void add_piece(char *name, int piece_value)
{
Node *p;
int c;
for(p = start; p!=NULL; p = p->next)
if(p->name == name)
p->sub_start = add_descending(p->sub_start, piece_value);
}
void print_pieces(char *name)
{
Node *p;
sub_Node *q;
for(p = start; p!=NULL; p = p->next)
if(p->name == name)
{
printf("The values of the owned pieces are:");
for(q = p->sub_start; q->value != -1; q = q->next)
printf(" %d", q->value);
}
}
int lose_piece(char *name)
{
Node *p;
sub_Node *q;
int aux;
for(p = start; p!=NULL; p = p->next)
if(p->name == name)
{
for(q = p->sub_start; q->next->value != -1; q = q->next) {}
aux = q->value;
q->value = q->next->value;
q->next = q->next->next;
return aux;
}
}
void print_players()
{
Node *p;
printf("The players are: ");
for(p = start; p->name != "\0"; p = p->next)
printf("%s ", p->name);
printf("\n");
}
int main()
{
initialize();
int y, value;
char name[20];
printf("Insert a digit to execute the desired task:\n"
"<0> end the program\n"
"<1> add a player, who doesn't own any piece yet\n"
"<2> remove a player and all his pieces\n"
"<3> print the name of all the players\n"
"<4> a player gets a piece\n"
"<5> a player loses the piece with the lowest value out of the ones that he has\n"
"<6> prints the pieces of a player in a descending order by value\n\n");
do
{
printf("digit: ");
scanf("%d", &y);
switch(y)
{
case 1:
printf("Insert the player's name: ");
scanf("%s", name);
add_player(name);
break;
case 2:
printf("Insert the player's name: ");
scanf("%s", name);
remove_player(name);
break;
case 3:
print_players();
break;
case 4:
printf("Insert the player's name: ");
scanf("%s", name);
printf("Insert the value of the piece: ");
scanf("%d", value);
add_piece(name, value);
break;
case 5:
printf("Insert the player's name: ");
scanf("%s", name);
printf("\nThe player loses the piece: %d\n", lose_piece(name));
break;
case 6:
printf("Insert the player's name: ");
scanf("%s", name);
print_pieces(name);
}
} while(y != 0);
return 0;
}

your two main problems where this scanf("%d", value); value should be passed by reference like this scanf("%d", &value); and the second is string comparison in c as in your code p->name != "\0" and if(p->name == name) this is wrong because actually you are making comparison between addresses of strings (where it resides in memory) not strings values. to compare strings in c you have to use strcmp and families.
Actually 3 main problems. for setting string values as you did in temp->name = name; is little bit more complicated than that. because you are assigning to temp->name a string from the stack that is volatile (the stack will be more likely invalid soon you return from the function) . in your case you have to alloc a new string by using malloc (and friends) or just by using strdup.
here is as a bonus a slightly rewrite of your program, you will find many advises and is a good starting point for how to structure your code for an easy maintenance.
still want to advise you to change members and variables to more declarative names as in sub_start and sub_Node can be PieceNode and pieces respectively.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct sub_Node
{
int value;
struct sub_Node *next;
}sub_Node;
typedef struct Node
{
char *name;
struct Node *next;
struct Node *prev; // this to make life easyer
struct sub_Node *sub_start;
}Node;
Node *start = NULL;
Node *find_player(char *name){
Node *tmp = start;
while( tmp ){
if(strcmp(tmp->name,name) == 0 )
break;
tmp = tmp->next;
}
return tmp;
}
// int to return Error Code
//
int add_player(char *name)
{
Node *temp;
if( find_player(name)) {
printf("player %s already exists\n", name);
return 1;
}
// do not cast malloc
temp = malloc(sizeof(Node));
if( !temp ){
printf ("not enough memory\n");
return 2;
}
temp->name = strdup ( name); // here was your error
temp->sub_start = NULL; // keep it simple
temp->prev = NULL;
temp->next = start;
if(start)
start->prev = temp;
start = temp;
return 0; // no error
}
void DestroyPieces(sub_Node* piece){
if( piece ) {
DestroyPieces( piece->next );
free( piece );
}
}
// as usual use int to return error code
int remove_player(char *name)
{
Node *player = find_player(name);
if ( !player ){
return 1; // player not found
}
if ( player->next ){
player->next->prev = player->prev;
}
if ( player->prev ){
player->prev->next = player->next;
}
DestroyPieces(player->sub_start);
free(player->name);
free(player);
return 0; // success
}
sub_Node* new_piece(int value){
sub_Node *temp = malloc( sizeof(sub_Node) );
if(temp){
temp->value = value;
temp->next = NULL;
}
return temp;
}
// int to return error code
// pass sub_start as pointer to pointer, as it might be updated
int add_descending(sub_Node** psub_start, int piece_value)
{
sub_Node *piece, *current, *prev = NULL;
if( !psub_start){
return 5; // this should not happen
}
current = *psub_start;
piece = new_piece( piece_value );
if( !piece ) return 1; // no mem
if(!current){
// this is the first and only one
*psub_start = piece;
return 0; // OK
}
while(current && current->value >= piece_value)
{
prev = current;
current = current->next;
}
if( prev )
prev->next = piece;
piece->next = current;
if( current == *psub_start ){
*psub_start = piece;
}
return 0 ; // OK
}
void add_piece(Node * player, int piece_value)
{
if ( !player) {
return ;
}
if(add_descending (&(player->sub_start), piece_value) == 0 )
return ; //OK
printf("an error occured while adding a piece (%d) to player '%s'\n",piece_value,player->name);
}
void print_pieces(Node *player)
{
sub_Node *q;
if( !player ){
return;
}
if( !player->sub_start ){
printf("Player '%s' has no pieces\n",player->name);
return;
}
printf("The values of the owned pieces are:");
for(q = player->sub_start; q != NULL; q = q->next)
printf(" %d", q->value);
printf("\n");
}
void lose_piece(Node *player)
{
if( !player ){
return;
}
sub_Node *q, *prev = NULL;
int aux;
if( !player->sub_start ){
printf("Player '%s' has no pieces\n",player->name);
return;
}
// i think you want drop the last one
for(q = player->sub_start; q->next != NULL ;prev = q, q = q->next) {
;
}
if(prev)
prev->next = NULL;
else
player->sub_start = NULL;
aux = q->value;
free(q);
printf("\nThe player loses the piece: %d\n", aux);
return;
}
void print_players()
{
Node *p;
if( !start ){
printf("there are no players, try to add some\n");
return;
}
printf("The players are: ");
for(p = start; p != NULL; p = p->next)
printf("%s ", p->name);
printf("\n");
}
void print_menu(void){
printf("Insert a digit to execute the desired task:\n"
"<0> end the program\n"
"<1> add a player, who doesn't own any piece yet\n"
"<2> remove a player and all his pieces\n"
"<3> print the name of all the players\n"
"<4> a player gets a piece\n"
"<5> a player loses the piece with the lowest value out of the ones that he has\n"
"<6> prints the pieces of a player in a descending order by value\n\n");
}
Node * get_player(char *name){
Node *player = find_player(name);
if(!player)
printf("Player '%s' do not exists\n",name);
return player;
}
int main()
{
// initialize(); no more needed
int y, value;
char name[20];
Node *player;
print_menu();
do
{
printf("digit: ");
scanf("%d", &y);
switch(y)
{
case 1:
printf("Insert the player's name: ");
scanf("%s", name);
add_player(name);
break;
case 2:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
if( player )
break;
case 3:
print_players();
break;
case 4:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
if( player ){
printf("Insert the value of the piece: ");
scanf("%d", &value);
add_piece(player, value);
}
break;
case 5:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
lose_piece(player);
break;
case 6:
printf("Insert the player's name: ");
scanf("%s", name);
player = get_player(name);
print_pieces(player);
}
} while(y != 0);
return 0;
}

Related

Why my front view of the Linked list is not printing

So i was trying this question in which I have written the below code:-
#include <bits/stdc++.h>
using namespace std;
struct Node
{
char data;
struct Node* next;
};
struct Node *reverseList(struct Node *head)
{
Node *current = head;
Node *prev = NULL, *next = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
bool isPalindrome(struct Node* head)
{
Node* frontHead = head;
Node* reverseHead = reverseList(head);
Node* temp = reverseHead;
cout<<"back view"<<endl;
while(temp)
{
cout<<temp->data<<"->";
temp = temp ->next;
}
cout<<endl;
cout<<"front view"<<endl;
temp = frontHead;
while(temp)
{
cout<<temp->data<<"->";
temp = temp ->next;
}
cout<<endl;
bool flag = true;
// while(frontHead && reverseHead)
// {
// // cout<<frontHead->data<<"-->"<<reverseHead->data<<endl;
// if(frontHead->data !=reverseHead->data)
// {
// flag=false;
// break;
// }
// frontHead = frontHead->next;
// reverseHead = reverseHead->next;
// }
return flag;
}
void push(struct Node** head_ref, char new_data)
{
struct Node* new_node = (struct Node*)malloc(
sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* ptr)
{
while (ptr != NULL)
{
cout << ptr->data << "->";
ptr = ptr->next;
}
cout << "NULL" << "\n";
}
// Driver code
int main()
{
struct Node* head = NULL;
char str[] = "abacba";
int i;
for (i = 0; i<strlen(str); i++)
push(&head, str[i]);
// printList(head);
isPalindrome(head) ? cout << "Is Palindrome" << "\n\n" : cout << "Not Palindrome" << "\n\n";
return 0;
}
When I try to print the node form the front as well as from the backward(after reversing the linked list I got this output:-
back view
a->b->a->c->b->a->
front view
a->
Is Palindrome
can Anyone tell me why i am not able to get the front view of the linked list .
Any help will be appreciated

insert in simple linked list doesn't get last line

I am trying to insert into a simply linked list from a file. Do you
guys have any idea why the function doesn't insert the last line in my
file?
This is the file:
10,Ghidul Ciberbobocului,Stan Daria,3,Popescu Matei,Ionescu Gigel,Ilinca Radu,100.5
2,Spring IT,Mirodene Cristina,4,Dumitru Mihai,Vasiliu Valentin,Balasa Silvia,Dumitru Ion,400.
89,Serile teatrului studentesc,Petre Ion,2,Nicolae Ramona,Stan Alberto,1000
1,Tutoring,Petre Miruna,2,Bode Cristina,Angelescu Paul,500.5
11,IT Fest,Ciurea Ion,2,Ionescu Georgiana,Neagu Bianca,100.6
My code:
struct Proiect {
int id;
char* numeProiect;
char* numeCoordonator;
unsigned int nrStudenti;
char** studenti;
float costInscriere;
};
struct nodLista {
Proiect proiect;
nodLista *next;
};
nodLista* inserareNod(nodLista *first, Proiect p) {
nodLista* newNode = new nodLista;
newNode->next = NULL;
newNode->proiect = p;
if (!first) {
return newNode;
}
nodLista* aux = first;
while (aux->next) {
aux = aux->next;
}
aux->next = newNode;
printf("%d\n", aux->proiect.id);
return first;
}
void main() {
nodLista* first = NULL;
Proiect proiect;
FILE *f = fopen("proiecte2.txt", "r");
char line[150];
int nrProiecte = 0;
if (f) {
while (fgets(line, sizeof(line), f)) {
nrProiecte++;
}
printf("Nr proiecte: %d",nrProiecte);
fclose(f);
}
else {
printf("Fisierul nu a fost gasit");
}
f = fopen("proiecte2.txt", "r");
char *token[150], sep_list[] = ",";
Proiect* listaProiecte;
listaProiecte = (Proiect*)malloc(nrProiecte * sizeof(Proiect));
int i = 0;
if (f) {
while(fgets(line, sizeof(line), f)) {
token[0] = strtok(line, sep_list);
listaProiecte[i].id = atoi(token[0]);
first = inserareNod(first, listaProiecte[i]);
i++;
}
}
else printf("Fisierul nu aputut fi deschis");
}
Output: It only displays 10 , 2, 89 and 1.
Did you make prints inside this function to check if it's doing the while correctly?
while(fgets(line, sizeof(line), f)) {
token[0] = strtok(line, sep_list);
listaProiecte[i].id = atoi(token[0]);
first = inserareNod(first, listaProiecte[i]);
i++;
}

Memory Leak in C and C++ Code

I am trying to return a pointer from a function and use the return in a different function but I am getting memory leak.
The test code which I wrote and detected with memory leak by CPPCheck.
########################################################################
# include < stdio.h >
# include < malloc.h >
# include < string.h >
char* replace ( char* st, char* word, char *replaceWith );
int main ( void )
{
char str[] = "Hello how are ## and what are ## doing ?";
char word[]="##";
char replaceWith[]="you";
printf("%s",replace(str,word,replaceWith));
getchar();
return 0;
}
char* replace(char* st,char* word,char *replaceWith)
{
int i = 0;
char *sr,*s,*ret;
int oldlen;
int count = 0;
int newlen;
int stlen;
s=(char *)malloc(strlen(st) + 1);
strcpy(s, st);
oldlen=strlen(word);
newlen=strlen(replaceWith);
for (i = 0; s[i]! = '\0'; )
{
if( memcmp( &s[i], word, oldlen ) == 0)
{
count++;
i+=oldlen;
}
else
{
i++;
}
}
sr= (char *) malloc (i+1+count*(newlen-oldlen));
ret = (char *) malloc (i+1+count*(newlen-oldlen));
ret=sr;
while(*s)
{
if(memcmp( s, word, oldlen) == 0)
{
memcpy(sr, replaceWith, newlen);
s+ = oldlen;
sr+ = newlen;
}
else
{
*sr++ = *s++;
}
}
*sr = '\0';
return ret;
}
Try this
#include<stdio.h>
#include<malloc.h>
#include<string.h>
char* replace ( char* st, char* word, char *replaceWith );
int main ( void )
{
char str[] = "Hello how are ## and what are ## doing ?";
char word[]="##";
char replaceWith[]="you";
char * ret = replace(str,word,replaceWith);
printf("%s",ret);
free(ret); //freeing the allocated memory
getchar();
return 0;
}
char* replace(char* st,char* word,char *replaceWith)
{
int i = 0;
char *sr,*s,*ret, *temps;
int oldlen;
int count = 0;
int newlen;
int stlen;
s=(char *)malloc(strlen(st) + 1);
temps = s; // storing the address of s in a temp location
strcpy(s, st);
oldlen=strlen(word);
newlen=strlen(replaceWith);
for (i = 0; s[i]!= '\0';)
{
if( memcmp( &s[i], word, oldlen ) == 0)
{
count++;
i+=oldlen;
}
else
{
i++;
}
}
sr= (char *) malloc (i+1+count*(newlen-oldlen));
ret=sr;
while(*s)
{
if(memcmp( s, word, oldlen) == 0)
{
memcpy(sr, replaceWith, newlen);
s += oldlen;
sr += newlen;
}
else
{
*sr++ = *s++;
}
}
*sr = '\0';
free(temps); // freeing the memory allocated for s
return ret;
}
Always free same count with malloc.
free s, sr at end of replace,
use return value of replace instead of direct use on printf
and free return value (return of ret from replace) when not needed.
I have doing lots of experimenting with the memory leak and meanwhile I wrote the following code. Please comment about the pros and cons side of it.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
// Prototype declaration of replaceAll function
static char* replaceAll(char *pSource, char *pWord, char*pWith);
/////////////////////////////////////////////////////////////////////////////
//
// NAME : main
//
// DESCRIPTION : Implementation of main which invokes the replaceAll
// function and displays the output
//
// PARAMETERS : void
//
// RETURNED VALUE : int
//
/////////////////////////////////////////////////////////////////////////////
int main( void )
{
char *finalString = NULL; // To save the base returned address
char srcString[] = "Hello how r you"; // Actual String
char pWord[] = "r"; // Word to be replaced
char pWith[] = "are"; // Word to be replaced with
printf("\n Before Calling the replaceAll function:");
printf("%s",srcString);
printf("\n");
finalString = replaceAll(srcString, pWord, pWith); //calling the replaceAll function
printf("\n After Calling the replaceAll function:");
// Checking if NULL is returned
if( finalString != NULL )
{
//printing the string
printf("%s", finalString);
}
else
{
printf("\n Error: Blank String returned ");
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
//
// NAME : replaceAll
//
// DESCRIPTION : Implementation of replaceAll function which replaces
// a word in given string with another word
//
// PARAMETERS : char *
//
// RETURNED VALUE : char *
//
/////////////////////////////////////////////////////////////////////////////
static char* replaceAll(char *pSource, char *pWord, char*pWith)
{
char *pSt = NULL; // Pointer to the source String to avoid modifying the pSource
char *pTarget = NULL; // Target pointer to be malloced
char *pTg = NULL; // Pointer to the target string
int count; // Counter
int nWord = strlen (pWord); // length of the word which needs to be replaced
int nWith = strlen (pWith); // length of the word with which the word needs to be replaced
static const char nullP = '\0'; // null character
int szTarget = 0;
// Assigning the base address of the pSource to a temporary and iterate through
for ( pSt = pSource, count = 0; *pSt != nullP; pSt++ )
{
// Count number of occurances of the Word in the String to calculate the length of the final string
if( memcmp( pSt, pWord, nWord ) == 0)
{
count++;
pSt += nWord-1;
}
}
// Calculate the required target Size
szTarget = strlen (pSource) + count * (nWith - nWord) + sizeof (nullP);
// Allocate memory for the target string
pTarget = (char *)malloc(szTarget);
// Check if the malloc function returns sucessfully
if ( pTarget != NULL)
{
// Copying the string with replacement
for (pTg = pTarget, pSt = pSource; *pSt != nullP; )
{
if( memcmp (pSt, pWord, nWord) == 0)
{
memcpy (pTg,pWith,nWith);
pSt += nWord;
pTg += nWith;
}
else
{
*pTg++ = *pSt++;
}
}
// Assigning NULL Character to the target string after copying
*pTg = '\0';
}
return pTarget;
}

Using fgets and strtok to read in data and create linked list

Need some help with reading in lines of data from a text file using the fgets and string tokenization commands, which will then be used to create a linked list. I've followed some examples I've found on Stack Overflow and other tutorial websites, but still cannot get the read function below to work properly in my program, it just causes it to crash. The data file has lines like this:
Zucchini, Squash, pound, 2.19, 45
Yellow, Squash, pound, 1.79, 15
Based on everything I've read, I believe I have the necessary code, but obviously I'm missing something. Also, I commented out one of the fields (the one for float price) as I'm not sure what to use to copy the float value from the data, as I cannot treat it as a string (the integer value right below it seems to let me get away with it in my compiler).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Struct for linked list node
struct produceItem
{
char produce[20];
char type[20];
char soldBy[20];
float price;
int quantityInStock;
struct produceItem *next;
};
// Function to read in data from file to
void read(struct produceItem **head)
{
struct produceItem *temp = NULL;
struct produceItem *right = NULL;
//char ch[3];
char line[50];
char *value;
FILE *data = fopen("RecitationFiveInput.txt", "r");
printf("Trying to open file RecitationFiveInput.txt\n");
if (data == NULL)
{
printf("Could not open file RecitationFiveInput.txt\n");
}
else
{
while(fgets(line, sizeof(line), data))
{
value = strtok(line, ", ");
strcpy(temp->produce, strdup(value));
value = strtok(NULL, ", ");
strcpy(temp->type, strdup(value));
value = strtok(NULL, ", ");
strcpy(temp->soldBy, strdup(value));
//value = strtok(NULL, ", ");
//strcpy(temp->price, strdup(value));
value = strtok(NULL, " \n");
strcpy(temp->quantityInStock, strdup(value));
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
right = *head;
while(right->next != NULL)
{
right = right->next;
}
right->next = temp;
}
}
printf("Successfully opened file RecitationFiveInput.txt\n");
}
fclose(data);
return;
}
// Function to display the nodes of the linked list that contains the data from the data file
void display(struct produceItem *head)
{
int value = 1;
struct produceItem *temp = NULL;
temp = head;
printf("=============================================================================\n");
printf(" Item # Produce Type Sold By Price In Stock\n");
printf("=============================================================================\n");
if(temp == NULL)
{
return;
}
else
{
while(temp != NULL)
{
printf(" %d %s %s %s %lf %d\n", value, temp->produce, temp->type, temp->soldBy, temp->price, temp->quantityInStock);
value++;
temp = temp->next;
if(temp == NULL)
{
break;
}
}
}
return;
}
//Main function
int main()
{
int input = 0;
struct produceItem *head = NULL;
while(1)
{
printf("\nList Operations\n");
printf("=================\n");
printf("1. Stock Produce Department\n");
printf("2. Display Produce Inventory\n");
printf("3. Reverse Order of Produce Inventory\n");
printf("4. Export Produce Inventory\n");
printf("5. Exit Program\n");
printf("Enter your choice: ");
if(scanf("%d", &input) <= 0)
{
printf("Enter only an integer.\n");
exit(0);
}
else
{
switch(input)
{
case 1:
read(&head);
break;
case 2:
display(head);
break;
case 3:
//function
break;
case 4:
//function
break;
case 5:
printf("You have exited the program, Goodbye!\n");
return 0;
break;
default:
printf("Invalid option.\n");
}
}
}
return 0;
}
Never mind everyone, found the issue. The crashes were due to me not allocating memory for the temp pointer in the read me function.

Polynomial multiplication using linked list in c

Iam working on a program to perform addition,subtraction,multiplication and differentiation operations on a polynomial using linked list in c.
The other operations are working fine except multiplication one.
here is the code
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n\nenter coeff:");
scanf("%d",&node->coeff);
printf("\nenter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\ncontinue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf(" + ");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
void polysub(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff-poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
void polymul(struct link *n1, struct link *n2, struct link *n)
{
struct link * n2beg=n2;
while (n1)
{
struct link * temp=(struct link *)malloc(sizeof(struct link));
temp->next=NULL;
n2=n2beg;
while (n2)
{
temp->coeff = n1->coeff * n2->coeff;
temp->pow = n1->pow + n2->pow;
n2 = n2->next;
temp->next=(struct link *)malloc(sizeof(struct link));
temp=temp->next;
temp->next=NULL;
}
polyadd(temp,n,n);
n1 = n1->next;
free(temp);
}
}
void diff(struct link* p1,struct link* p2)
{
while(p1->next!=NULL)
{
p2->coeff=p1->coeff*p1->pow;
p2->pow=p1->pow-1;
p2->next=NULL;
p2->next=(struct link *)malloc(sizeof(struct link));
p2=p2->next;
p2->next=NULL;
p1=p1->next;
}
}
main()
{
int op;
char ch;
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\n\nWhat do you want to do?\n1.Addition\n2.Subtraction
\n3.Multiplication\n4.Differentiation\n0.Exit
\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n\nenter 1st polynomial:");
create(poly1);
printf("\n\nenter 2nd polynomial:");
create(poly2);
printf("\n1st Polynomial:\t");
show(poly1);
printf("\n2nd Polynomial:\t");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:\t");
show(poly);
break;
case 2:
printf("\n\nenter 1st polynomial:\t");
create(poly1);
printf("\n\nenter 2nd polynomial:\t");
create(poly2);
printf("\n\n1st Polynomial:\t");
show(poly1);
printf("\n\n2nd Polynomial:\t");
show(poly2);
polysub(poly1,poly2,poly);
printf("\n\nSubtracted polynomial:\t");
show(poly);
break;
case 3:
printf("\n\nenter 1st polynomial:");
create(poly1);
printf("\n\nenter 2nd polynomial:");
create(poly2);
printf("\n\n1st Polynomial:\t");
show(poly1);
printf("\n\n2nd Polynomial:\t");
show(poly2);
polymul(poly1,poly2,poly);
printf("\n\nMultiplied polynomial:\t");
show(poly);
break;
case 4:
printf("\n\nenter polynomial:");
create(poly1);
printf("\n\nPolynomial:\t");
show(poly1);
diff(poly1,poly2);
printf("\n\nDifferentiated Polynomial:\t");
show(poly2);
break;
}
/* printf("\n Want to continue? Y/N:");
ch=getch();*/
}
while(op);
}
polyadd(temp,n,n) as used in polymul() has trouble coping with using n as a source polynomial and as the sum destination polynomial.
Either re-work polyadd() to cope with parameters that point to the same polynomial or re-work the call of polyadd() in polymul() to use distinct polynomials. Suggest the first.
I did not go through your full code, as I found an error in your create() function. Note that you have passed poly1 to the function create() as argument.
This not correct as C follows call by value. What happens is only the value of poly1 (which is still un-initialised) is passed and *node stores that value. It's better to pass the address of poly1 as argument and catch that value in the function using pointer to pointer.
#include<stdio.h>
#include <conio.h>
struct node
{
int c,e;
struct node *link;
}*start1=NULL,*start2=NULL,*start3=NULL,*temp1,*temp2,*temp3,*new_node;
int delete_dup(int h)
{
struct node *cr,*prev,*run,*tmp;
cr = start3->link;
prev = start3;
while(cr != NULL){
run = start3;
while(run != cr)
{
if(run->e == cr->e)
{
run->c+=cr->c;
tmp = cr;
cr = cr->link;
prev->link = cr;
remove(tmp);h--;
break;
}
run = run->link;
}
if(run == cr){
cr = cr->link;
prev = prev->link;
}
}
return h;
}
void main()
{
int n,m,i,j,k;
puts("Enter the number of terms in first polynomial");
scanf("%d",&n);
for(i=0;i<n;i++)
{
new_node=(struct node*)malloc(sizeof(struct node));
new_node->link=NULL;
puts("C=");
scanf("%d",&new_node->c);
puts("E=");
scanf("%d",&new_node->e);
new_node->link=start1;
start1=new_node;
}
puts("Enter the number of terms in first polynomial");
scanf("%d",&m);
for(i=0;i<m;i++)
{
new_node=(struct node*)malloc(sizeof(struct node));
new_node->link=NULL;
puts("C=");
scanf("%d",&new_node->c);
puts("E=");
scanf("%d",&new_node->e);
new_node->link=start2;
start2=new_node;
}
temp1=start1;
temp2=start2;
i=0; j=0;
while(i<m)
{
j=0; temp1=start1;
while(j<n)
{
new_node=(struct node*)malloc(sizeof(struct node));
new_node->link=NULL;
new_node->c=temp1->c*temp2->c;
new_node->e=temp1->e+temp2->e;
new_node->link=start3;
start3=new_node;
j++;
temp1=temp1->link;
}
temp2=temp2->link;
i++;
}
i=0;
k=delete_dup(m*n);
temp3=start3;
while(i<k-1)
{
printf("(%dx^%d)+",temp3->c,temp3->e);
temp3=temp3->link;
i++;
}
printf("(%dx^%d)",temp3->c,temp3->e);
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
int coef,pow;
struct node *next;
};
struct node* create()
{
int c;
struct node *new_node=NULL;
struct node *head=NULL,*temp;
do{
new_node=(struct node*)malloc(sizeof(struct node));
printf("\nEnter coef :");
scanf("%d",&new_node->coef);
printf("\nEnter power : ");
scanf("%d",&new_node->pow);
if(head==NULL)
{
head=new_node;
head->next=NULL;
temp=new_node;
}
else
{
temp->next=new_node;
temp=temp->next;
temp->next=NULL;
}
printf("\nDo you want to continue :");
scanf("%d",&c);
}while(c==1);
return head;
}
void print(struct node *p)
{
while(p!=NULL)
{
printf("|%d|%d|->",p->coef,p->pow);
p=p->next;
}
}
struct node* create_new(int p, int q)
{
struct node *nn=(struct node*)malloc(sizeof(struct node));
nn->coef=p;
nn->pow=q;
nn->next=NULL;
return nn;
}
struct node* add(struct node *poly1,struct node *poly2)
{
struct node *tempadd,*temp;
struct node *temp1=poly1;
struct node *temp2=poly2,*head=NULL;
while(temp1!=NULL && temp2!=NULL)
{
if(temp1->pow==temp2->pow)
{
tempadd=create_new(temp1->coef+temp2->coef,temp2->pow);
temp1=temp1->next;
temp2=temp2->next;
}
else if(temp1->pow<temp2->pow)
{
tempadd=create_new(temp2->coef,temp2->pow);
temp2=temp2->next;
}
else
{
tempadd=create_new(temp1->coef,temp1->pow);
temp1=temp1->next;
}
if(head==NULL)
{
head=tempadd;
temp=head;
}
else
{
temp->next=tempadd;
temp=temp->next;
}
}
if(temp1!=NULL)
{
while(temp1!=NULL)
{
tempadd=create_new(temp1->coef,temp1->pow);
temp1=temp1->next;
temp->next=tempadd;
temp=temp->next;
}
}
else if(temp2!=NULL)
{
while(temp2!=NULL)
{
tempadd=create_new(temp2->coef,temp2->pow);
temp2=temp2->next;
temp->next=tempadd;
temp=temp->next;
}
}
return head;
}
void main()
{
struct node *head1,*head2,*head3;
head1=create();
print(head1);
head2=create();
print(head1);
printf("\n");
print(head2);
printf("\n");
head3=add(head1,head2);
printf("\n\nResult : ");
print(head3);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node {
struct node *previous;
int coef;
int pow;
struct node *next;
}poly;
poly *locate(int,poly *);
void display(poly *);
poly *mult(poly *,poly *,poly *);
void display2(poly *);
void main()
{
char ch;
poly *s1,*s2,*s3,*head1,*head2,*head3,*s4;;
s1=(poly *)malloc(sizeof(poly));
s1->previous=NULL;
head1=s1;
s2=(poly *)malloc(sizeof(poly));
s2->previous=NULL;
head2=s2;
head3=s3;
printf("Enter first polynomial :\n");
do{ //Input for polynomial-1
printf("Enter co-efficient : ");
scanf("%d",&s1->coef);
printf("Enter exponent : ");
scanf("%d",&s1->pow);
printf("Do you want to enter more terms(Y/N) : ");
scanf(" %c",&ch);
if(ch=='Y'){
s1->next=(poly*)malloc(sizeof(poly));
s1->next->previous=s1;
s1=s1->next;
}
else {
s1->next=NULL;
break;
}
}while(1);
printf("Enter second polynomial : \n");
do{ //input for polynomial-2
printf("Enter co-efficient : ");
scanf("%d",&s2->coef);
printf("Enter exponent : ");
scanf("%d",&s2->pow);
printf("Do you want to enter more terms(Y/N) : ");
scanf(" %c",&ch);
if(ch=='Y'){
s2->next=(poly*)malloc(sizeof(poly));
s2->next->previous=s2;
s2=s2->next;
}
else {
s2->next=NULL;
break;
}
}while(1);
printf("Entered polynomials are : \n");
display(s1);
printf("\n");
display(s2);
s3=NULL;
s4=mult(s1,s2,s3);
printf("Resultant Polynomial after multiplication :\n");
display(s4);
}
void display(poly *a)
{
while(a!=NULL)
{
printf("%dx^%d",a->coef,a->pow);
if(a->previous!=NULL)
printf(" + ");
a=a->previous;
}
}
poly *mult(poly *s1,poly *s2,poly *s3)
{
while(s1->previous!=NULL)
s1=s1->previous;
while(s2->previous!=NULL)
s2=s2->previous;
while(s1)
{
while(s2->previous!=NULL)
s2=s2->previous;
while(1)
{
if(s2->next!=NULL)
{
poly *s4;
if(s3==NULL)
{
s3=(poly *)malloc(sizeof(poly));
s3->pow=s1->pow+s2->pow;
s3->coef=s1->coef*s2->coef;
s3->previous==NULL;
s3->next==NULL;
}
else
{
s4=locate(s1->pow+s2->pow,s3);
if(s4==NULL)
{
s3->next=(poly *)malloc(sizeof(poly));
s3->next->previous=s3;
s3=s3->next;
s3->pow=s1->pow+s2->pow;
s3->coef=s1->coef*s2->coef;
s3->next==NULL;
}
else
{
s4->coef=(s4->coef)+(s1->coef*s2->coef);
}
}
s2=s2->next;
}
else
{
poly *s4;
if(s3==NULL)
{
s3=(poly *)malloc(sizeof(poly));
s3->pow=s1->pow+s2->pow;
s3->coef=s1->coef*s2->coef;
s3->previous==NULL;
s3->next==NULL;
}
else{
s4=locate(s1->pow+s2->pow,s3);
if(s4==NULL)
{
s3->next=(poly *)malloc(sizeof(poly));
s3->next->previous=s3;
s3=s3->next;
s3->pow=s1->pow+s2->pow;
s3->coef=s1->coef*s2->coef;
s3->next==NULL;
}
else
{
s4->coef=s4->coef+s1->coef*s2->coef;
}
}
break;
};
}s1=s1->next;
}return s3;
}
poly *locate(int exp,poly *s3)
{
if(s3==NULL)
{
return NULL;
}
else if(s3->pow==exp)
{
return s3;
}
else{
return locate(exp,s3->previous);
}
}
I considered the first term of first polynomial and multiplied with all the terms of second polynomial thereby creating a primary linked list of multiplied polynomial. As next piece of code I considered next term of first polynomial and then multiplied and searched in the multiplied polynomial for the same index and added the result to it, If it is not present I created a new node in the multiplied polynomial.
Happy Coding

Resources