Hello I'm trying to code a linked list but it's not working. Malloc at first and at last pointers is returning a no matching function for call to. Here's the code:
#include <stdio.h>
#include <stdlib.h>
typedef struct number{
int x;
struct number *next;
}Number;
Number *first = NULL, *temp = NULL, *last = NULL;
void insert(int x){
if(first==NULL){
first= (Number)malloc(sizeof(Number));
first->x=x;
first->next = NULL;
temp=first;
}
else{
last = (Number)malloc(sizeof(Number));
last->x = x;
last->next = NULL;
temp->next =last;
temp=last;
}
}
void printList(){
Number *hold = first;
while(hold!=NULL){
printf("\n%d\n",hold->x);
hold = hold->next;
}
}
int main(){
int size;
printf("Enter size: ");
scanf("%d",&size);
Number *hold = first;
printf("Enter value:\n");
for(int i=0;i<size;i ){
int x;
scanf("%d",&x);
insert(x);
}
printfList();
}
what could be wrong here?
CodePudding user response:
Your "insert" logic ("append", actually) is wonky. Regions are allocated, but the addresses returned are soon lost.
void insert( int x ) {
temp = malloc( sizeof *temp ); // Make a node
/* omitting test for NULL */
temp->x = x; // populate its members
temp->next = NULL;
if( first == NULL )
first = temp; // all done!
else
last = last->next = temp; // all done here, too!
}
CodePudding user response:
the problem is that you declared first
to be a pointer in that line
Number *first = NULL, *temp = NULL, *last = NULL;
but when you are trying to use malloc
, malloc
returns a pointer of type void
, so you should cast it into (Number*)
not (Number)
, you should cast void pointer
into Number pointer
.
so instead of
first = (Number)malloc(sizeof(Number));
you should do
first = (Number*)malloc(sizeof(Number));
refer to malloc() manual as the prototype of malloc
is
void *malloc(size_t size);
and just one small syntax error, it's not
printfList();
it's :
printList();
with these small edits, this is the final code:
#include <stdio.h>
#include <stdlib.h>
typedef struct number{
int x;
struct number *next;
}Number;
Number *first = NULL, *temp = NULL, *last = NULL;
void insert(int x){
if(first==NULL){
first = (Number*)malloc(sizeof(Number));
first->x=x;
first->next = NULL;
temp=first;
}
else{
last = (Number*)malloc(sizeof(Number));
last->x = x;
last->next = NULL;
temp->next =last;
temp=last;
}
}
void printList(){
Number *hold = first;
while(hold!=NULL){
printf("\n%d\n",hold->x);
hold = hold->next;
}
}
int main(){
int size;
printf("Enter size: ");
scanf("%d",&size);
printf("Enter value:\n");
for(int i=0;i<size;i ){
int x;
scanf("%d",&x);
insert(x);
}
printList();
}
and this is some example output:
Enter size:5
Enter value:
1
2
3
4
5
1
2
3
4
5
CodePudding user response:
- The error message sounds you like are compiling this with a c instead of a c compiler.
Number
is a typedef forstruct Number
but you use it as a pointer. In c, we don't cast void pointers so just leave it out:
void insert(int x) {
if(first==NULL){
first = malloc(sizeof(Number));
first->x=x;
first->next = NULL;
temp = first;
} else {
last = malloc(sizeof(Number));
last->x = x;
last->next = NULL;
temp->next = last;
temp = last;
}
}
printfList()
should beprintList()
.
and here is an example execution:
Enter size: 3
Enter value:
1
2
3
1
2
3
I suggest you eliminate the global variables and pass them into the functions that need them.
temp
, in particular, is a implementation detail ofinsert()
.Also, the inconsistent formatting, means that I instantly distrust your code. Formatting matters for readability so why make it hard for yourself?