2013-08-25 4 views
1

Это код, чтобы сделать связанный список с 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 и даже на моем ноутбуке.

+1

Так, что не работает? –

+2

Это показывает * что * ошибка? И вы не должны называть деструктора. – chris

+0

не отображается ошибка. Но он постоянно компилируется. Не вызвать деструктора не имело значения. – sidPhoenix

ответ

3

h->next= h->next->next;

Что вы пытаетесь достичь здесь?

Изменение while петля в void node::printList() к:

while(h!=NULL) 
{ 
    cout<< h->data; 
    cout<< '\n'; 
    h= h->next; 
} 
+0

О, это получилось. Это была ошибка. Благодарю. – sidPhoenix

+2

Теперь вы можете принять это как ответ. – jev

Смежные вопросы