expected ')' before '*' token passing in Node - c

I've searched this question and the previous ones do not seem to be related to my problem. I seem to have a disconnect on passing a node into a function, for some reason I keep getting this error when attempting to compile.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char c;
int titleCount;
int authorCount;
char bookTitle[35];
char author[35];
/* Create Struct */
typedef struct bookData {
char bookTitle[35];
char author[35];
int book_stock;
float retail_price;
int wholesale_purchased;
int customer_purchased;
struct bookData *book;
} new_book;
/*Create Node */
typedef struct Node {
new_book books;
struct Node *next;
} node_t;
/* We are GUARANTEED at least 1 input value, so go ahead and initialize the head */
node_t *head = NULL; /*Initalize head to NULL since it is empty */
head = malloc(sizeof(node_t));
if(head == NULL) {
printf("allocation failed");
return 0;
}
head -> next = NULL;
/*Memory allocation successful */
/*Might as well populate the head with data from the user */
titleCount = 0;
authorCount = 0;
printf("Enter Title\n");
while(( c = getchar()) != '\n') {
bookTitle[titleCount++] = c;
}
bookTitle[titleCount] = '\0';
printf("%s\n", bookTitle);
strcpy(head -> books.bookTitle, bookTitle);
printf("Enter Author\n");
while(( c = getchar()) != '\n') {
author[authorCount++] = c;
}
author[authorCount] = '\0';
strcpy(head -> books.author, author);
printf("Bookstock #:\n");
scanf("%d", &(head -> books).book_stock);
printf("Enter retail price $:\n");
scanf("%f", &(head -> books).retail_price);
printf("Enter Wholesale purchased quanity:\n");
scanf("%d", &(head -> books).wholesale_purchased);
printf("Enter quantity sold:\n");
scanf("%d", &(head -> books).customer_purchased);
printf("%s\n", head -> books.bookTitle);
printf("%s\n", head -> books.author);
printf("%d\n", head -> books.book_stock);
printf("%.2f\n", head -> books.retail_price);
printf("%d\n", head -> books.wholesale_purchased);
printf("%d\n", head -> books.customer_purchased);
takeUserInput(head);
}
/*Now populate all other nodes, until user enters END_DATA */
void takeUserInput(node **head) {
int titleCount;
int authorCount;
char bookTitle[35];
char author[35];
int flag = 0;
while(1) {
titleCount = 0;
authorCount = 0;
node_t *temp = NULL;
temp = malloc(sizeof(node_t));
temp -> next = NULL;
printf("Enter Title\n");
while(( c = getchar()) != '\n') {
bookTitle[titleCount++] = c;
}
bookTitle[titleCount] = '\0'
if(bookTitle == "END_DATA") {
flag = 1;
break;
}
strcpy(temp -> books.bookTitle, bookTitle);
printf("Enter Author\n");
while(( c = getchar()) != '\n') {
author[authorCount++] = c;
}
author[authorCount++] = '\0';
strcpy(temp -> books.author, author);
printf("Bookstock #\n");
scanf("%d", &(temp - > books).book_stock);
printf("Enter retail Price in $\n:");
scanf("%.2f", &(temp -> books).retail_price);
printf("Enter wholesale purchased\n:");
scanf("%d", &(temp -> books).wholesale_purchased);
printf("Enter customer purchased\n:");
scanf("%d", &(temp -> books).customer_purchased);
if(temp -> books.book_stock < head -> books.book_stock) {
void pushToFront(head, temp);
} else {
void insert(head,temp);
}
}
if(flag == 1) {
free(temp);
}
}
void pushToFront(node_t **head, node_t *temp) {
temp -> next = *head;
*head -> temp;
}
void insert(node_t *head, node_t *temp) {
node_t *current = head;
while(current -> next -> new_book.book_stock < temp -> new_book.book_stock || current -> != NULL) {
current = current -> next;
}
if(current -> next = NULL) {
current -> next = temp;
}
node_t *xtraNode = current -> next
current -> next = temp;
temp -> next = xtraNode;
}
Error:
lab3p1.c:114: error: expected ‘)’ before ‘*’ token
lab3p1.c:180: error: expected ‘)’ before ‘*’ token
lab3p1.c:188: error: expected ‘)’ before ‘*’ token
To my understanding, I only need to pass in a double pointer when I'm adjusting which node the head points to pushToFront. So on insert(), I'm not actually adjusting the head pointer, just the next pointer of a particular node and the node being inserted. So I'm a little confused as to why this error is occurring.

