我的程序没有报错,但是运行却没有输出,这是为什么
#include
#define elementype int
#define headatatype char
#define MAXSIZE 20
using namespace std;
typedef struct lnode { //链表的定义
elementype data;
lnode* next;
}*linklist; //定义链表linklist
typedef struct hnode {
headatatype data;
linklist first;
}*headnode;
bool initlist(linklist& l, headnode& e) {//链表初始化
l->next = nullptr;
e->first = l;
cout << "请输入链表信息" << endl;
cin >> e->data;
return true;
}
bool isempty(const headnode& e) { //判断链表是否为空
if (e->first == nullptr)
return true;
else return false;
}
bool destroylist(linklist& l,headnode&e) { //销毁链表
if (isempty(e )) {
cout << "链表为空!" << endl;
return false;
}
while (l) {
auto t = l->next;
delete[]l;
l = t;
}
return true;
}
int getlength(const linklist& l, headnode& e) {//统计链表长度
int a = 0;
lnode *p = l;
if (isempty(e)) {
return 0;
}
while (p->next == nullptr)
{
p = p->next;
a++;
}
return a;
}
bool getelem(const linklist& l,const int&a,elementype &b) {//获取第i个数据
if (a < 0) {
cout << "位置错误" << endl;
return false;
}
lnode* p = l;
for (int i = 1; i < a+1; i++) {
p = p->next;
if (p->next == nullptr) {//??
return false;
}
}
b = p->data;
return true;
}
//按值查找链表(返回下标)
int locateelem(linklist& l, elementype& e) {
lnode* p = l;
int a = 1;
while (p->data != e&&p->next!=nullptr){
p = p->next;
a++;
}
if (p->next == nullptr) {
cout << "链表中没有该元素!" << endl;
return 0;
}
else return a;
}
//按值查找链表(返回指针)
lnode* locateelem_l(linklist& l, elementype& e)
{
lnode* p;
p = l->next;
while (p && p->data != e) {
p = p->next;
}
return p;
}
bool eraselist(linklist& l, const int& a) { //删除元素
if (a < 0) {
cout << "删除位置错误" << endl;
return false;
}
lnode* p = l;
for (int i = 0; i < a - 1; i++) {
l = l->next;
}
if (l->next == nullptr) {
cout << "删除位置错误" << endl;
return false;
}
for (int i = 0; i < a; i++) {
p = p->next;
}
l->next = p->next;
return true;
}
bool insertlist(linklist& l, const int& i, const elementype&e){//插入数据
lnode* p = l;
int j = 0;
for (j = 0; j < i; j++) {
p = p->next;
}
if (!p || i < 0) {
cout << "错误" << endl;
return false;
}
lnode* insert = new lnode; //linklist与linklist是否等价
insert->data = e;
insert->next = p->next;
p->next = insert;
return true;
}
//头插法创建链表
void creatlisthead(linklist& l, const int a,headnode&e){
elementype b;
cout << "请输入数据" << endl;
for (int j = 0; j < a; j++) {
cin >> b;
lnode* p = new lnode;
p->data = b;
p->next = e->first;
e->first = p;
}
}
//尾插法创建链表
void creatlisttail(linklist& l, const int a, headnode& e) {
cout << "请输入数据" << endl;
lnode* r = l;
int m=0;
while (r->next != nullptr) {
r = r->next;
m++;
}
for (int i = 0; i < a; i++) {
linklist p = new lnode;
cin >> p->data;
if (i==0&&m==0){
e->first =p;
p->next = nullptr;
}
else {
r->next = p;
p->next = nullptr;
r = p;
}
}
}
//(没有头节点)
void creatlisttail(linklist& l, const int n) {
lnode* r = l;
for (int i = 0; i < n; i++)
{
lnode* p = new lnode;
cin >> p->data;
p->next = r->next;
r->next = p;
r = r->next;
}
}
//双向链表
typedef struct dulnode { //双向链表的定义
elementype data;
dulnode* prior, * next;
}*dulinklist;
void dinitlist(dulinklist& l) { //双向链表的初始化
l = new dulnode;
l->prior = nullptr;
l->next = nullptr;
}
void dcreatlisthead(dulinklist&l,const int n) {//头插法建立双向链表
for (int i = 0; i < n; i++) {
dulnode* p = new dulnode;
cin >> p->data;
p->prior = l;
p->next = l->next;
l->next = p;
}
}
void dcreatlisttail(dulinklist& l, const int n) {//尾插法建立双向链表
dulnode* r = l;
for (int i = 0; i < n; i++) {
dulnode* p = new dulnode;
cin >> p->data;
p->prior = r;
p->next = r->next;
r->next = p;
r = p;
}
}
bool dulistinsert(dulinklist&l,const elementype m,const int n) {//在位置n前插入一个数据
dulnode* p = l->next;
int j = 1;
for ( j = 1; j < n&&p->next!=nullptr; j++) {
p = p->next;
}
if (j < n || p->next == nullptr) {
return false;
}
dulnode*a = new dulnode;
a->data = m;
a->next = p->next;
p->next = a;
a->prior = p;
p->next->prior = a;
return true;
}
bool dulisterase(dulinklist& l, const int a) {//删除位置a上的节点
dulnode* p = l->next;
for (int i = 1; i < a && p->next != nullptr; i++) {
p = p->next;
}
if (a < 0 || !p->next) {
return false;
}
if (p->next->next == nullptr) { //删除节点在尾部
dulnode* s = p->next;
p->next = nullptr;
delete[]s;
return true;
}
else {
dulnode* q = p->next; //删除节点不在尾部
p->next = q->next;
q->next->prior = q->prior;
delete[]q;
return true;
}
}
int main() {
headnode m;
linklist n;
bool t = initlist(n, m);
if (t) {
cout << "链表初始化成功!" << endl;
}
else cout << "链表初始化失败" << endl;
int a;
cout << "请输入链表数据个数" << endl;
cin >> a;
creatlisthead(n, a, m);
return 0;
}
根本原因在于:你main函数中的两个变量没有申请内存空间,还有就是你没有输出函数!
headnode m = new hnode;
linklist n = new lnode;
//遍历链表输出
这样虽然能解决问题,但是代码不太规范,给你整体修改了一下。
你main函数中只使用了initlist、creatlisthead两个函数啊,只给你改了这两个函数和main函数,而且,你main函数中没有显示链表。运行结果:
initlist函数:
bool initlist(headnode& e) {//链表初始化
e = new hnode; //申请内存空间
//linklist l = new lnode; //申请内存空间
//l->next = nullptr;
e->first = 0;
cout << "请输入链表信息" << endl;
cin >> e->data;
return true;
}
creatlisthead函数:
//头插法创建链表
void creatlisthead(const int a, headnode& e) {
elementype b;
cout << "请输入数据" << endl;
for (int j = 0; j < a; j++) {
cin >> b;
lnode* p = new lnode;
p->data = b;
p->next = e->first;
e->first = p;
}
}
main函数:
int main() {
headnode m;
linklist n;
bool t = initlist(m);
if (t) {
cout << "链表初始化成功!" << endl;
}
else cout << "链表初始化失败" << endl;
int a;
cout << "请输入链表数据个数" << endl;
cin >> a;
creatlisthead(a, m);
//遍历链表
cout << "遍历链表:" << endl;
n = m->first;
while (n)
{
cout << n->data << " ";
n = n->next;
}
return 0;
}
headnode m;
linklist n;
bool t = initlist(n, m);
这里n是个指针,你没有分配空间呢,就在initlist函数里直接给next赋值,崩溃了
贴出下你的输出信息,以及你怎么判断“没有错误”的。
链表初始化成功!
这些信息也没有么?