本人菜鸟 c++代码出错确不知道哪错了

qq_36629047 2017-11-24 03:34:10
头文件
SingleList.h
#include<iostream>
using namespace std;


//单链表节点结构
template<class T>
struct SLNode{
T date;//数据域
SLNode<T> *next;//指针域
SLNode()
{
next = NULL;
}//构造函数,只对指针初始化,指针默认指向空

};


//定义单链表类
template <class T>
class SLList{
private:
SLNode<T> *head;

public:
SLList(void);
//构造函数


void Creat();
//构造函数

~SLList();
//析构函数


//const函数不能修改函数里面的数据
//const类型不能调用非const函数


int length()const;
//返回链表长度

//bool Find(int k, T &item)const;
//存取,将链表中第K个节点的值赋值给item

int Search(const T &item)const;
//查找,在链表中查找值为item的节点 并返回在链表中的位置

void Delete(int k);
//删除:删除链表中第k个节点,并把字段值赋值给item

void Insert(int k, const T& item);
//插入:在链表中第k个节点后插入字段值为item的节点

void Display();
//打印

};




cpp文件
SingleList.cpp
#include"SingleList.h"
#include<iostream>

using namespace std;


//构造函数
template <class T>
SLList<T>::SLList()
{
head = new SLNode<T>();
head->next = NULL;

}


//创建一个链表
template <class T>
void SLList<T>::Creat(){
SLNode<T> *p, *q;
int b = 0;
char ch;
p = head;
q = new SLNode<T>();
while (b == 0){
cout << "请输入创建节点的值" << endl;
cin >> q->date;
p -> next = q;
p = q;

cout << "创建新的节点输入a,结束创建节点输入b" << endl;
cin >> ch;
switch (ch)
{
case 'a':
b = 0;
q = new SLNode<T>();
case 'b':
b = 1;
default:
cout << "输入错误,默认退出" << endl;
cin.get();
b = 1;
}
}


}

//单链表析构函数
template <class T>
SLList<T>::~SLList()
{
SLNode<T> *p;
while (head->next != NULL)
{
p = head->next;
delete head;
head = p;
}

}

//返回链表的长度
template <class T>
int SLList<T>::length()const
{
int length = 0;
SLNode *p;
p = head->next;
while (p != NULL)
{
length++;
p = p->next;

}
return length;
}


//查找,在链表中查找值为item的节点,并返回链表中的位置
template<class T>
int SLList<T>::Search(const T&item)const
{
SLNode<T> *p, *q;
int k = 0;
p = head->next;
while (p != NULL)
{
k++;
if (p->date == item)
return k;
p = p->next;

}
cout << "没有这个值的节点" << endl;
cin.get();
return 0;

}

//删除链表中的第k个字节
template <class T>
void SLList<T>::Delete(int k)
{
SLNode *p,*q;
p = head->next;
int i = 1;
if (k > length())
{
cout << "要删除的节点不存在" << endl;
cin.get();
return;
}
else{
if (k == 1)
{
head = p->next;
return;
}
else
{
for (i; i < k - 1; i++)
p = p->next;
q = p->next;
p->next = q->next;
return;

}
}
}


//插入:在链表中第K个字节后插入字段值为item的节点
template<class T>
void SLList<T>::Insert(int k, const T&item)
{
SLNode *q,*p;

q = new SLNode<T>();
q->date = item;
p = head->next;
if (k > length())
{
cout << "输入节点出现错误";
cin.get();
return;
}
else{
for (int i = 1; i < k; i++)
p = p->next;
q->next = p->next;
p->next = q;
return;
}
}

//打印:把链表打印出来
template<class T>
void SLList<T>::Display(){
SLNode<T> *p;
p = header->next;
while (p != NULL)
{
cout << p->date << "->";
p = p->next;

}
return;
}

main.cpp
#include<iostream>
#include"SingleList.h"


using namespace std;

void main(){

SLList<int> *link ;
link = new SLList<int>();
link->Creat();
link->length();
link->Display();
return;
}
以上是代码
...全文
239 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-11-24
  • 打赏
  • 举报
回复
引用 2 楼 cfjtaishan 的回复:
两个问题: 1. 多个函数里(比如Insert函数,Delete函数等,根据提示的错误,找对应的函数)定义的p,q变量类型不对,SLNode *p改成
SLNode<T> *p, q;
2. Display函数里变量名写错,header-->head(应该是后者); 对于这两类错误,有些函数里没有写错,所以写代码还需要细心;
SLNode<T> *p, *q
q前不能少*,因为q是指针。
自信男孩 2017-11-24
  • 打赏
  • 举报
回复
两个问题: 1. 多个函数里(比如Insert函数,Delete函数等,根据提示的错误,找对应的函数)定义的p,q变量类型不对,SLNode *p改成
SLNode<T> *p, q;
2. Display函数里变量名写错,header-->head(应该是后者); 对于这两类错误,有些函数里没有写错,所以写代码还需要细心;
FoolCarpe 2017-11-24
  • 打赏
  • 举报
回复
明显的名字写错 SLNode *p 改成 SLNode<T> *p header 改成 head 逻辑对不对,就需要你自己看了

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