stamatiou

July 21st, 2011, 04:22 PM

Hey guys,

I am trying to do a function that gets the length of a singly linked list but it always tells me it is zero! Here's the code:

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

struct node *next,*prev;

int data;

}Node;

typedef struct list{

Node *head,*tail;

int len;

}List;

int find_len(List *list)

{

int counter = 0;

Node *dummy;

dummy = (*list).head;

while(dummy != NULL)

{

counter = counter + 1;

dummy = dummy->next;

}

counter = list->len;

return 0;

}

Node *new1,*new2,*new3;

List list;

new1 = malloc(sizeof(Node));

new2 = malloc(sizeof(Node));

new3 = malloc(sizeof(Node));

new1->data = 26;

new2->data = 12;

new3->data = 45;

list.head = new1;

list.tail = new3;

list.head->next = new2;

new2->next = new3;

new3->prev = new2;

new2->prev = new1;

find_len(&list);

printf("LENGTH = %d",list.len);

return 0;

}

I am trying to do a function that gets the length of a singly linked list but it always tells me it is zero! Here's the code:

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

struct node *next,*prev;

int data;

}Node;

typedef struct list{

Node *head,*tail;

int len;

}List;

int find_len(List *list)

{

int counter = 0;

Node *dummy;

dummy = (*list).head;

while(dummy != NULL)

{

counter = counter + 1;

dummy = dummy->next;

}

counter = list->len;

return 0;

}

Node *new1,*new2,*new3;

List list;

new1 = malloc(sizeof(Node));

new2 = malloc(sizeof(Node));

new3 = malloc(sizeof(Node));

new1->data = 26;

new2->data = 12;

new3->data = 45;

list.head = new1;

list.tail = new3;

list.head->next = new2;

new2->next = new3;

new3->prev = new2;

new2->prev = new1;

find_len(&list);

printf("LENGTH = %d",list.len);

return 0;

}