I'm reading through a data structure textbook. I'm doing the part of Linked list.
here's the code from the textbook:
I'm not clear with pointer.
what I'm confused is that the code created a pointer to the structure (*NodePtr)
Q1. Is NodePtr store the address of the structure??
Q2. Are top, np, last address of the structure??
Q3. here.....NodePtr makeNode(int);... does it returns an address of the structure which is np?? but following part np is used as a pointer??
Please help me... thank you guys...
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int num;
struct node* next;
}Node, *NodePtr;
main()
{
void printlist(NodePtr);
NodePtr makeNode(int);
int n;
NodePtr top, np, last;
top = NULL;
if(scan(%d, &n)!= 1) n=0;
while(n!=0)
{
np= makeNode(n);
if(top == NULL) top = np;
else last -> next = np;
last = np;
if(scanf("%d", &n)!=1) n= 0;
}
printList(top);
}
NodePtr makeNode(int n)
{
NodePtr np = (NodePtr)malloc(sizeof(Node));
np -> num = n;
np -> next = NULL;
return np;
}
void printList(NodePtr np)
{
while(np != NULL)
{
printf("%d\n", np->num);
np = np->next;
}
}
from Data Structures in C. by Noel Kalicharan. page60