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
|