![]() |
| | #1 | |
| Registered User Join Date: Apr 2009
Posts: 6
| Linked List Not Saving Value as Int Any suggestions will be greatly appreciated. node .h Code: #include <stdio.h>
#ifndef _NODE_H
#define _NODE_H
/* Struct to handle the linked list itself */
typedef struct node_tag{
char surface[30];
int cord[8];
struct node_tag *next;
struct node_tag *prev;
} Node;
typedef struct linked_list{
Node *head;
Node *tail;
} list;
Node *make_node(list *shapes, char *surface, int cord[]);
void push_back(list *shapes, char *surface, int cord[]);
void release(list *shapes) ;
void walk_list(list *shapes) ;
void delete_node(list *shapes, Node *p) ;
#endif
Code: #include "node.h"
list *getLevel(int x);
int main(void){
//Create list for level
list *level = NULL;
level = (list *)malloc(sizeof(list));
//get the current level - Takes a int parameter (the level you want to load) and returns a linked list.
level = getLevel(1);
Node *p = level->head;
if(isdigit(p->cord[0])){
printf("%d", p->cord[0]);
}
//DEBUG - Print the contents of the Level Linked List
walk_list(level);
return 0;
}
list *getLevel(int x){
list *shapes = NULL;
shapes = (list *)malloc(sizeof(list));
char extension[5] = ".txt";
char surface[30];
char level[30];
int cord[8];
FILE *cfPtr;
//Concatenate int level (x) and char extension into file name
sprintf(level, "%d%s", x, extension);
//check to see if file exists
if((cfPtr = fopen(level, "r")) == NULL){
printf("File could not be opened\n");
}
else{
//scan the first line
fscanf(cfPtr, "%s%d%d%d%d%d%d%d%d", surface, &cord[0], &cord[1], &cord[2], &cord[3], &cord[4], &cord[5], &cord[6], &cord[7]);
//Loop until you reach the end of the file
while(!feof(cfPtr)){
//add surface to linked list
push_back(shapes, surface, cord);
//scan for next surface
fscanf(cfPtr, "%s%d%d%d%d%d%d%d%d", surface, &cord[0], &cord[1], &cord[2], &cord[3], &cord[4], &cord[5], &cord[6], &cord[7]);
}
//close the file
fclose(cfPtr);
}
return shapes;
}
Code: #include "node.h"
Node *make_node(list *shapes, char *surface, int cord[]){
Node *tmp = NULL;
int x = 0;
tmp = (Node *)malloc(sizeof(Node)) ;
if (tmp == NULL) {
fprintf(stderr, "Error allocating memory in make_node().\n") ;
exit(1) ;
}
//add surface to node
if (surface)
strncpy(tmp->surface, surface, sizeof(tmp->surface)) ;
//add each coordinate to node
for(x=0; x<8; x++){
tmp->cord[x] = cord[x];
}
tmp->next = NULL ;
tmp->prev = NULL ;
return tmp ;
}
void push_back(list *shapes, char *surface, int cord[]){
Node *tmp = NULL ;
if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
fprintf(stderr, "Inconsistent list in call to push_front().\n") ;
exit(2) ;
}
tmp = make_node(shapes, surface, cord);
if (!tmp) {
fprintf(stderr, "Error building node in push_front().\n") ;
exit(3) ;
}
if (shapes->head == NULL && shapes->tail == NULL) {
shapes->head = tmp ;
shapes->tail = tmp ;
} else {
shapes->tail->next = tmp ;
tmp->prev = shapes->tail ;
shapes->tail = tmp ;
}
return ;
}
void release(list *shapes) {
while (shapes->head)
delete_node(shapes, shapes->tail) ;
return ;
}
void walk_list(list *shapes) {
Node *p = shapes->head ;
int count = 0 ;
if (shapes->head == NULL && shapes->tail == NULL) {
printf("======================\nEmpty List.\n=====================\n") ;
return ; /* not an error to try to print an empty list */
}
if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
fprintf(stderr, "Error! Inconsistent list in call to walk_list()\n") ;
exit(4) ;
}
while (p) {
/* printf("Node[%d]:%s", count, p->surface);*/
printf("Node[%d]: %s, %d, %d, %d, %d, %d, %d, %d, %d\n", count, p->surface, p->cord[0], p->cord[1], p->cord[2], p->cord[3], p->cord[4], p->cord[5], p->cord[6], p->cord[7]) ;
count++ ;
p = p->next ;
}
}
void delete_node(list *shapes, Node *p) {
if ((shapes->head == NULL && shapes->tail != NULL) || (shapes->head != NULL && shapes->tail == NULL)) {
fprintf(stderr, "Inconsistent list in call to delete_node\n") ;
exit(5) ;
}
if ((shapes->head == NULL && shapes->tail == NULL) || (p == NULL))
return ;
if (p == shapes->head && p == shapes->tail) { /* Only one item in the list */
shapes->head = NULL ;
shapes->tail = NULL ;
} else if (p == shapes->head) { /* If we delete the head node */
shapes->head = shapes->head->next ;
shapes->head->prev = NULL ; /* only valid because of previous line */
} else if (p == shapes->tail) { /* If we delete the tail node */
shapes->tail = shapes->tail->prev ;
shapes->tail->next = NULL ;
} else { /* If we delete a node in the middle */
Node *pre_p, *post_p ;
pre_p = p->prev ;
post_p = p->next ;
pre_p->next = post_p ;
post_p->prev = pre_p ;
}
if (p)
free(p) ;
return ;
}
Quote:
| |
| bar338 is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| isdigit() checks characters, not integers. isdigit('0') (the character), returns true isdigit(0) (the integer) returns false If you want to know if you read it in properly, check the fscanf() result.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| Code: int main(void){
//Create list for level
list *level = NULL;
level = (list *)malloc(sizeof(list));
//get the current level - Takes a int parameter (the level you want to load) and returns a linked list.
level = getLevel(1);
...
}
list *getLevel(int x){
list *shapes = NULL;
shapes = (list *)malloc(sizeof(list));
...
return shapes;
}
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 Last edited by hk_mp5kpdw; 05-04-2009 at 11:17 AM. |
| hk_mp5kpdw is offline | |
| | #4 |
| Registered User Join Date: Apr 2009
Posts: 6
| My understanding of c memory usage is not the best at the moment. So how should i go about fixing the memory leak. I need to section off memory in both getLevel and main to set the variable type and save the linked list to it don't I? |
| bar338 is offline | |
| | #5 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| In the first section, you're allocating memory, and pointing at it. In the second, you're allocating more memory, and making the pointer point to the new memory. You need to free up the old memory, before you stop pointing at it. Or, you need to actually make use of your first block of memory, and not bother using the new chunk. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| NEED HELP READING FILE and PRINTING | geoffr0 | C Programming | 4 | 04-16-2009 05:26 PM |
| Sorting linked list please help with CODE | scarlet00014 | C Programming | 3 | 09-27-2008 11:24 PM |
| memory leak | aruna1 | C++ Programming | 3 | 08-17-2008 10:28 PM |
| Function argument assignment between types "unsigned int*" and "unsigned long*" | nadeer78 | C Programming | 8 | 03-10-2008 11:57 AM |
| My graphics library | stupid_mutt | C Programming | 3 | 11-26-2001 06:05 PM |