My Linked List App

This is a discussion on My Linked List App within the C++ Programming forums, part of the General Programming Boards category; After tons of test progs trying to get down the concept of doubly linked lists, I have come up with ...

  1. #1
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Angry 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
    -Save the whales. Collect the whole set.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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;
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21