D
Dan
I'm trying to creat a data structure, that can be either a integer,
double, string, or linked list. So I created the following, but don't
know if it is the data structure itself causing problems, or something
I am doing in the rest of the program.
This is the data structure.
struct node
{
char type;
union data
{
int integer;
double number;
char* word;
struct node* list;
}data;
struct node *next;
};
And this is the program I am using it in.
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char type;
union data
{
int integer;
double number;
char* word;
struct node* list;
}data;
struct node *next;
};
typedef struct node node;
typedef struct node* node_ptr;
node_ptr new_node(char type);
void add_node(node_ptr destination, node_ptr source);
void print_node(node_ptr value);
int main()
{
node_ptr A;
node_ptr B;
node_ptr C;
node_ptr D;
A=new_node('I');
A->data.integer = 1;
printf("%i ", A->data.integer);
B=new_node('S');
B->data.word = "Hello";
printf("%s ", B->data.word);
C=new_node('D');
C->data.number = 1.567;
printf("%f ", C->data.number);
D=new_node('L');
add_node (D,A);
print_node(D);
}
node_ptr new_node(char type)
{
node_ptr ret_ptr = (node_ptr) malloc (sizeof(node));
ret_ptr->type = type;
switch (toupper(type))
{
case 'I':
ret_ptr->data.integer=0;
break;
case 'D':
ret_ptr->data.number=0;
break;
case 'S':
ret_ptr->data.word = "";
break;
case 'L':
ret_ptr->data.list = NULL;
break;
default:
printf("Error");
return NULL;
}
ret_ptr->next = NULL;
return ret_ptr;
}
void add_node(node_ptr destination, node_ptr source)
{
node_ptr next_ptr;
node_ptr prev_ptr;
printf("%c ", destination->type);
if (destination->type =! 'L')
{
printf("Error");
return;
}
if (destination->data.list == NULL)
{
destination->data.list = source;
printf("%c ", destination->type);
return;
}
next_ptr = destination->data.list;
while (next_ptr->data.list != NULL)
{
prev_ptr = next_ptr;
next_ptr= next_ptr->next;
}
prev_ptr->next = next_ptr;
}
void print_node(node_ptr value)
{
if (value == NULL)
return;
printf("%c ",value->type);
switch (value->type)
{
case 'I':
printf("% i", value->data.integer);
break;
case 'D':
printf("% f", value->data.number);
break;
case 'S':
printf("% s", value->data.word);
break;
case 'L':
printf("[");
print_node(value->data.list);
printf("]");
break;
default:
printf("Error");
return;
}
print_node(value->next);
}
The bug is in add node with a list, 'L', why does it change the actual
node itself. Maybe I don't see the logic but why does this line
destination->data.list = source;
change the actual destination node itself to something else. It seems
to me it would just change the list data member in the inner union to
point to the source pointer. But it seems change the actual
destination pointer itself.
Arggh...C drives me nuts.
Any help would be appreciated.
Thanks,
Dan
double, string, or linked list. So I created the following, but don't
know if it is the data structure itself causing problems, or something
I am doing in the rest of the program.
This is the data structure.
struct node
{
char type;
union data
{
int integer;
double number;
char* word;
struct node* list;
}data;
struct node *next;
};
And this is the program I am using it in.
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char type;
union data
{
int integer;
double number;
char* word;
struct node* list;
}data;
struct node *next;
};
typedef struct node node;
typedef struct node* node_ptr;
node_ptr new_node(char type);
void add_node(node_ptr destination, node_ptr source);
void print_node(node_ptr value);
int main()
{
node_ptr A;
node_ptr B;
node_ptr C;
node_ptr D;
A=new_node('I');
A->data.integer = 1;
printf("%i ", A->data.integer);
B=new_node('S');
B->data.word = "Hello";
printf("%s ", B->data.word);
C=new_node('D');
C->data.number = 1.567;
printf("%f ", C->data.number);
D=new_node('L');
add_node (D,A);
print_node(D);
}
node_ptr new_node(char type)
{
node_ptr ret_ptr = (node_ptr) malloc (sizeof(node));
ret_ptr->type = type;
switch (toupper(type))
{
case 'I':
ret_ptr->data.integer=0;
break;
case 'D':
ret_ptr->data.number=0;
break;
case 'S':
ret_ptr->data.word = "";
break;
case 'L':
ret_ptr->data.list = NULL;
break;
default:
printf("Error");
return NULL;
}
ret_ptr->next = NULL;
return ret_ptr;
}
void add_node(node_ptr destination, node_ptr source)
{
node_ptr next_ptr;
node_ptr prev_ptr;
printf("%c ", destination->type);
if (destination->type =! 'L')
{
printf("Error");
return;
}
if (destination->data.list == NULL)
{
destination->data.list = source;
printf("%c ", destination->type);
return;
}
next_ptr = destination->data.list;
while (next_ptr->data.list != NULL)
{
prev_ptr = next_ptr;
next_ptr= next_ptr->next;
}
prev_ptr->next = next_ptr;
}
void print_node(node_ptr value)
{
if (value == NULL)
return;
printf("%c ",value->type);
switch (value->type)
{
case 'I':
printf("% i", value->data.integer);
break;
case 'D':
printf("% f", value->data.number);
break;
case 'S':
printf("% s", value->data.word);
break;
case 'L':
printf("[");
print_node(value->data.list);
printf("]");
break;
default:
printf("Error");
return;
}
print_node(value->next);
}
The bug is in add node with a list, 'L', why does it change the actual
node itself. Maybe I don't see the logic but why does this line
destination->data.list = source;
change the actual destination node itself to something else. It seems
to me it would just change the list data member in the inner union to
point to the source pointer. But it seems change the actual
destination pointer itself.
Arggh...C drives me nuts.
Any help would be appreciated.
Thanks,
Dan