#include<stdio.h>
#include<stdlib.h>
typedef struct {
int val;
struct node* next;
} Node, *NodePtr;
void makeInitialLinkedList(int a[], int n)
{
int i;
NodePtr rec=NULL;
NodePtr head=NULL;
for (i=0; i<n; i++) {
NodePtr cur=(NodePtr)malloc(sizeof(Node));
if (i==0) head=cur;
if (rec!=NULL) rec->next=(node*)cur;
cur->val=a[i];
cur->next=NULL;
rec=cur;
}
// test
while(n--) {
printf("%d ", head->val);
head=(NodePtr)head->next;
}
}
NodePtr copy(NodePtr head)
{
return NULL;
}
int main()
{
//Mission #2
int initialDataArray[10]={5,3,2,8,9,7,0,6,4,1};
makeInitialLinkedList(initialDataArray, 10);
}
I do not understand why this works. I did not make a name initially for the struct and only made a surname "Node" by typedef. Then how does "struct node* next" work correctly? is node* a kind of data types like int* or char*? I thought like this:
typedef struct strName {
int val;
struct strName* next;
} strSurName, *strPtr;
Does the type of pointer "next" and the initial name of struct have to same?
void *typedefpointers; this is likely to lead to errors and confusion.nodeis not defined just as you thought