hi .i am wirtting a linked list and i tried testing my add method.I observed that my values are not beeing written in the right place.for e.g my test value 5 should be in node 1 node 1 and my test value 6 should be in node 2
Code:
/* * File: main.c
* Author: onwugbej
*
* Created on November 23, 2011, 10:24 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
struct node{
int key;
struct node *next;
};
struct listset{
int length;
struct node *head;
};
struct listset * litset_new(){
struct listset *p = malloc(sizeof(*p));
struct node *dummy; // creating a dummy for my node to point to
p->head=malloc(sizeof(*dummy)); //creating space for my node head in the list
dummy=malloc(sizeof(*dummy));
p->head->next=dummy;
p->head->key=0;
dummy->next=dummy; //pointing to itself
return p;
}
void listset_add(struct listset * t, int item){
//struct listset *a=t;
struct node *p= malloc(sizeof(*p));
p->key=item;
p->next = t->head;
t->head->next=p; //let my nlist set head point to my my new node
//p->key=item; //settinh my item
}
/* Given a linke list head pointer
* this function to counts the number of nodes in a list
*/
int Length(struct node *head) {
struct node* current =head;
int count = 0;
while (current != NULL)
{ count++;
current = current->next;
}
return count;
}
int main(int argc, char** argv) {
struct listset *s;
s=litset_new();
listset_add(s,5);
listset_add(s,6);
printf("%d\n",s->head->next->key);
printf("%d\n",s->head->next->next->key);
return (EXIT_SUCCESS);
}