Помогите ипсравить недочеты

  • Автор темы rar14
  • Дата начала
R

rar14

#1
Вот прога работы с деком. Там есть меню с некоторыми операциями над деком. Она работает. Но есть кое-какие баги. Например, когда удаляешь все элементы в деке, то при попытке посмотреть содержимое дека прога умирает. Протестируйте у себя на компиляторе. Помогите исправить недочеты. Спасибо.


[codebox]#include <iostream>
#include <conio.h>
using namespace std;


struct deque {
char title[20];
char author[20];
int year;
deque *prev;
deque *next;
};

deque* create_deque();
deque* push_left(deque* d);
deque* push_right(deque* d);
void is_clear(deque* d);
deque* delete_right_element(deque* d);
deque* empty_deque(deque* d);
void get_right_element(deque* d);
void print_deque(deque* d);



int main() {

// Ñîçäàíåì äåê ñ ïåðâûì ýëåìåíòîì
deque* deq = create_deque();

char key;
bool flag = true;

BEGIN:

cout << "Press key <1> to push element in left" << "\n";
cout << "Press key <2> to push element in right" << "\n";
cout << "Press key <3> to check out deque on empty" << "\n";
cout << "Press key <4> to delete the right element" << "\n";
cout << "Press key <5> to delete all elements" << "\n";
cout << "Press key <6> to get the right element" << "\n";
cout << "Press key <7> to print deque" << "\n";
cout << "Press key <8> to exit" << "\n\n\n";

while (1 && flag) {
key = getch();
if (key >= 49 && key <= 56) break;
}

switch (key) {
case 49: {
deq = push_left(deq);
break;
}
case 50: {
deq = push_right(deq);
break;
}
case 51: {
is_clear(deq);
break;
}
case 52: {
deq = delete_right_element(deq);
break;
}
case 53: {
deq = empty_deque(deq);
break;
}
case 54: {
get_right_element(deq);
break;
}
case 55: {
print_deque(deq);
break;
}
case 56: exit(0);
}

while (1) {
key = getch();
if (key >= 49 && key <= 56) {
flag = false;
break;
}
}

system("cls");

goto BEGIN;


return 0;
}




deque* create_deque() {
deque* node = new (deque);
strcpy((*node).title, "Yazik Ci++");
strcpy((*node).author, "V.V. Podbelsky");
(*node).year = 2007;
(*node).prev = NULL;
(*node).next = NULL;
return node;
} // END OF FUNCTION

deque* push_left(deque* d) {
deque* node = new (deque);
while ((*d).prev) d = (*d).prev;
cout << "\t" << "PUSH LEFT:" << "\n\n";
cout << "\t" << " Title: "; cin >> (*node).title;
cout << "\t" << " Author: "; cin >> (*node).author;
cout << "\t" << " Year: "; cin >> (*node).year;
cout << "\n\t" << " Data is brought";
(*node).prev = NULL;
(*node).next = d;
(*d).prev = node;
return node;
} // END OF FUNCTION

deque* push_right(deque* d) {
deque* node = new (deque);
while ((*d).next) d = (*d).next;
cout << "\t" << "PUSH RIGHT:" << "\n\n";
cout << "\t" << " Title: "; cin >> (*node).title;
cout << "\t" << " Author: "; cin >> (*node).author;
cout << "\t" << " Year: "; cin >> (*node).year;
cout << "\n\t" << " Data is brought";
(*node).prev = d;
(*node).next = NULL;
(*d).next = node;
return node;
} // END OF FUNCTION

void is_clear(deque* d) {
if (!d) {
cout << "\tTHE DEQUE IS EMPTY";
}
else {
cout << "\tTHE DEQUE IS NOT EMPTY";
}
} // END OF FUNCTION

deque* delete_right_element(deque* d) {
while ((*d).next) d = (*d).next;
deque* tmp = d;
d = (*d).prev;
(*d).next = NULL;
delete tmp;
cout << "\tTHE RIGHT ELEMENT IS DELETED";
return d;
} // END OF FUNCTION

deque* empty_deque(deque* d) {
while ((*d).next) d = (*d).next;
while (d) {
deque* tmp = d;
d = (*d).prev;
delete tmp;
}
cout << "\tALL ELEMENTS IN DEQUE IS DELETED";
return d;
} // END OF FUNCTION

void get_right_element(deque* d) {
while ((*d).next) d = (*d).next;
cout << "\t" << "THE RIGHT ELEMENT:" << "\n\n";
cout << "\t" << " Title: " << (*d).title << "\n";
cout << "\t" << " Author: " << (*d).author << "\n";
cout << "\t" << " Year: " << (*d).year << "\n";
} // END OF FUNCTION

void print_deque(deque* d) {
while ((*d).prev) d = (*d).prev;
cout << "\t" << "PRINT DEQUE:" << "\n\n";
int count = 1;
do {
cout << "\t" << count++ << " > " << "Title: " << (*d).title << "\n";
cout << "\t" << " Author: " << (*d).author << "\n";
cout << "\t" << " Year: " << (*d).year << "\n\n";
d = (*d).next;
} while (d);
} // END OF FUNCTION[/codebox]
 
S
#2
если ты ее удаляешь - то что там смотреть????
там же ссылка на NULL становится

в debug??? или в release???