Это код, чтобы сделать связанный список с 2 с ценностно входом одного пользователя, а другой 7.Создание связанного списка в C++
#include<iostream>
#include<cstdlib>
using namespace std;
class node{
public:
node();
~node();
void printList();
void insert_front(int);
void delete_front();
private:
int data;
node *head;
node *next;
};
node::node()
{
head=NULL;
}
node::~node(){//destructor
cout <<"destructor called";
while(head!= NULL) delete_front() ;
}
void node::delete_front(){
node *h=head;
if(head==NULL)
{
cout<< "Empty List.\n";
return;
}
head = head->next;
delete(h);
}
void node::printList()
{
node *h=head;
cout<< "Printing the list";
while(h!=NULL)
{
cout<< h->data;
cout<< '\n';
h->next= h->next->next;
}
}
void node::insert_front(int value){
node *temp = new node;
temp->data=value;
temp -> next = NULL;
if (head != NULL){
temp->next =head;
}
head= temp;
}
int main()
{
node ListX;
cout<< "enter integer";
int as;
cin>> as;
ListX.insert_front(as);
ListX.insert_front(7);
ListX.printList();
ListX.~node();//call destructor to free objects
return 0;
}
Сообщать об ошибке в этом, как он показывает сообщение об ошибке при компиляции онлайн на http://www.compileonline.com/compile_cpp_online.php и даже на моем ноутбуке.
Так, что не работает? –
Это показывает * что * ошибка? И вы не должны называть деструктора. – chris
не отображается ошибка. Но он постоянно компилируется. Не вызвать деструктора не имело значения. – sidPhoenix