我是C编程的新手,我正在尝试用一个节点列表和一个桥梁列表组成的图表,这些桥梁应该指向第一个列表的另一个节点。 要明确:
node1 - >桥 - > node2和节点列表node1-> node2-> nodeN。 输入
nodeA
nodeB
nodeC
nodeD
和第二个输入
nodeA nodeB
nodeA nodeD
nodeB nodeC
nodeC nodeD
我做错了什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 20
struct node{
char name[MAXL];
struct node *next;
struct link *bridge;
};
struct link{
struct link *next;
struct node *bridge;
};
struct node *insertnode(struct node *node, char name[MAXL]);
struct node *bridgelink(struct node *node, struct node *link, char finishname[MAXL]);
struct link *insert(struct node *node, struct link *link, char startname[MAXL], char finishname[MAXL]);
int main(int argc, const char * argv[]) {
char t_name[MAXL], start[MAXL], finish[MAXL];
struct node *mynode=NULL;
FILE *innodes;
FILE *inlinks;
innodes=fopen("innodes.txt", "r");
inlinks=fopen("inlinks.txt", "r");
while(fscanf(innodes, "%s", t_name)!=EOF){
mynode=insertnode(mynode, t_name);
}
while (fscanf(inlinks, "%s %s", start, finish)!=EOF) {
while(mynode!=NULL){
if(strcmp(mynode->name, start)==0){
mynode->bridge=insert(mynode, mynode->bridge, start, finish);
}
mynode=mynode->next;
}
}
return 0;
}
struct node *insertnode(struct node *node, char name[MAXL]){
struct node *newptr=NULL;
newptr=malloc(sizeof(struct node));
strcpy(newptr->name, name);
newptr->next=node;
return newptr;
}
struct node *bridgelink(struct node *node, struct node *link, char finishname[MAXL]){
struct node *newptr=NULL;
newptr=malloc(sizeof(struct node));
newptr->next=node;
return newptr;
}
struct link *insert(struct node *node, struct link *link, char startname[MAXL], char finishname[MAXL]){
struct link *newptr;
newptr=malloc(sizeof(struct link));
while(node!=NULL){
if(strcmp(node->name, finishname)==0){
newptr->bridge=bridgelink(newptr->bridge, node, finishname);
}
node=node->next;
}
newptr->next=link;
return newptr;
}
答案 0 :(得分:0)
这需要一些错误检查代码,但似乎可以做你想要的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 20
struct node{
char name[MAXL];
struct node *next;
struct link *bridge;
};
struct link{
struct link *next;
struct node *bridge;
};
struct node *insertnode(struct node *node, char name[MAXL]);
static void bridge(struct node *snode, struct node *fnode);
int main(int argc, const char * argv[]) {
char t_name[MAXL], start[MAXL], finish[MAXL];
struct node *mynode=NULL;
struct node *snode, *fnode, *tnode;
struct link *tlink;
FILE *innodes;
FILE *inlinks;
innodes=fopen("innodes.txt", "r");
inlinks=fopen("inlinks.txt", "r");
while(fscanf(innodes, "%s", t_name)!=EOF){
mynode=insertnode(mynode, t_name);
}
while (fscanf(inlinks, "%s %s", start, finish)==2) {
snode=NULL;
fnode=NULL;
tnode=mynode;
while(tnode!=NULL){
if(strcmp(tnode->name, start)==0){
snode=tnode;
if(fnode!=NULL){
break;
}
}
if(strcmp(tnode->name, finish)==0){
fnode=tnode;
if(snode!=NULL){
break;
}
}
tnode=tnode->next;
}
if(snode!=NULL && fnode!=NULL){
bridge(snode, fnode);
}else{
if(snode==NULL){
fprintf(stderr, "start node '%s' not valid\n", start);
}
if(fnode==NULL){
fprintf(stderr, "finish node '%s' not valid\n", finish);
}
}
}
for(snode=mynode; snode!=NULL; snode=snode->next){
if(snode->bridge==NULL){
printf("%s\n", snode->name);
}else{
for(tlink=snode->bridge; tlink!=NULL; tlink=tlink->next){
printf("%s => %s\n", snode->name, tlink->bridge->name);
}
}
}
return 0;
}
struct node *insertnode(struct node *node, char name[MAXL]){
struct node *newptr=NULL;
newptr=malloc(sizeof(struct node));
strcpy(newptr->name, name);
newptr->next=node;
newptr->bridge=NULL;
return newptr;
}
static void bridge(struct node *snode, struct node *fnode){
struct link *newptr=NULL;
newptr=malloc(sizeof(struct link));
newptr->bridge=fnode;
newptr->next=snode->bridge;
snode->bridge=newptr;
}