数据结构十字链表创建,求大佬帮忙
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
const int MaxVertexNum = 100;//最大的顶点数目
typedef struct EdgeNode {//边结点
char tail, head;
EdgeNode* hLink;
EdgeNode* tLink;
}EdgeNode;
typedef struct VertexNode {//顶点结点
char data;
EdgeNode * fstIn;
EdgeNode * fstOut;
}Vertex;
typedef struct VertexList {
Vertex array;
int Vnum, ArcNum; //表示输入的顶点数和弧数
}Graph, *pGraph;
int locateVertex(char x, pGraph G) {
for (int i = 0; i < G->Vnum; i++) {
if (G->array.data == x) {
return i;
}
}
return 0;
}
void createGraph(pGraph G) {//创造又想吐
int v, a;
char vertex;
char tail, head;//分别表示弧的弧头和弧尾
EdgeNode *q;
printf("请输入该有向图的顶点数和弧数:");
scanf("%d %d", &v, &a);
G->Vnum = v;
G->ArcNum = a;
printf("请输入顶点:\n");
//顶点表的初始化
for (int i = 0; i < G->Vnum; i++) {
printf("请输入第%d个顶点的值:", i + 1);
scanf("%c", &G->array.data);
G->array.fstIn = G->array.fstOut = NULL;
}
printf("建立边表:\n");
for (int i = 0; i < G->ArcNum; i++) {
int k, j;
printf("请输入弧:\n");
scanf("%c,%c", &tail, &head);
k = locateVertex(tail, G);
j = locateVertex(head, G);
q = (EdgeNode *)malloc(sizeof(EdgeNode));
q->tail = tail;
q->head = head;
q->hLink = G->array.fstIn;//此处建议别画图,越画越乱,直接写
q->tLink = G->array.fstOut;
G->array.fstOut = q;
G->array.fstIn = q;
}
}
void displayGraph(pGraph G) {
EdgeNode *q;
EdgeNode *p;
for (int i = 0; i < G->Vnum; i++) {
q = G->array.fstIn;
p = G->array.fstOut;
if (!q)
{
printf("%c顶点没有入度!", G->array.data);
}
else {
printf("%c的入度是: \n", G->array.data);
while (!q)
{
printf("%c->%c ", q->tail, q->head);
q = q->hLink;
}
}
if (!p) {
printf("%c顶点没有出度!", G->array.data);
}
else {
printf("%c的出度是: \n", G->array.data);
while (!p)
{
printf("%c->%c", p->tail, p->head);
q = q->tLink;
}
}
}
}
int main() {
Graph G;
createGraph(&G);
displayGraph(&G);
return 0;
}
以上是我码的代码,但是有个奇葩的问题
就是我在对顶点表初始化的时候就是以下这段
//顶点表的初始化
for (int i = 0; i < G->Vnum; i++) {
printf("请输入第%d个顶点的值:", i + 1);
scanf("%c", &G->array.data);
G->array.fstIn = G->array.fstOut = NULL;
}
经过我的调试,我发现进入for循环第一个scanf直接不执行.而且,我原本定的顶点数是3个顶点
但是第一个和第三个scanf直接跳过,只输入了一次,人晕了,希望有大佬能帮帮我。。。。 本问题已解决
总的来说是scanf 输出字符的时候没有清空键盘缓存区的问题
本来我是已经想到这个低级问题的但是我用了fflush(stdin) 万万没想到 VS是无法支持这个的
然后我用了getchar();
然后最后调试之后,发现原代码还有错,以下是完善之后的
其实原本用的scanf_s的,一直以为是scanf_s的问题,我太菜了{:5_100:}{:5_100:}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
const int MaxVertexNum = 100;//最大的顶点数目
typedef struct EdgeNode {//边结点
char tail, head;
EdgeNode* hLink;
EdgeNode* tLink;
}EdgeNode;
typedef struct VertexNode {//顶点结点
char data;
EdgeNode * fstIn;
EdgeNode * fstOut;
}Vertex;
typedef struct VertexList {
Vertex array;
int Vnum, ArcNum; //表示输入的顶点数和弧数
}Graph, *pGraph;
int locateVertex(char x, pGraph G) {
for (int i = 0; i < G->Vnum; i++) {
if (G->array.data == x) {
return i;
}
}
return 0;
}
void createGraph(pGraph G) {//创造又想吐
int v, a;
char vertex;
char tail, head;//分别表示弧的弧头和弧尾
EdgeNode *q;
printf("请输入该有向图的顶点数和弧数:");
scanf("%d %d", &v, &a);
G->Vnum = v;
G->ArcNum = a;
printf("请输入顶点:\n");
//顶点表的初始化
for (int i = 0; i < G->Vnum; i++) {
getchar();
scanf("%c", &G->array.data);
G->array.fstIn = G->array.fstOut = NULL;
}
printf("建立边表:\n");
for (int i = 0; i < G->ArcNum; i++) {
int k, j;
printf("请输入弧:\n");
getchar();
scanf("%c %c", &tail, &head);
k = locateVertex(tail, G);
j = locateVertex(head, G);
q = (EdgeNode *)malloc(sizeof(EdgeNode));
q->tail = tail;
q->head = head;
q->hLink = G->array.fstIn;//此处建议别画图,越画越乱,直接写
q->tLink = G->array.fstOut;
G->array.fstOut = q;
G->array.fstIn = q;
}
}
void displayGraph(pGraph G) {
EdgeNode *q;
EdgeNode *p;
for (int i = 0; i < G->Vnum; i++) {
q = G->array.fstIn;
p = G->array.fstOut;
if (!q)
{
printf("%c顶点没有入度!\n", G->array.data);
}
else {
printf("\n%c的入度是:\n", G->array.data);
while (q)
{
printf("%c->%c ", q->tail, q->head);
q = q->hLink;
}
}
if (!p) {
printf("%c顶点没有出度!", G->array.data);
}
else {
printf("\n%c的出度是:\n", G->array.data);
while (p)
{
printf("%c->%c ", p->tail, p->head);
p = p->tLink;
}
}
}
}
int main() {
Graph G;
createGraph(&G);
displayGraph(&G);
return 0;
}
页:
[1]