Re: [Dev-C++] my delete function in my linked list
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-11-10 01:21:40
|
I'm not entirely sure what you believe is supposed to happen in it as is,
but you aren't keeping track of the node before the one with your info in
it. You need that so you can link around the one you are removing, as it is
a one-way linked list. Also, and I cannot stress this enough, you *MUST*
delete the pointer to the node you have, you don't have to do it in the
function, but as you don't return the node you remove you are losing it's
pointer. Try somethink like:
node delete_from(node *&list, int info)
{
node *back, *current;
current = list;
back = NULL;
while(current->next != NULL)
{
if(current->data == info)
{
// found node
if(back) back->next = current->next;
else list = list->next; // change start pointer
return current; // return old node
}
else
{
// skip to next, but keep a copy of the old node
back = current;
current = current->next;
}
}
}
I'm a bit confused by your use of *&list in the arguments. I assume you you
want a reference to the list pointer so you can modify it, but I would have
thought that the correct notation would be &*list, or maybe &(*list)? I
don't have any experience in doing that as it is probably a bad way of
programming (no offence). Usualy when creating a list you put it in a
container, i.e.
struct list
{
node *start;
};
You can then pass a pointer to the list struct and the fuctions will operate
on it's start member. In some lists you can also keep a pointer to the end,
or maintain number for the size of the list. I bet you are doing this for a
lesson, otherwise it would be easier to use the C++ STL container list.
Jason.
PS: I've not tested the function, I'll leave that to you, but I think it
should work.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Saturday, November 10, 2001 12:01 AM
Subject: [Dev-C++] my delete function in my linked list
> 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
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|