|
From: Williams, T. W. <TIM...@sa...> - 2001-11-09 23:08:04
|
In your main(), when you declare your first pointer to a node you need to
set that equal to NULL or 0. The problem you were having is when you add
you were taking whatever address that was in list and just plunking it right
in ->next which isn't so bad if it's a good address but the way it was
written, when you were displaying it, it looped fine, but never got to NULL
and instead just trotted off into memory.
node *list = NULL;
instead of...
node *list;
--tim
> -----Original Message-----
> From: chromatonus 712 [mailto:chr...@ho...]
> Sent: Friday, November 09, 2001 5:44 PM
> To: dev...@li...
> Subject: [Dev-C++] linked lists
>
>
> hi, if you'd like to help, this is a menu of functions doing
> operations on a
> linked list, and i have a runtime error after i performed
> add_front() and
> displayed the list. The correct list was displayed but the
> error message
> came right after. Can you please find out what's wrong? Thank you!
>
> and, is there an equivalent expression for the Borland
> gotoxy() in Dev?
> thanks
>
> #include <iostream.h>
> #include <conio.h>
> #include <stdlib.h>
> #include <math.h>
>
>
> struct node
> {
> int data;
> node *next;
> };
>
>
> //PROTOTYPES
>
> int menu();
> node create();
> node add_front(node *&list, int info);
> //node add_back(node *&list);
> node delete_from(node *&list);
> void display(node *list);
> void exit();
>
>
> int menu()
> {
> int choice;
>
> cout<< "LINKED LIST EDITOR MENU OPTIONS" << "\n";
>
> cout<< "1. Create List" << "\n";
> cout<< "2. Add to Front of List" << "\n";
> cout<< "3. Add to Back of List" << "\n";
> cout<< "4. Display List" << "\n";
> cout<< "5. Delete from List" << "\n";
> cout<< "10. Exit" << "\n" << "\n";
> cout<< "choice? ";
> cin>> choice;
> return(choice);
> }
>
> int main()
> {
> node *list;
> int choice, info;
> choice = menu();
> while( choice != 10 )
> {
> switch( choice )
> {
> case 1: create();
> break;
> case 2:
>
> cout<< "input? ";
> cin>> info;
> add_front(list, info);
> cout<< "choice? ";
> cin>> choice;
> break;
>
> //case 3: add_back(list);
> // break;
> case 4: display(list);
> cout<< "choice? ";
> cin>> choice;
> break;
> case 5: delete_from(list);
> cout<< "choice? ";
> cin>> choice;
> break;
> case 10: exit();
> break;
> choice = menu();
> }
> }
>
> }
>
>
> node create()
> {
> node *list = new node;
> list = NULL;
> }
>
> node add_front(node *&list, int info)
> {
>
> node *head = new node;
> head -> data = info;
> head -> next = list;
> list = head;
>
>
> }
>
> //node add_back(node *&list)
> //{
> // int info;
> // cout<< "input? ";
> // cin>> info;
> // if (list == NULL)
> // {
> // node *tail = new node;
> // tail -> next = NULL;
> // list -> next = tail;
> // }
> // else
> // {
> // node *tail = new node;
> // tail -> next = NULL;
> // tail -> next = tail;
> // }
> //}
>
> node delete_from(node *&list)
> {
> int info;
> node *hold, *jump;
> cout<< "number to delete? ";
> cin>> info;
> hold = list;
> jump = hold -> next;
> while(hold != NULL)
> {
> if(hold -> data == info)
> {
> hold = jump;
> list -> next = jump;
> jump = hold -> next;
> }
> else
> {
> hold = jump;
> jump = hold -> next;
> }
> }
> }
>
> void display(node *list)
> {
> while(list != NULL)
> {
> cout<< list -> data << " ";
> list = list -> next;
> }
> system("PAUSE");
> }
>
> void exit()
> {
> system("PAUSE");
> }
>
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|