After tons of test progs trying to get down the concept of doubly linked lists, I have come up with this. It works FINE on Turbo C++, but I ported it to M$ VC++ 6, and did the WinAPI clrscr() thingy and I get no compiler errors, but when I run it, it instantly crashes. Heres the code:
Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
struct node {
int value;
node *prev, *next;
};
class list {
public:
void newnode(int val);
void addnode(int val);
void browse();
void close();
private:
node *head, *tail, *main, *p;
};
void list::newnode(int val) {
head = new node;
head->prev = NULL;
head->next = main;
main->prev = head;
main->value = val;
}
void list::addnode(int val) {
main->next = new node;
main->next->prev = main;
main = main->next;
main->value = val;
main->next = NULL;
}
void list::browse() {
char ch = NULL;
p = head->next->next;
do {
clrscr();
cout << "The value is: " << p->value << "\nType 'f' to go forward\nType 'b' to go backward\nType 'd' to delete\nType 'q' to quit\n> ";
cin >> ch;
if (ch == 'd') {
if (p->prev != 0) {
p->prev->next = p->next;
p = p->prev;
} else {
p->next->prev = 0;
p = p->next;
}
}
if (ch == 'f') {
if (p->next->value != 0) {
p = p->next;
} else {
}
}
if (ch == 'b') {
if (p->prev->value != 0) {
p = p->prev;
} else {
}
}
} while (ch != 'q');
}
void list::close() {
main->next = NULL;
}
void main() {
int x;
list list1;
clrscr();
list1.newnode(NULL);
do {
clrscr();
cout << "Enter value.\nType 0 to quit.\n> ";
cin >> x;
if (x == 0) {
break;
}
list1.addnode(x);
} while (x != 0);
list1.close();
list1.browse();
}
Thanks alot, Scott