I was reading on linked lists, and the only good source I could find was one from Stanford CS Library. I was hoping to implement what I learned from it, and run it on my compiler. The program is to find the number of elements in a linked list of {1,2,3}.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
int main()
{
struct node* BuildOneTwoThree()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1; // setup first node
head->next = second; // note: pointer assignment rule
second->data = 2; // setup second node
second->next = third;
third->data = 3; // setup third link
third->next = NULL;
return head;
int Length(struct node* head)
{
struct node* current = head;
int count = 0;
while (current != NULL)
{
count++;
current = current->next;
}
printf("%d",count);
return count;
}
}
return 0;
}
It is returning blank. I don't understand where O made a mistake, what am I doing wrong?
gcc -Wall -g) then use the debugger (e.g.gdb)