Почему при окончании работы программы выводится случайный набор символов?
Почему при окончании работы программы выводится набор символов 00000204C3B3F8A000000204C3B3F84000000204C3B3F3000000000000000010?
С пошаговой откладкой даже не получилось разобраться, откуда эта строчка лишняя берется...
Тот самый для двусвязного списка:
#include <iostream>
using namespace std;
struct node {
public:
char elem ;//point to the previous element
node* next;//point to the next element
node* prev;//point to the first element
public:
node(char el) {
this->elem = el;
this->prev = this->next = NULL;
}
};
class Stack {
public:
node* head;
node* tail;
Stack() : head(NULL), tail(NULL) {};//point to previous and next elements before procedures
bool empty();
void popfront();
void popback();
node* pushfront(char key);
node* pushback(char key);
node* getat(int i);
node* operator [](int i) {
return getat(i);
}
void insert(int i,char c);
void erase(int i);
~Stack() {
while (head != NULL) {
popfront();
}
}
private :
int size=0;
};
bool Stack::empty() {
if (head ==NULL && tail==NULL) {
return true;
}
return false;
}
void Stack::popfront() {
if (head==NULL) {
return;
}
node* ptr = head->next;
cout << &(ptr->prev);
if (ptr!= NULL) {
ptr->prev = NULL;
}
else {
tail = NULL;//or "= ptr"
}
delete head;
head = ptr;
return;
}
void Stack::popback() {
if(tail==NULL){
return;
}
node* ptr = tail->prev;
if (ptr != NULL) {
ptr->next = NULL;
}
else {
head = NULL;
}
delete tail;
tail = ptr;
return;
}
node* Stack::pushfront(char key) {
node* ptr = new node(key);//создали новый объект с временным указателем ptr
ptr->next = head;
if (head != NULL) {
head -> prev=ptr;
}
if (tail == NULL) {
tail = ptr;
}
head = ptr;
size++;
return ptr;
}
node* Stack::pushback(char key) {
node* ptr= new node(key);
ptr->prev = tail;
if (tail != NULL) {
tail->next = ptr;
}
if (head == NULL) {//first object
head = ptr;
}
tail = ptr;//shift of pointer
size++;
return ptr;
}
node* Stack::getat(int i) {
node* ptr = head;// index(head)=0
int n = 0;
while (n != i) {
if (ptr == NULL) {
return NULL;
}
ptr = ptr->next;
n++;
}
return ptr;
}
void Stack::insert(int i,char c) {
node* ptr = getat(i);
if (head==NULL&&tail==NULL) {
pushfront(c);
return;
}
else if (ptr->prev == NULL && ptr->next == NULL && ptr!=NULL) {
pushfront(c);
return;
}
node* pt = new node(c);
pt->next = ptr;
pt->prev = ptr->prev;
ptr->prev = pt;
(pt->prev)->next = pt;
return;
}
void Stack::erase(int i) {
node* ptr = getat(i);
if (ptr == NULL) {
return;
}
if (ptr->prev == NULL && ptr != NULL) {
popfront();
return;
}
if (ptr->next == NULL) {
popback();
return;
}
node* left = ptr->prev;
node* right = ptr->next;
left->next = right;
right->prev = left ;
delete ptr;
}
void main()
{
Stack lst;
cout<<lst.empty()<<endl;
lst.pushback('[');
lst.pushback('(');
lst.pushback(')');
lst.pushback(']');
cout << lst.empty()<<endl;
for (node* ptr = lst.head; ptr != NULL; ptr=ptr->next) {
unsigned char x = ptr->elem;
cout << x << " ";
}
cout << endl;
return;
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Да все просто... По окончании программы (кстати, int main, не void!) вызывается деструктор
~Stack() {
while (head != NULL) {
popfront();
}
}
который вызывает popfront();, а в нем написано
cout << &(ptr->prev);
Все? Вопросов, откуда эта строка, больше нет?