Code:
/**
AUTHOR: Kevin Layfield
FILE: hw3.c
ASSIGNMENT: Homework 2
DATE: 9/14/2006
*/
#include <stdio.h>
#include <stdlib.h>
#include "hw3.h"
//initialization of a list. program crashes here. I don't know why.
void init_list(list_t *list){
list->head=NULL;
list->count=0;
list->index=0;
}
//add a new node to list
void add(list_t *list, int i, void *item)
{
//node to be added
node_t *newnode;
//increment count
list->count++;
//assign the third argument as data for the node
newnode->data=item;
//if the list is empty, create a head
if(list->count==0)
{
list->head = malloc(sizeof(node_t));
list->head->data = newnode->data;
list->head->next = NULL;
}
//else, traverse the list until desired position is reached
else
{
list->traverse = list->head;
while(list->index<i){
list->traverse=list->traverse->next;
list->index++;
}
item = malloc(sizeof(node_t));
//assign the new node's next value = to traverse's next value
newnode->next = list->traverse->next;
//essentially place the newnode in the position ahead of traverse
list->traverse->next = newnode;
//reset index
list->index=0;
}
}
void *get(list_t *list, int i)
{
void *returned;
list->traverse = list->head;
//traverse list until you reach desired position
while(list->index<i){
list->traverse=list->traverse->next;
list->index++;
}
list->index=0;
//assign the data value to a new variable
returned = list->traverse->data;
//return new variable
return returned;
}
int size(list_t *list)
{
//return the value that gets incremented each time a new node is added
return list->count;
}
the .h file.....
Code:
typedef struct{
void *info;
void *next;
}node_t;
typedef struct{
node_t *front;
int size;
}ll_t;
void init_ll(ll_t *l);
void add(ll_t *list, int i, void *item);
void *get(ll_t *list, int i);
int size(ll_t *list);
first off, i never used a linked list in C, only in Java and since its built into Util, it was a lot easier. Im trying to make a driver class in which i can put in any numbers i want, but i cant quite figure out how to do that, dont worry about the code above, i know its not all right, but i wanna fix it myself, just i have no idea where to go on the driver class. I know im gonna need a loop to go to the next node and keep gonig till the list is at its end. Thanks