[Dev-C++] my delete function in my linked list
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: chromatonus 7. <chr...@ho...> - 2001-11-10 00:01:59
|
hi, my delete function (deleting a node with a user-entered number, no
repetitions in the list) doesn't work and i can't find what's wrong. 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, int info);
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 = NULL;
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: cout<< "number to delete? ";
cin>> info;
delete_from(list, info);
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;
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
|