Does anybody have any idea why my output displays the 1d node name for both the 2d nodes "alice" and "joe" to be "bob" when my input is: "Ins alice wife", followed by "Ins joe bob". I'm really stuck on this one.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct node2d {
struct node1d *first;
char *name;
struct node2d *down;
};
struct node1d {
struct node1d *next;
char *name;
};
int print2d (struct node2d *);
struct node2d *make2dnode(char *, struct node2d *);
struct node1d *make1dnode2(char *, struct node1d *);
int dostuff(struct node2d *);
int main (int argc, char *argv[]) {
int i;
int x = 0;
struct node2d *p2d=NULL;
for (i=argc-1; i>0; i--) {
p2d = make2dnode(argv[i], p2d);
};
dostuff(p2d);
dostuff(p2d);
print2d(p2d);
system("PAUSE");
}
int dostuff(struct node2d *node){
int c;
int x=0;
char input[1000];
struct node2d *p2d = node;
printf("Enter command: ");
while((c=getchar())!='\n'){
if (c==EOF)
return 2;
input[x] = c;
x++;
}
input[x]= '\0';
if(input[0]=='I' && input[1] == 'n' && input[2] == 's' && input[3] == ' '){
int i = 4;
int x = 0;
int y = 0;
int z = 0;
char node2[1000];
char node1[1000];
struct node1d *p1d=NULL;
while(input[i]!= ' '){
node2[x] = input[i];
x++;
i++;
}
node2[x]='\0';
i++;
while(input[i]!= NULL){
node1[z] = input[i];
z++;
i++;
}
node1[x]='\0';
while(p2d){
if(strcmp((p2d->name), node2)==0){
p1d = make1dnode2(node1, p1d);
if(p2d->first==NULL|| strcmp(p2d->first->name, p1d->name) == 1){
p2d->first=p1d;
}
else{
while(strcmp(p2d->first->name, p1d->name) != 1){
p2d->first=p2d->first->next;
}
p2d->first = p1d;
p1d->next= p2d ->first;
}
}
p2d=p2d->down;
}
}
else if(input[0] == 'R' && input[1] == 'e' && input[2] == 'm' && input[3] == ' ')
printf("Yess");
else{
printf("Error");
return 1;
}
system("PAUSE");
}
// Make a 2 dimensional node with down pointer next and name name
struct node2d *make2dnode(char *name, struct node2d *next){
struct node2d *p2d;
p2d = (struct node2d *) malloc(sizeof(struct node2d));
p2d->first = NULL;
p2d->name = name;
p2d->down = next;
return p2d;
}
int print2d (struct node2d *p2d) {
int i=0;
int x=0;
struct node1d *p1d;
while (p2d) {
(i==0) ? printf("The first 2d-node has name: %s\n", p2d->name): printf("The next 2d-node has name: %s\n", p2d->name);
i++;
p1d = p2d->first;
while (p1d) {
printf("\t %s has a 1d-node with name %s\n", p2d->name, p1d->name);
x++;
p1d = p1d->next;
};
p2d = p2d->down;
};
printf("The number of 2d nodes is %d\n", i);
printf("The number of 1d nodes is %d\n", x);
return 0;
}
struct node1d *make1dnode2(char *name, struct node1d *next){
struct node1d *p1d;
p1d = (struct node1d *) malloc(sizeof(struct node1d));
p1d->next = NULL;
p1d->name = name;
return p1d;
}