-
My Linked List App
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
-
The problem isn't the clrscr(), it's your list. main doesn't point to anything when it's initialised but you attempt to assign an address to it's prev pointer and an int to it's val. I've changed a few other things, it's not perfect (you haven't assigned the tail pointer and you may want head to be a part of the list rather than an empty node), but works -
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 =NULL;
head->value=val;
main = head;
}
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;
do {
clrscr();
cout << "The value is: ";
if(p==head)
cout << "head";
else
cout << p->value;
cout <<"\nType 'f' to go forward\nType 'b' to go backward\nType 'd' to delete\nType 'q' to quit\n> ";
cin >> ch;
if (ch == 'd'&& p!=head) {
node* temp=p;
if (p->prev != 0 ) {
if(p->next)
p->next->prev = p->prev;
p->prev->next = p->next;
p = p->prev;
delete temp;
} else {
p->next->prev = 0;
p = p->next;
delete temp;
}
}
if (ch == 'f') {
if (p->next != 0) {
p = p->next;
} else {
}
}
if (ch == 'b') {
if (p->prev !=0&&p->prev->value != 0) {
p = p->prev;
} else {
}
}
} while (ch != 'q');
}
void list::close() {
main->next = NULL;
}
int 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();
return 0;
}