看C语言数据结构仿写了一个C++的单向链表,有错误,大神帮忙看看

sinat_30330131 2017-05-10 04:07:30
#include<string>
using namespace std;
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
bool OK = 1,ERROR = 0;
class Customer
{
private:
string name;
double cost;
public:
Customer(string nm,double cs);
};
class Linkedlist
{
private:
enum {L_size = 10};
struct Node
{
Customer customer;
struct Node * next;
};
typedef struct Node * Linklist;
Node * front;
Node * head;
Node * now;
int nowsize;
const int maxsize;
public:
Linkedlist();
Linkedlist(int n);
~Linkedlist();
Linkedlist & operator = (const Linkedlist & L) { return * this; }
bool ListInsert (Linklist * L,int i,Customer & e);
bool ListDelete (Linklist * L,int i,Customer & e);
bool ListTailInsert (const Customer & e);
void show();
};
#endif



#include "Linkedlist.h"
#include <cstdlib>
#include <iostream>
using namespace std;
Customer::Customer (string nm,double cs)
{
name = nm;
cost = cs;
}
Linkedlist::Linkedlist():nowsize(0),maxsize(0){}
Linkedlist::Linkedlist(int n):maxsize(n)
{
head = new Node;
head->next = front;
front = NULL;
nowsize = 0;
}
Linkedlist::~Linkedlist()
{
Node * temp;
while (front != NULL)
{
temp = front;
front = front->next;
delete temp;
}
}
bool Linkedlist::ListInsert(Linklist * L,int i,Customer & e)//插入到第i个结点
{
int j = 1;
if( nowsize == maxsize )
{
cout << "Fail in Insert!" << endl;
return ERROR;
}
Linklist p;//链表指针必然从头开始,可以把Node群看成一个地址不连续的数组,而指针必然指向第一个节点。
p = *L;
while(p && j < i)
{
p = p->next;//p寻找到当前的i位置
j++;
}
if (!p || nowsize > i)
{
cout << "Fain in Insert!" << endl;
return ERROR;
}
Node * s = new Node;//创建一个新的节点插入。
s->customer = e;
s->next = p->next;//原来p的后继结点现在变成了s的后继结点。
p->next = s;//p代表之前在i节点的地址,next就是现在要插入的地方。
//这里相当于把新加入的s的前后排好。
now = s;//s是函数里临时变量结束以后会销毁,需要给类里的一个变量。
nowsize++;
return OK;
}
bool Linkedlist::ListDelete(Linklist * L,int i,Customer & e)//删除第i个结点
{
int j = 1;
Linklist p,q;
p = *L;
while (p->next && j < i)
{
p = p->next;
++j;
}//找到第i个结点。
if (!(p->next) || j > i)
{
cout << "Error!" << endl;
return ERROR;
}
q = p->next;
p->next = q->next;
e = q->customer;
delete q;
nowsize--;
return OK;
}
bool Linkedlist::ListTailInsert(const Customer & e)
{
if (nowsize == maxsize)
{
cout << "Fail in Insert!";
return ERROR;
}
Node * add = new Node;
add->customer = e;
add->next = NULL;
nowsize++;
if (front == NULL)
front = add;
return OK;
}


VS编译一直提示:
1>c:\users\administrator\documents\visual studio 2012\projects\链表\链表\linkedlist.cpp(13): error C2512: “Linkedlist::Node”: 没有合适的默认构造函数可用
1>c:\users\administrator\documents\visual studio 2012\projects\链表\链表\linkedlist.cpp(48): error C2512: “Linkedlist::Node”: 没有合适的默认构造函数可用
1>c:\users\administrator\documents\visual studio 2012\projects\链表\链表\linkedlist.cpp(86): error C2512: “Linkedlist::Node”: 没有合适的默认构造函数可用

求救,哪里出了问题!
ListInsert和ListDelete是指定位置的插入与删除函数。
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_30330131 2017-05-12
  • 打赏
  • 举报
