#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是指定位置的插入与删除函数。
4 个解决方案
#1
L_size没啥用,可以不看
#2
#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;
}
#3
你Node函数需要添加一个构造函数,以初始化customer成员,Customer类是不没有默认构造函数的
#4
谢谢版主和赵四老师,太粗心大意了我
#1
L_size没啥用,可以不看
#2
#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;
}
#3
你Node函数需要添加一个构造函数,以初始化customer成员,Customer类是不没有默认构造函数的
#4
谢谢版主和赵四老师,太粗心大意了我