Я просто хотел бы поблагодарить Реми Лебо за редактирование этого сообщения для меня.
Мой компилятор выдает мне эту ошибку:
main. cpp : 93: 20: ошибка: недопустимое преобразование из 'int' в 'listnode *' [-fpermissive]
temp -> next = NULL;
Это в методе InsertTail
.
Я не понимаю, в чем проблема. Может кто-нибудь помочь мне решить это, пожалуйста? Все, что мне нужно, это иметь возможность установить значение listnode в Null.
Вот мой файл C ++:
program4.cpp
#include <iostream>
using namespace std;
#undef NULL
const int NULL = 0;
typedef int element;
const element SENTINEL = -1;
element read_element();
class listnode{
public:
element data;
listnode * next;
};
class LList {
private:
listnode * head;
listnode * tail;
public:
LList();
~LList();
void Print();
void InsertHead(element thing);
void InsertTail(element thing);
element DeleteHead();
void ReadForward();
void ReadBackward();
void Clean();
void Steal(LList & Victim);
void Append(LList & Donor);
void Duplicate(LList & Source);
void Reverse();
};
void LList::Print(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, and its elements
// have been displayed
listnode * temp;
temp = head;
while (temp != NULL){
cout << temp -> data << endl;
temp = temp -> next;
}
}
void LList::ReadForward(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of new listnodes containing elements
// given by the user in forward order
element userval;
Clean();
cout << "Enter elements, " << SENTINEL << " to stop: ";
userval = read_element();
while (userval != SENTINEL){
InsertTail(userval);
userval = read_element();
}
}
void LList::InsertTail(element thing){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, except that a
// new listnode containing element thing has been
// inserted at the tail-end of the list
listnode * temp;
temp = new listnode;
temp -> data = thing;
temp -> next = NULL;
if (head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
}
element read_element(){
// PRE: the user must enter a series of zero or
// more non-valid element values, followed
// by a valid element value
//
// POST: all entered non-valid element values will
// be successfully discarded, and the first
// valid element value entered will be
// returned
element userval;
cin >> boolalpha >> userval;
while (! cin.good()){
cin.clear();
cin.ignore(80, '\n');
cout << "Invalid data type, should be an element, "
<< "try again: ";
cin >> boolalpha >> userval;
}
return userval;
}
void LList::ReadBackward(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of new listnodes containing elements
// given by the user in backward order
element userval;
Clean();
cout << "Enter elements, " << SENTINEL << " to stop: ";
userval = read_element();
while (userval != SENTINEL){
InsertHead(userval);
userval = read_element();
}
}
void LList::InsertHead(element thing){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, except that a
// new listnode containing element thing has been
// inserted at the head-end of the list
listnode * temp;
temp = new listnode;
temp -> data = thing;
temp -> next = head;
if (head == NULL)
tail = temp;
else
;
head = temp;
}
void LList::Clean(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid and empty, and all of
// its listnodes have been deleted
while (head != NULL)
DeleteHead();
}
element LList::DeleteHead(){
// PRE: the N. O. LList is valid and not empty
// POST: the N. O. LList is unchanged, except that the
// listnode at the head end of the list has been
// deleted, and its data element has been
// returned
listnode * temp;
element thing;
temp = head;
head = head -> next;
thing = temp -> data;
delete temp;
return thing;
}
LList::LList(){
// PRE: none
// POST: the N. O. LList is valid and empty
head = NULL;
}
LList::~LList(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is valid and empty, and its
// listnodes have been deleted
Clean();
}
void LList::Steal(LList & Victim){
// PRE: the N. O. and Victim LLists are valid
// POST: the Victim LList is valid and empty
// the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of the listnodes originally on the
// Victim LList
Clean();
head = Victim.head;
tail = Victim.tail;
Victim.head = NULL;
}
void LList::Append(LList & Donor){
// PRE: the N. O. and Donor LLists are valid
// POST: the Donor LList is valid and empty
// the N. O. LList is valid, and it now consists
// of its own original listnodes followed by the
// listnodes originally on the Donor LList
if (head != NULL)
tail -> next = Donor.head;
else
head = Donor.head;
if (Donor.head != NULL)
tail = Donor.tail;
else
;
Donor.head = NULL;
}
void LList::Duplicate(LList & Source){
// PRE: the N. O. and Source LLists are valid
// POST: the Source LList is unchanged
// the N. O. LList is valid, all of its previous
// listnodes have been deleted, and it now
// consists of listnodes containing the same
// elements and in the same order as on the
// Source LList
listnode * temp;
Clean();
temp = Source.head;
while (temp != NULL){
InsertTail(temp -> data);
temp = temp -> next;
}
}
void LList::Reverse(){
// PRE: the N. O. LList is valid
// POST: the N. O. LList is unchanged, except its
// elements are in reverse order
listnode * temp;
LList Helper;
temp = head;
while (temp != NULL){
Helper.InsertHead(temp -> data);
temp = temp -> next;
}
Steal(Helper);
}
iny main(){
cout << "creating/constructing LList object L" << endl;
LList L;
cout << "L has been created/constructed" << endl;
cout << "L is calling its print method" << endl;
L.Print();
cout << "L has been printed" << endl;
cout << "L is calling its ReadForward method" << endl;
L.ReadForward();
cout << "L has been read forward" << endl;
cout << "L is calling its Print method" << endl;
L.Print();
cout << "L has been printed" << endl;
cout << "L is calling its ReadBackward method" << endl;
L.ReadBackward();
cout << "L has been read backward" << endl;
cout << "L is calling its Print method" << endl;
L.Print();
cout << "L has been printed" << endl;
cout << "L is calling its Clean method" << endl;
L.Clean();
cout << "L has been cleaned" << endl;
cout << "L is calling its Print method" << endl;
L.Print();
cout << "L has been printed" << endl;
}