I wrote a program up with linked lists that creates a circular list containing 9 nodes. Each node contains a number (ranging from 1-9) as an item.
My question is...in the while loops, how does
x = x->next
cause the pointer to move across the list?
#include <stdio.h>
#include <stdlib.h>
#define FLAG 1
typedef struct node* link;
struct node {
int item;
link next;
}; // node containing an item and a link
int main(void)
{
int i = 2;
int cnt = 1;
int N = 9;
int num;
link t = (link)malloc(sizeof *t);
link x = t;
t->item = 1;
t->next = t;
for (;i <= N; i++)
{
x = (x->next = (link)malloc(sizeof *x));
x->item = i;
x->next = t;
}
puts("Enter a number between 1-9");
scanf("%d", &num);
while (x->item != num)
x = x->next;
t = x;
while (FLAG == 1)
{
if (t == x->next)
{
printf("\nTotal # of %d nodes\n", cnt);
break;
}
x = x->next;
printf("%d ", x->item);
cnt++;
}
return 0;
}