#include <stdio.h>
#include <stdlib.h>
//定义六边形结构体
typedef struct hexagon{
int num;
int x;
int y;
struct hexagon* next;
}Hexagon;
//创建新的六边形节点
Hexagon* creat_hexagon(int num,int x,int y){
Hexagon* new_hexagon = (Hexagon*)malloc(sizeof(Hexagon));
new_hexagon->num = num;
new_hexagon->x = x;
new_hexagon->y = y;
new_hexagon->next = NULL;
return new_hexagon;
}
//在链表的结尾添加新的六边形节点
void add_hexagon(Hexagon** head,Hexagon* new_hexagon){
Hexagon* current_hexagon = *head;
if(*head == NULL){
*head = new_hexagon;
}else{
while(current_hexagon->next != NULL){
current_hexagon = current_hexagon->next;
}
current_hexagon->next = new_hexagon->next;
}
}
// 输出链表中存储的六边形信息
void print_hexagon(Hexagon* head) {
while (head != NULL) {
for(int i = 0; i <= 6; i++)
{
printf("六边形的第%d个节点坐标为: (%d,%d)", head->num , head->x, head->y);
head = head + 1;
}
}
}
int main() {
int max = 6;
Hexagon* head = NULL;
int x,y,num;
// 创建并添加新的六边形节点
printf("请输入六边形各点的的序号和坐标:");
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex1 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex1);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex2 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex2);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex3 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex3);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex4 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex4);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex5 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex5);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex6 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex6);
print_hexagon(head);
return 0;
}
我个人觉得你思路有问题
//定义六边形结构体
typedef struct hexagon{
int num;
int x;//这里用数组int[6]
int y;//这里用数组int[6]
struct hexagon* next;
}Hexagon;
for(int i = 0; i <= 6; i++)
{
printf("六边形的第%d个节点坐标为: (%d,%d)", head->num , head->x, head->y);
head = head + 1;
}
int main() {
int max = 6;
Hexagon* head = NULL;
Hexagon* temp = head;//保存链表头节点地址,用于最后从此开始遍历
int x,y,num;
for(num = 0; num<max; i++){
int number = 0;
for(number =0; number< 6; number++){
printf("请输入第 %d 个六边形的第 %d 个坐标:", num, number);
scanf("%d %d", &x, &y);
Hexagon* hex = creat_hexagon(num ,x ,y);//创建新的
add_hexagon(&head, hex1);//添加到head后
head = head.next//head走到最新的节点
}
}
head = temp; // 将链表头结点还给head
print_hexagon(head); // 打印,如果上一行不写,这里可以用temp试试,看能不能遍历
/* 创建并添加新的六边形节点
printf("请输入六边形各点的的序号和坐标:");
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex1 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex1);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex2 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex2);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex3 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex3);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex4 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex4);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex5 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex5);
scanf("%d%d%d", &num, &x, &y);
Hexagon* hex6 = creat_hexagon(num ,x ,y);
add_hexagon(&head, hex6);
print_hexagon(head);
*/
return 0;
}
我没有调试你的代码,我这边没有vc6.0,C编译器
1、print_hexagon函数里面应该是head = head->next,而不是+1
2、add_hexagon里面最后一条赋值语句应该是current_hexagon->next = new_hexagon;因为要把new_hexagon塞到链表里面
3、print_hexagon函数for(int i = 0; i <= 6; i++)这里只有6条边,应该改成:for(int i = 0; i < 6; i++)或者for(int i = 1; i <= 6; i++)
你用
你的代码
包裹起来,大伙给你说
– echo_lovely 1年前@echo_lovely: 包裹起来是什么意思呀,截张图吗,我是新人不太了解
– 笨小孩罢了 1年前@笨小孩罢了: https://www.cnblogs.com/echo-lovely/p/16215739.html markdown语法,github,博客园,csdn写博客都会用到代码块,那个就是代码块
– echo_lovely 1年前@笨小孩罢了: @笨小孩罢了: 我只是让你把你的代码用代码块包裹下,这样大家可以复制,不用调整缩进
– echo_lovely 1年前@echo_lovely: 好的,谢谢指导,我去看看
– 笨小孩罢了 1年前@echo_lovely你好,下了个软件才发现是调试用的,请问是这样吗?
– 笨小孩罢了 1年前