回复
谢谢版主和赵四老师,太粗心大意了我
paschen 版主 2017-05-10
  • 打赏
  • 举报
回复
你Node函数需要添加一个构造函数,以初始化customer成员,Customer类是不没有默认构造函数的
赵4老师 2017-05-10
  • 打赏
  • 举报
回复
#include <cstdlib>
#include <iostream>
#include<string>
using namespace std;
bool OK = 1,ERROR = 0;
class Customer
{
private:
	string name;
	double cost;
public:
    Customer();
	Customer(string	nm,double cs);
};
Customer::Customer() {
    return;
}
class Node
{
public:
	Customer customer;
	Node* next;
    Node();
};
Node::Node() {
    return;
}
typedef Node* Linklist;
class Linkedlist
{
private:
	enum {L_size = 10};
	Node * front;
	Node * head;
	Node * now;
	int	nowsize;
	const int maxsize;
public:
	Linkedlist();
	Linkedlist(int n);
	~Linkedlist();
	Linkedlist & operator =	(const Linkedlist &	L) { return	* this;	}
	bool ListInsert	(Linklist *	L,int i,Customer & e);
	bool ListDelete	(Linklist *	L,int i,Customer & e);
	bool ListTailInsert	(const Customer	& e);
	void show();
};

Customer::Customer (string nm,double cs)
{
	name = nm;
	cost = cs;
}
Linkedlist::Linkedlist():nowsize(0),maxsize(0){}
Linkedlist::Linkedlist(int n):maxsize(n)
{
    head = new Node;
	head->next = front;
	front =	NULL;
	nowsize	= 0;
}
Linkedlist::~Linkedlist()
{
	Node * temp;
	while (front !=	NULL)
	{
		temp = front;
		front =	front->next;
		delete temp;
	}
}
bool Linkedlist::ListInsert(Linklist * L,int i,Customer & e)//插入到第i个结点
{
	int	j =	1;
	if(	nowsize	== maxsize )
	{
		cout <<	"Fail in Insert!" << endl;
		return ERROR;
	}
	Linklist p;//链表指针必然从头开始,可以把Node群看成一个地址不连续的数组,而指针必然指向第一个节点。
	p =	*L;
	while(p	&& j < i)
	{
		p =	p->next;//p寻找到当前的i位置
		j++;
	}
	if (!p || nowsize >	i)
	{
		cout <<	"Fain in Insert!" << endl;
		return ERROR;
	}
	Node * s = new Node;//创建一个新的节点插入。
	s->customer	= e;
	s->next	= p->next;//原来p的后继结点现在变成了s的后继结点。
	p->next	= s;//p代表之前在i节点的地址,next就是现在要插入的地方。
	//这里相当于把新加入的s的前后排好。
	now	= s;//s是函数里临时变量结束以后会销毁,需要给类里的一个变量。
	nowsize++;
	return OK;
}
bool Linkedlist::ListDelete(Linklist * L,int i,Customer & e)//删除第i个结点
{
	int	j =	1;
	Linklist p,q;
	p =	*L;
	while (p->next && j	< i)
	{
		p =	p->next;
		++j;
	}//找到第i个结点。
	if (!(p->next) || j	> i)
	{
		cout <<	"Error!" << endl;
		return ERROR;
	}
	q =	p->next;
	p->next	= q->next;
	e =	q->customer;
	delete q;
	nowsize--;
	return OK;
}
bool Linkedlist::ListTailInsert(const Customer & e)
{
	if (nowsize	== maxsize)
	{
		cout <<	"Fail in Insert!";
		return ERROR;
	}
	Node * add = new Node;
	add->customer =	e;
	add->next =	NULL;
	nowsize++;
	if (front == NULL)
		front =	add;
	return OK;
}
int main() {
    Linkedlist L;

    return 0;
}
sinat_30330131 2017-05-10
  • 打赏
  • 举报
回复
L_size没啥用,可以不看

64,690

社区成员

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

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