I have spent a bit time to try compile your code but the number of errors is astonishing. Your code requires your serious attention.
Start with the simple things first.
First move the declaration of the structures before main:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Create Struct */
typedef struct bookData{
char bookTitle[35];
char author[35];
int book_stock;
float retail_price;
int wholesale_purchased;
int customer_purchased;
struct bookData *book;
}new_book;
/*Create Node */
typedef struct Node{
new_book books;
struct Node *next;
}node_t;
void takeUserInput(node_t **head);
void pushToFront(node_t **head, node_t *temp);
void insert(node_t *head, node_t *temp);
int main(){
//...
}
and put your function prototypes before main as well.
void takeUserInput(node **head){
should be:
void takeUserInput(node_t **head){
and
takeUserInput(head);
should be:
takeUserInput(&head);
and
void takeUserInput(node_t **head){ // node_t not node sg7
int titleCount;
int authorCount;
char bookTitle[35];
char author[35];
int c; // declaration was missing sg7!
later
bookTitle[titleCount] = '\0' // missing ;
if(bookTitle == "END_DATA"){ // this is NOT the way to compare strings
I also noticed that you have not accepted a single answer, yet.
I have stopped here and I let you continue yourself. Good luck. Everyone has to carry his own cross.

Related

Linked List Input and Output

I want to create a program in C that differentiates between a number or a character and put them into a linked list, then output the list. The issue is that when I input anything into it, it only outputs 10. In the results down below 1+2+3, I need it to separate the char and the nums. Help will be greatly appreciated
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// define a union to overlay an integer with a char
union DataType{
int operand;
char operate;
};
// typedef to rename DataType
typedef union DataType Data;
// create a structure for a node
typedef struct NodeType{
Data data;
struct NodeType* next;
}Node;
Node* top = NULL;
// typedef to rename NodeType
void displayNode();
void addNode() {
Node * newNode = malloc(sizeof(Node));
char expression[64];
// get the stuff
printf("Enter expression: ");
fgets(expression, 64, stdin);
//char array , int size, stdin
for (int i = 0; i < strlen(expression); i++) { //checks individual character
if (isdigit((expression[i]))){ // if digit add it to operate
(*newNode).data.operate = (expression[i]); // how
}else //else add it to operand
newNode->data.operand = (expression[i]); //how
}
if (top == NULL)
top = newNode; // update headPtr in main
else {
Node *currPtr = top;
// find last Node
while (currPtr->next != NULL)
currPtr = currPtr->next;
// insert new Node
currPtr->next = newNode;
}
}
void popNode()
{
if(top != NULL)
{
Node * temp = top;
top = top->next;
free(temp);
}
}
void displayNode()
{
if(top == NULL)
{
printf("Error");
return;
}
Node * curr = top;
// while(curr -> next != NULL){
printf("\n+---------------------------------+");
printf("\nOperator stack: ");
printf("%c ", curr->data.operate);
printf("\nOperand stack: ");
printf("%d ", curr->data.operand);
printf("\n+---------------------------------+");
curr = curr->next;
// }
}
int main()
{
int menu = 1;
do {
//call an array in main
addNode(&top);
displayNode(&top);
char cont;
printf("\nDo you want to enter another expression (Y/N)?");
scanf(" %c", &cont);
if (cont == 'n'|| cont == 'N') { // if not close menu
menu = 0;
}
}while(menu == 1);
return 0;
}
Enter expression:1+2+3
+---------------------------------+
Operator stack:
Operand stack: 10
+---------------------------------+
Do you want to enter another expression (Y/N)?
Testing out your code quickly indicated that the addNode function was only creating one initial node. That is why you only see one operator stack output. With that I moved the node creation line of code along with the node linkage loop inside the for loop.
Following is a revised version of your code with the noted changes in the addNode function.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// define a union to overlay an integer with a char
union DataType
{
int operand;
char operate;
};
// typedef to rename DataType
typedef union DataType Data;
// create a structure for a node
typedef struct NodeType
{
Data data;
struct NodeType* next;
} Node;
Node* top = NULL;
// typedef to rename NodeType
void displayNode();
void addNode()
{
char expression[64];
// get the stuff
printf("Enter expression: ");
fgets(expression, 64, stdin);
//char array , int size, stdin
for (int i = 0; i < strlen(expression); i++) //checks individual character
{
Node * newNode = malloc(sizeof(Node)); /* Moved this line of code here to build multiple nodes */
if (isdigit((expression[i]))) // if digit add it to operate
{
(*newNode).data.operate = (expression[i]); // how
}
else //else add it to operand
newNode->data.operand = (expression[i]); //how
/* Moved this block of code inside the for loop to create the linked list */
if (top == NULL)
top = newNode; // update headPtr in main
else
{
Node *currPtr = top;
// find last Node
while (currPtr->next != NULL)
currPtr = currPtr->next;
// insert new Node
currPtr->next = newNode;
}
}
}
void popNode()
{
if(top != NULL)
{
Node * temp = top;
top = top->next;
free(temp);
}
}
void displayNode()
{
if(top == NULL)
{
printf("Error");
return;
}
Node * curr = top;
while(curr -> next != NULL){
printf("\n+---------------------------------+");
printf("\nOperator stack: ");
printf("%c ", curr->data.operate);
printf("\nOperand stack: ");
printf("%d ", curr->data.operand);
printf("\n+---------------------------------+");
curr = curr->next;
}
}
int main()
{
int menu = 1;
do
{
//call an array in main
addNode();
displayNode();
char cont;
printf("\nDo you want to enter another expression (Y/N)?");
scanf(" %c", &cont);
if (cont == 'n'|| cont == 'N') // if not close menu
{
menu = 0;
}
}
while(menu == 1);
return 0;
}
Following was the output at the terminal utilizing your test expression.
#Dev:~/C_Programs/Console/Diff/bin/Release$ ./Diff
Enter expression: 1+2+3
+---------------------------------+
Operator stack: 1
Operand stack: 49
+---------------------------------+
+---------------------------------+
Operator stack: +
Operand stack: 43
+---------------------------------+
+---------------------------------+
Operator stack: 2
Operand stack: 50
+---------------------------------+
+---------------------------------+
Operator stack: +
Operand stack: 43
+---------------------------------+
+---------------------------------+
Operator stack: 3
Operand stack: 51
+---------------------------------+
Do you want to enter another expression (Y/N)?
I suspect that there is some more refinement that you would probably do but this gets your code to the point of creating multiple nodes and linking them. Give that a try and see if it meets the spirit of your project.

C- Linked List with *chars

I am trying to build a simple linked list.
I have successfully built a linked list only with int variables but when I add *char variables the output is wrong.
The int values seem to be correct but the *char type are wrong.
The *char type seem to always be the last one inserted.
Sample Input
Number: 5
Character: a
Number: 4
Character: b
Sample Output
5
b
**************
4
b
#include <stdio.h>
#include <stdlib.h>
typedef struct BOOKS
{
int value;
char *char_value;
struct BOOKS *next;
} BOOKS;
BOOKS *insert_value(BOOKS *node, int n, char *char_value);
BOOKS *read_list(BOOKS *head);
int main()
{
int aux = 0;
int menu = 0;
int option;
BOOKS *head = NULL;
BOOKS *tail = NULL;
while (menu != -2)
{
int choices;
printf("1. Insert Book\n");
printf("2. Print Books\n");
printf("3. Exit\n");
scanf("%d", &choices);
switch (choices)
{
case 1:
{
int n;
char char_value[2000] = "";
printf("Number:");
scanf("%d", &n);
printf("Character: ");
scanf("%s", &char_value);
if (aux == 0)
{
/*FIRST INTERACTION*/
head = malloc(sizeof(BOOKS));
tail = malloc(sizeof(BOOKS));
head = tail = insert_value(tail, n, char_value);
aux = 1;
}
else
{
tail->next = malloc(sizeof(BOOKS));
/*FORMING TAIL->NEXT*/
tail->next = insert_value(tail->next, n, char_value);
/*ASSIGNING TAIL->NEXT AS THE NEW TAIL */
tail = tail->next;
}
break;
}
case 2:
{ /*READ THE LIST*/
read_list(head);
break;
}
case 3:
{
menu = -2;
break;
}
default:
printf("Invalid choice\n");
break;
}
}
}
BOOKS *insert_value(BOOKS *node, int n, char *char_value)
{
node->value = n;
node->char_value = char_value;
node->next = NULL;
return node;
}
BOOKS *read_list(BOOKS *head)
{
BOOKS *a = head;
while (a != NULL)
{
printf("%d\n", a->value);
printf("%s\n", a->char_value);
printf("\n********************\n");
a = a->next;
}
}
You're passing char_value from main to your insert_books function, and then saving this pointer in your newly created node. The address of char_value from main doesn't change, so you're saving the same address to each node you create. Since all these pointers point to the same place, they will all read back whatever value was written there last. If you want to make a copy of string in each node, you can use (non standard) strdup or malloc more memory and use strcpy. But, it's an odd design choice you're using a character array to accept one character anyway. I recommend changing char_value in both main and struct BOOKS to char char_value;, then you can simply use equals assignment as with value to save copies of the char. Be sure to scanf a char the correct way.
A couple of other things:
As written, you can change scanf("%s", &char_value); to scanf("%s", char_value);. char_value is a character array, which decays to a char* in this context, there's no need to specify the address of char_value.
head = malloc(sizeof(BOOKS));
tail = malloc(sizeof(BOOKS));
head = tail = insert_value(tail, n, char_value);
This is a memory leak. You're allocating separate memory for head and tail, but then setting both pointers equal to the memory allocated for tail (the return value of insert_value). The memory allocated for head is never freed nor used.
This will help with the errors:-
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
typedef struct BOOKS
{
int value;
char char_value[2000];
struct BOOKS *next;
} BOOKS;
BOOKS *insert_value(BOOKS *node, int n, char *char_value);
BOOKS *read_list(BOOKS *head);
int main()
{
int aux = 0;
int menu = 0;
int option;
BOOKS *head = NULL;
BOOKS *tail = NULL;
while (menu != -2)
{
int choices;
printf("1. Insert Book\n");
printf("2. Print Books\n");
printf("3. Exit\n");
scanf("%d", &choices);
switch (choices)
{
case 1:
{
int n;
char char_value[2000];
printf("Number:");
scanf("%d", &n);
printf("Character: ");
scanf("%s", char_value);
if (aux == 0)
{
/*FIRST INTERACTION*/
head = malloc(sizeof(BOOKS));
tail = malloc(sizeof(BOOKS));
head = tail = insert_value(tail, n, char_value);
aux = 1;
}
else
{
tail->next = malloc(sizeof(BOOKS));
/*FORMING TAIL->NEXT*/
tail->next = insert_value(tail->next, n, char_value);
/*ASSIGNING TAIL->NEXT AS THE NEW TAIL */
tail = tail->next;
}
break;
}
case 2:
{ /*READ THE LIST*/
read_list(head);
break;
}
case 3:
{
menu = -2;
break;
}
default:
printf("Invalid choice\n");
break;
}
}
}
BOOKS *insert_value(BOOKS *node, int n, char *char_value)
{
node->value = n;
int size = sizeof(char_value)/sizeof(char);
strncpy(node->char_value, char_value, size);
node->next = NULL;
return node;
}
BOOKS *read_list(BOOKS *head)
{
BOOKS *a = head;
while (a != NULL)
{
printf("%d\n", a->value);
printf("%s\n", a->char_value);
printf("\n********************\n");
a = a->next;
}
}

wrong output when code runs problem with linked list?

Upon running the code below i get the output
NAME: (null) | GPA: 0.000000 | YEAR: (NULL)
are the linked lists not implemented correctly? I am currently using a makefile and bringing in a test.data file with names and gpa and senior/ect..
Ollie 2.9 freshmen
John 3.2 senior
Julie 2.2 freshmen
Joe 1.8 freshmen
Mary 3.8 senior
Sue 3.4 junior
Jane 2.7 senior
Bob 2.8 senior
Fred 3.2 freshmen
Bill 3.3 junior
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "students.h"
Student *top = NULL;
Student *temp, *temp1, *temp2;
// Creates the entire linked list from the file.
// Should call readNext and push
// Returns head of the linked list
Student *buildStudentList()
{
Student *p;
p = readNext();
push(&top, p);
return top; //TODO: Change return
}
//Read a single line from standard input and return a student structure located on the heap
Student *readNext()
{
Student *s =(Student*) malloc(sizeof(Student));
scanf("%s", s -> name);
scanf("%f", &s -> gpa);
scanf("%s", s -> year);
s->next = NULL;
return s; //TODO: Change return
}
//Return a student structure stored on the heap
Student *makeStudent(char *name, float gpa, char *year)
{
Student *s =(Student*) malloc(sizeof(Student));
s -> name = name;
s -> gpa = gpa;
s -> year = year;
s -> next = NULL;
return s; //TODO: Change return
}
//insert a new student node at the head of the linked list
void push(Student **list, Student *student)
{
top = *list;
student -> next = top;
top = student;
}
//Insert a student node in the desired position on the linked list
void insert(Student *list, Student *s, int position)
{
int i;
top = list;
temp = top;
for(i = 1; i < position -1; i++)
{
temp = temp -> next;
}
if(temp == NULL)
{
//blank
}
else
{
s -> next = temp -> next;
temp -> next = s;
}
}
//Displays contents of a single student structure
void display(Student *s){
printf("NAME:%s | GPA: %f | YEAR:%s
", s -> name, s-> gpa, s -> year);
}
//Displays contents of the entire linked list
void displayAll(Student *list)
{
temp = list;
while(temp != NULL)
{
display(temp);
temp = temp -> next;
}
}
//Delete all data allocated on the heap before terminating program
void cleanUp(Student *list)
{
temp1 = list;
temp2 = temp1 -> next;
while(temp1 != NULL)
{
free(temp1);
temp1 = temp2;
}
if(temp2 != NULL)
{
temp2 = temp2 -> next;
}
}
//Main function tests your functions.
int main()
{
printf("Program Started
");
//Construct Linked List from Standard Input
Student *list = buildStudentList();
//Insert a new student in desired position
Student *s = makeStudent("Max",3.0, "senior");
insert(list, s, 3);
//Display entire linked list
displayAll(list);
//Free all heap memory
cleanUp(list);
printf("Program Successful Exit
");
exit(EXIT_SUCCESS);
}
Since you didn't post your struct definition, I had to guess whether (e.g.) name was char *name; or (e.g. char name[100];). Within the code, it used it as a pointer.
So ...
Your readNext and makeStudent don't allocate space for the strings (char * pointers) name and year, so they're probably segfaulting.
insert takes Student *list when it really needs Student **list.
IMO, you should have a separate List type to avoid confusion (that has a single element: Student *head;). So, wherever you have Student *list, you replace it with List *list
When you do that, you don't have to pass down Student ** [a double star] pointer when you mean a list. Using list->head is a lot easier and more descriptive than *list.
Also, be consistent. Some functions take Student **list if they modify the list [they have to]. Others use Student *list, but they should be consistent as well.
No need for the various global scope temp variables. These should be function scoped and use more descriptive names.
Your insert has issues. It will orphan the node it's trying to insert if no position match is found (e.g. insert at position 99 in your example). Usual is to insert at tail or return an error code. Also, it wasn't totally clear what position meant [to me], because of the code you had. It could have been "insert before" or "insert after" the Nth node.
You can't insert a literal newline in a double quoted string. So, use the \n escape sequence (e.g.) printf("hello world\n");
Also, functions that take no arguments should use void (e.g.) instead of int main(), use int main(void).
Here's a cleaned up version of your code, incorporating what I've mentioned above:
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
//#include "students.h"
typedef struct student Student;
struct student {
Student *next;
float gpa;
char *name;
char *year;
};
typedef struct list {
Student *head;
} List;
//insert a new student node at the head of the linked list
void
push(List *list, Student *student)
{
student->next = list->head;
list->head = student;
}
//Return a student structure stored on the heap
Student *
makeStudent(char *name, float gpa, char *year)
{
Student *s = (Student *) malloc(sizeof(Student));
s->name = strdup(name);
s->gpa = gpa;
s->year = strdup(year);
s->next = NULL;
return s;
}
//Read a single line from standard input and return a student structure located on the heap
Student *
readNext(void)
{
char name[1000];
float gpa;
char year[1000];
Student *s = NULL;
int count = scanf("%s %f %s",name,&gpa,year);
if (count == 3) {
printf("readNext: name='%s' gpa=%g year='%s'\n",name,gpa,year);
s = makeStudent(name,gpa,year);
}
return s;
}
// Creates the entire linked list from the file.
// Should call readNext and push
// Returns head of the linked list
List *
buildStudentList(List *list)
{
Student *p;
while (1) {
p = readNext();
if (p == NULL)
break;
push(list, p);
}
return list;
}
//Insert a student node in the desired position on the linked list
int
insert(List *list, Student *s, int position)
{
Student *cur;
Student *prev;
int i;
int goflg;
//position -= 1;
#if 0
i = 1; // insert before Nth position
#else
i = 0; // insert after Nth position
#endif
prev = NULL;
for (cur = list->head; (cur != NULL) && (i < position);
++i, cur = cur->next) {
prev = cur;
}
// this may not be needed -- usual is to insert at tail if position is not
// found -- this will orphan the node to be inserted
#if 0
goflg = (i == position);
#else
goflg = 1;
#endif
if (goflg) {
s->next = cur;
if (prev != NULL)
prev->next = s;
else
list->head = s;
}
return goflg;
}
//Displays contents of a single student structure
void
display(Student *s)
{
printf("NAME:%s | GPA: %f | YEAR:%s\n", s->name, s->gpa, s->year);
}
//Displays contents of the entire linked list
void
displayAll(List *list)
{
Student *temp = list->head;
while (temp != NULL) {
display(temp);
temp = temp->next;
}
}
//Delete all data allocated on the heap before terminating program
void
cleanUp(List *list)
{
Student *cur;
Student *next;
for (cur = list->head; cur != NULL; cur = next) {
next = cur->next;
free(cur->name);
free(cur->year);
free(cur);
}
list->head = NULL;
}
//Main function tests your functions.
int
main(void)
{
List top = { NULL };
List *list;
printf("Program Started\n");
//Construct Linked List from Standard Input
list = buildStudentList(&top);
//Insert a new student in desired position
Student *s = makeStudent("Max", 3.0, "senior");
insert(list, s, 3);
//Display entire linked list
displayAll(list);
//Free all heap memory
cleanUp(list);
printf("Program Successful Exit\n");
exit(EXIT_SUCCESS);
}

Pointer problem while using Linked list in C

I'm working on a c programming code and most of the code is working fine except the polynomial Multiplication part. It has a runtime error. please help me in removing this runtime error from Polynomial Multiplication, I couldn't find the error i think it's in the third for loop.
Thanks...
It will be great if you solve the error
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 17
typedef struct node
{
int coeff;
struct node *next;
}node;
node * init();
void read(node *h1);
void print(node *h1);
node * add(node *h1,node *h2);
node * multiply(node *h1, node *h2);
void main()
{
node *h1=NULL,*h2=NULL,*h3=NULL;
int option;
do
{
printf("\n1 : create 1’st polynomial");
printf("\n2 : create 2’nd polynomial");
printf("\n3 : Add polynomials");
printf("\n4 : Multiply polynomials");
printf("\n5 : Quit");
printf("\nEnter your choice :");
scanf("%d",&option);
switch(option)
{
case 1:
h1=init();
read(h1);
break;
case 2:
h2=init();
read(h2);
break;
case 3:
h3=add(h1,h2);
printf("\n1’st polynomial -> ");
print(h1);
printf("\n2’nd polynomial -> ");
print(h2);
printf("\n Sum = ");
print(h3);
break;
case 4:
h3=multiply(h1,h2);
printf("\n1’st polynomial -> ");
print(h1);
printf("\n2’nd polynomial -> ");
print(h2);
printf("\n Product = ");
print(h3);
break;
}
}while(option!=5);
}
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf("\n Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{
printf("\nenter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
}
void print(node *p)
{
int i;
for(i=0;p!=NULL;i++,p=p->next)
if(p->coeff!=0)
printf("%dX^%d ",p->coeff,i);
}
node * add(node *h1, node *h2)
{
node *h3,*p;
h3=init();
p=h3;
while(h1!=NULL)
{
h3->coeff=h1->coeff+h2->coeff;
h1=h1->next;
h2=h2->next;
h3=h3->next;
}
return(p);
}
node * multiply(node *h1, node *h2)
{
node *h3,*p,*q,*r;
int i,j,k,coeff,power;
h3=init();
for(p=h1,i=0;p!=NULL;p=p->next,i++)
for(q=h2,j=0;q!=NULL;q=q->next,j++)
{
coeff=p->coeff * q->coeff;
power=i+j;
for(r=h3,k=0;k<power;k++)
r=r->next;
r->coeff=r->coeff+coeff;
}
return(h3);
}
node * init()
{
int i;
node *h=NULL,*p;
for(i=0;i<MAX;i++)
{
p=(node*)malloc(sizeof(node));
p->next=h;
p->coeff=0;
h=p;
}
return(h);
}
There is at least a probleme here in the multiply function:
...
for (r = h3, k = 0; k < power; k++)
r = r->next;
r->coeff = r->coeff + coeff;
...
At some point r becomes NULL and at the next step when you dereference r with r->coeff (r being NULLnow) your program will result un undefined behaviour (a segfault on most platforms).
There were several problems:
Memory was leaked: make sure you can point out a free for every malloc.
read was creating a new list, but leaking it and never updating the data in h1 or h2.
add didn't check for NULL h2.
The result of addition/multiplication was unnecessarily retained.
There was an arbitrary maximum of 17 nodes - the whole point of a linked list is so that there's no need to maintain such arbitrary limits.
Variables had scopes that were too large. This is 2011: one can declare variables near to point of use and not at the beginning of the block.
The use of scanf was non-idiomatic and inapplicable to interactive line-oriented input. For such input, read complete lines and parse them, not the stdin stream. In the stdin stream, a newline is a field separator and you don't want that, I think.
There weren't any asserts: program defensively, assert what you think should be true.
This works and should be correct and free of undefined behavior under all conditions.
// https://github.com/KubaO/stackoverflown/tree/master/questions/c-linked-debug-52867729
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
int power; // primary key for sorting
int coeff;
struct Node *next;
} Node;
Node *new_node(Node *prev, Node *next, int power);
Node *get_node(Node **head, Node *hint, int power);
void delete_nodes(Node *head);
Node *read(void);
void print(const Node *head);
Node *add(const Node *head1, const Node *head2);
Node *multiply(const Node *head1, const Node *head2);
void print_nodes(const Node *n1, const Node *n2, const char *extra_label,
const Node *extra);
const char *arity_suffix(int n);
bool parse_line(int max_length, const char *fmt, int count, ...);
static void *guarded_malloc(size_t size) {
void *result = malloc(size);
if (!result) {
fprintf(stdout, "Memory allocation has failed: exiting.");
abort();
}
return result;
}
int main() {
Node *h1 = NULL, *h2 = NULL;
int option;
do {
printf("\n1 : Create 1'st polynomial");
printf("\n2 : Create 2'nd polynomial");
printf("\n3 : Print polynomials");
printf("\n4 : Add polynomials");
printf("\n5 : Multiply polynomials");
printf("\n6 : Quit");
printf("\nEnter your choice: ");
if (!parse_line(10, "%d", 1, &option)) continue;
switch (option) {
case 1:
delete_nodes(h1);
h1 = read();
break;
case 2:
delete_nodes(h2);
h2 = read();
break;
case 3:
print_nodes(h1, h2, NULL, NULL);
break;
case 4: {
Node *sum = add(h1, h2);
print_nodes(h1, h2, "Sum", sum);
delete_nodes(sum);
break;
}
case 5: {
Node *prod = multiply(h1, h2);
print_nodes(h1, h2, "Product", prod);
delete_nodes(prod);
break;
}
}
} while (option != 6);
delete_nodes(h1);
delete_nodes(h2);
}
Node *read() {
int n;
printf("\n Enter number of terms: ");
if (!parse_line(10, "%d", 1, &n)) return NULL;
/* read n terms */
Node *head = NULL;
for (int i = 0; i < n;) {
int power, coeff;
printf("\nEnter the %d%s term (power coeff): ", i + 1, arity_suffix(i + 1));
if (!parse_line(80, "%d%d", 2, &power, &coeff) || !coeff) continue;
Node *p = get_node(&head, NULL, power);
if (!p->coeff) i++; // count only new terms
p->coeff = coeff;
}
return head;
}
void print(const Node *p) {
for (; p; p = p->next) printf("%dX^%d ", p->coeff, p->power);
}
void add_to(Node **sum, const Node *h) {
Node *r = NULL;
for (; h; h = h->next) {
r = get_node(sum, r, h->power);
r->coeff += h->coeff;
}
}
Node *add(const Node *h1, const Node *h2) {
Node *sum = NULL;
add_to(&sum, h1);
add_to(&sum, h2);
return sum;
}
Node *multiply(const Node *h1, const Node *h2) {
Node *prod = NULL;
for (const Node *p = h1; p; p = p->next) {
Node *r = NULL;
for (const Node *q = h2; q; q = q->next) {
int power = p->power + q->power;
r = get_node(&prod, r, power);
r->coeff += p->coeff * q->coeff;
}
}
return prod;
}
Node *new_node(Node *prev, Node *next, int power) {
assert(!prev || prev->power < power);
assert(!next || next->power > power);
Node *node = guarded_malloc(sizeof(Node));
node->power = power;
node->coeff = 0;
node->next = next;
if (prev) prev->next = node;
return node;
}
void delete_nodes(Node *head) {
while (head) {
Node *p = head;
head = head->next;
free(p);
}
}
static bool list_contains(Node *head, Node *elt) {
for (; head; head = head->next)
if (head == elt) return true;
return false;
}
Node *get_node(Node **head, Node *hint, int power) {
Node *node = hint;
Node *next = hint ? hint->next : head ? *head : NULL;
assert(!hint || !*head || list_contains(*head, hint));
assert(!hint || hint->power <= power);
assert(!node || !next || node->power < next->power);
while (next && next->power <= power) {
node = next;
next = next->next;
}
if (!node || node->power != power) {
assert(!node || node->power < power);
Node *n = new_node(node, next, power);
if (!node) *head = n;
node = n;
}
return node;
}
void print_nodes(const Node *h1, const Node *h2, const char *extra_label,
const Node *extra) {
printf("\n1'st polynomial -> ");
print(h1);
printf("\n2'nd polynomial -> ");
print(h2);
if (extra_label) {
printf("\n %s = ", extra_label);
print(extra);
}
printf("\n");
}
const char *arity_suffix(int n) {
if (n == 0) return "st";
if (n == 1) return "nd";
return "rd";
}
bool parse_line(int max_length, const char *fmt, int count, ...) {
bool result = false;
int const buf_size = max_length + 2; // include \n and zero termination
char *const buf = guarded_malloc((size_t)buf_size);
char *const str = fgets(buf, buf_size, stdin);
if (str) {
size_t n = strlen(str);
if (str[n - 1] == '\n') { // we must have read a whole line
str[n - 1] = '\0'; // remove the newline
va_list ap;
va_start(ap, count);
int rc = vsscanf(buf, fmt, ap);
va_end(ap);
result = rc == count;
}
}
free(buf);
return result;
}
I think you should use memcopy in read method
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf("\n Enter number of terms :");
scanf("%d",&n);
/* read n terms */
for (i=0;i<n;i++)
{
printf("\nenter a term(power coeff.)");
scanf("%d%d",&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
memcpy (h, p, sizeof(node))
}

Not able to write information into linear linked list

The processFilefunction scans the information from the file correctly to p, however the addNodeLast function doesn't add the information from p to the linked list employees because it crashes.
Can someone help me fix the crashing, so that it will link each node together?
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void processFile(NODEPTR *employees, FILE *fp);
void outputPayFile(NODEPTR employees);
FILE *fp;
int main(void) {
NODEPTR employees;
if (fopen_s(&fp, "payfile2.txt", "r") != 0) {
printf("Failed to open payfile.txt for reading\n");
exit(0);
}
...missing code...
}
void processFile(NODEPTR *employees, FILE *fp) {
int i = 0;
NODEPTR p;
while(i < 5) {
printf("Entered Loop\n");
p = (NODEPTR) malloc(sizeof(node));
fscanf(fp, "%s %s %c %d %c %f\n", p->firstName, p->lastName, &(p->gender),
&(p->tenure), &(p->rate), &(p->rate), &(p->salary));
addNodeLast(employees, p);
i++;
}
}
In list.h:
void addNodeLast(NODEPTR *list, NODEPTR t) {
NODEPTR p;
if (*list == NULL)
*list = t;
else {
p = *list;
while (p->next)
p = p->next;
p->next = t;
}
}
Definition of NODEPTR:
typedef struct node {
char firstName[11];
char lastName[16];
char gender;
int tenure;
char rate;
float salary;
struct node *next;
} node, *NODEPTR;
You never initialise the next pointer. This is a common mistake. The call to malloc does not zero your memory. As a result, when you add a node to a non-empty list, you are likely to run straight off the end because the first node's next pointer could be non-null.
You need to at least do this:
p = malloc(sizeof(node));
if( p != NULL ) p->next = NULL;
You could consider using calloc, which does zero:
p = calloc(1, sizeof(node));

Resources