C issue - Can't figure how to assign pointer to beginning of list -
i have simple assignment professor wants do. pull in numbers text file , load linked list. don't want details have basic question.
he provided function so:
intlist* init_intlist( int n ) { intlist *lst; lst = (intlist *)malloc(sizeof(intlist)); lst->datum = n; lst->next = null; return lst; }
this function used initialize linked list first element. asked define function signature:
int insert_intlist( intlist *lst, int n )
so assume wants add linked list tried this:
int insert_intlist( intlist *lst, int n ) { intlist* lsttemp; lsttemp = (intlist *)malloc(sizeof(intlist)); lsttemp->datum = n; lsttemp->next = lst; lst = lsttemp; free(lsttemp); }
so thought process creates temporary node, assigns data value (datum) , assigns next pointer point current pointer pointing at. reassign main pointer newly created temp node.
that way have instance 2 nodes:
[new temp node] -> [prev initialized node]
when step through code looks great...
then in main have function print list:
while (lst!=null) { printf("the value is:%d", lst->datum); lst=lst->next; }
the problem seems print 1 digit (namely first digit reading in file, think last 1 in list or @ least thought last 1 in list).
but should keep going through have 10 digits in file. know code dirty , clean up...here entire main function if needs more info:
#include <stdio.h> #include <stdlib.h> #include "intlist.h" int main(int argc, char *argv[]) { char c; /* character read file. */ file* ptr; /* pointer file. file structure defined in <stdio.h> */ int index=0; //intlist* alist[10]; //will use later /* open file - no error checking done */ ptr = fopen("1.txt","r"); /* read 1 character @ time, checking end of file. eof defined in <stdio.h> -1 */ if(ptr==null) { printf("error: can't open file.\n"); /* fclose(file); don't pass null pointer fclose !! */ return 1; } //alist[index] = malloc(sizeof(intlist)); need later on.... intlist *lst=null; while ((c = fgetc(ptr)) != eof) { if (c != ' ') { //make sure isnt space int = c - '0'; //get value text file if(c=='\n') { // alist[index]=lst; // index++; // alist[index] = malloc(sizeof(intlist)); while (lst!=null) { printf("the value is:%d", lst->datum); lst=lst->next; } free(lst); free(alist[index]); return 0; //new line in file //create linked list } if (lst==null) lst = init_intlist(i); else insert_intlist( lst, i); } } fclose(ptr); system("pause"); return 0; }
here intlist.h may need it:
#ifndef __intlist_h__ #define __intlist_h__ /* each entry in list contains int */ typedef struct intlist { int datum; struct intlist *next; } intlist; intlist *init_intlist( int n ); /* initializes intlist initial datum n */ int insert_intlist( intlist *lst, int n ); /* inserts int (n) intlist beginning*/ void list_append(intlist *list, void *datum); /* inserts entry end of list */ intlist* list_front(intlist *list); /*return element @ front of list, , remove list*/ void list_map( intlist *list, void (*f)(void *) ); /*applies function each element of list */ void list_delete( intlist *list ); /* deletes (and frees) entries in list */ #endif
a couple of issues here.
i'll start bad bug:
int insert_intlist( intlist *lst, int n ) { intlist* lsttemp; lsttemp = (intlist *)malloc(sizeof(intlist)); lsttemp->datum = n; lsttemp->next = lst; lst = lsttemp; free(lsttemp); // <<<<< no! }
you still using memory, can't free it.
secondly, proto-type supplied insertion has no way return new front of list, can not change front of list. implies must add new nodes back, rather front have done.
also, supplied return type of int
means expects out number of nodes in list, no problem you're going have walk list find anyway.
have go @ it, you're not doing badly @ all.
Comments
Post a Comment