#pragma once
#include<cstdio>
#include<iostream>
using namespace std;
typedef int student;
//链表结构定义
struct node
{
student data;
struct node* next;
};
//链表函数声明
node CreateList(void);
node CreateNode(student);
void InsertNode(node&, student);
void PrintList(node);
//主程序
int main()
{
node head = CreateList();
student stu1 = 1;
InsertNode(head, stu1);
cout << head.next->data << endl;//可以直接打印
PrintList(head);//程序停止运行,这是为什么呢
}
//链表函数定义
node CreateList()//创建头结点
{
node head;
head.next = NULL;
return head;
}
node CreateNode(student data)//创建结点
{
node NewNode;
NewNode.data = data;
NewNode.next = NULL;
return NewNode;
}
void InsertNode(node& head, student data)//插入结点
{
node newnode = CreateNode(data);
newnode.next = head.next;
head.next = &newnode;
}
void PrintList(node Head) //打印链表
{
node* Node = new node;
Node = Head.next;
while (Node)
{
cout << "student:";
cout << Node->data << endl;
cout << endl;
Node = Node->next;
}
}
C++链表打印问题,为什么用我定义的函数打印就出错
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- adgentleman 2021-06-16 15:38关注
可能是你的基础不太牢固,对于堆和栈的理解不太够。我直接给你贴代码吧。
#pragma once #include <cstdio> #include <iostream> using namespace std; typedef int student; //链表结构定义 struct node { student data; struct node *next; }; //链表函数声明 node CreateList(void); node *CreateNode(student data); void InsertNode(node &, student); void PrintList(node); //主程序 int main() { node head = CreateList(); student stu1 = 1; InsertNode(head, stu1); cout << head.next->data << endl; //可以直接打印 PrintList(head); //程序停止运行,这是为什么呢 } //链表函数定义 node CreateList() //创建头结点 { node head; head.next = NULL; return head; } node *CreateNode(student data) //创建结点 { node *NewNode = new node; NewNode->data = data; NewNode->next = NULL; return NewNode; } void InsertNode(node &head, student data) //插入结点 { node *newnode = CreateNode(data); newnode->next = head.next; head.next = newnode; } void PrintList(node Head) //打印链表 { node *Node = Head.next; while (Node) { cout << "student:"; cout << Node->data << endl; cout << endl; Node = Node->next; } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 请把下列每一行代码完整地读懂并注释出来
- ¥15 pycharm运行main文件,显示没有conda环境
- ¥15 易优eyoucms关于二级栏目调用的问题
- ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
- ¥15 为什么eclipse不能再下载了?
- ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
- ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
- ¥15 特定网页无法访问,已排除网页问题
- ¥50 如何将脑的图像投影到颅骨上
- ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境