Thread: [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
|
|
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
|
|
From: Jason S. <mys...@op...> - 2001-11-10 01:43:47
|
Hey everyone,
Why would you use a struct like that over just using a pointer like he has?
I've just been taught linked lists recently in school, have a test monday on them actually...but I don't see the advantage of using the struct over the pointer if you are only going to keep the start pointer. Of course like you said, if you want an end pointer or the size of the struct...
As well, I was taught *& too, p is before r in the alphabet was the little way to remember it.
Jay
----- Original Message -----
From: Jason Hardman
To: Dev-C++
Sent: Friday, November 09, 2001 8:20 PM
Subject: Re: [Dev-C++] my delete function in my linked list
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.
|
|
From: Jason H. <jas...@bt...> - 2001-11-10 02:26:03
|
The main reason was to avoid the use of *&, I personally believe that =
using reference to a pointer, in main(), is a bad way of doing things. =
Using a container (I believe) is representative of better design, =
although it does look silly in the case of it containing only one =
member. I find it easier to deal with the list that way, and by using =
abstraction and such you can help avoid coding errors. Of course if I =
was using C++ I would create a linked list object and have methods to =
work upon the container.
You could create a global pointer for the list, and by putting it in =
'list' namespace you can avoid exposing it to global scope. I can't =
remember the exact best way of doing it this way off the top of my head =
though. What is currently being done is OK for demonstrating the use of =
a linked list, but what I have mentioned is more applicable for an =
implementation you are more likely to use in a real world application.
I don't use references much myself so I don't know much about thier =
syntax. Something I did read in Stroustrup was that you should avoid =
modifying values passed as arguments and instead use return values, =
because the user may not be aware that you have modified a value they =
passed.
In the end the choice is yours, so use what you want. It may be helpful =
to be aware of the alternatives. My explanation might not be very good =
as I'm having trouble trying to explain what I feel is right. I tend to =
rely on intuition a bit when deciding on how to go about a piece of =
code. Ask your teacher and see what he/she thinks about it?
In the end this is all over the top for what is needed so just ignore =
me. ;)
Jason.
----- Original Message -----=20
From: Jason Silverman=20
To: dev...@li...=20
Sent: Saturday, November 10, 2001 1:43 AM
Subject: Re: [Dev-C++] my delete function in my linked list
Hey everyone,
Why would you use a struct like that over just using a pointer =
like he has?
I've just been taught linked lists recently in school, have a test =
monday on them actually...but I don't see the advantage of using the =
struct over the pointer if you are only going to keep the start pointer. =
Of course like you said, if you want an end pointer or the size of the =
struct...
As well, I was taught *& too, p is before r in the alphabet was =
the little way to remember it.
Jay
----- Original Message -----=20
From: Jason Hardman=20
To: Dev-C++=20
Sent: Friday, November 09, 2001 8:20 PM
Subject: Re: [Dev-C++] my delete function in my linked list
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.
|
|
From: Luigi S. <gi...@li...> - 2001-11-11 14:21:31
|
Hi
AFAIK it is common practice to use pointers instead of references when a
functions needs to change some of its parameters, the reason is the one
pointed out by Jason:
when a routine requires a reference parameter, the user may not think that
it will be modified, on the other hand passing a pointer (of course non
const) stresses the fact that the parameter will be changed.
To make it clearer, one should use: "void swap(int *pa, int *pb);"
instead of "void swap (int &a, int &b);"
Of course there are cases when you are forced to use references:
overloading the stream insertion operator (">>")is one of these; more
often, for efficiency reasons, one finds some parameter passed as a const
referece: this is safe because of the const.
At 02.25 10/11/01 +0000, Jason Hardman wrote:
>The main reason was to avoid the use of *&, I personally believe that
>using reference to a pointer, in main(), is a bad way of doing things. ...
>
--
gisan
gi...@li...
|