Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deleting first element of a linked list

Reply
Thread Tools

Deleting first element of a linked list

 
 
sara
Guest
Posts: n/a
 
      02-15-2011
Hi All,

I am creating a linked list (containing numbers 0 to 10) and want to
delete the first element of the list (0) but the result is not
correct. Could you please help?

Thanks a lot
Sara

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct node
{
node* next;
int data;
};

void PrintList(node *head)
{
while (head)
{
cout<<head->data<<" ";
head=head->next;
}
}

void DeleteNode(node* head,int i)
{
if (!head)
return;

if (head->data==i)
{
node *tmp=head;
head=head->next;
delete tmp;
return;
}
}

int main()
{
node *head=NULL;
for (int i=10; i>=0;i--)
{
node* n=new node;
n->data=i;
n->next=head;
head=n;
}

DeleteNode(head,0);
PrintList(head);

return 0;
}
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      02-15-2011
On 15 fév, 10:40, sara <(E-Mail Removed)> wrote:
> Hi All,
>
> I am creating a linked list (containing numbers 0 to 10) and want to
> delete the first element of the list (0) but the result is not
> correct. Could you please help?
>
> Thanks a lot
> Sara
>
> #include <iostream>
> #include <string>
> #include <fstream>
> #include <sstream>
>
> using namespace std;
>
> struct node
> {
> * * * * node* next;
> * * * * int data;
>
> };
>
> void PrintList(node *head)
> {
> * * * * while (head)
> * * * * {
> * * * * * * * * cout<<head->data<<" ";
> * * * * * * * * head=head->next;
> * * * * }
>
> }
>
> void DeleteNode(node* head,int i)


Here you pass head by value, you should pass it by reference:
void DeleteNode(node*& head,int i)

> {
> * * * * if (!head)
> * * * * * * * * return;
>
> * * * * if (head->data==i)
> * * * * {
> * * * * * * * * node *tmp=head;
> * * * * * * * * head=head->next;
> * * * * * * * * delete tmp;
> * * * * * * * * return;
> * * * * }
>
> }
>
> int main()
> {
> * * * * node *head=NULL;
> * * * * for (int i=10; i>=0;i--)
> * * * * {
> * * * * * * * * node* n=new node;
> * * * * * * * * n->data=i;
> * * * * * * * * n->next=head;
> * * * * * * * * head=n;
> * * * * }
>
> * * * * DeleteNode(head,0);
> * * * * PrintList(head);
>
> * * * * return 0;
>
> }


 
Reply With Quote
 
 
 
 
sara
Guest
Posts: n/a
 
      02-15-2011
On Feb 15, 3:08*am, Michael Doubez <(E-Mail Removed)> wrote:
> On 15 fév, 10:40, sara <(E-Mail Removed)> wrote:
>
>
>
>
>
>
>
>
>
> > Hi All,

>
> > I am creating a linked list (containing numbers 0 to 10) and want to
> > delete the first element of the list (0) but the result is not
> > correct. Could you please help?

>
> > Thanks a lot
> > Sara

>
> > #include <iostream>
> > #include <string>
> > #include <fstream>
> > #include <sstream>

>
> > using namespace std;

>
> > struct node
> > {
> > * * * * node* next;
> > * * * * int data;

>
> > };

>
> > void PrintList(node *head)
> > {
> > * * * * while (head)
> > * * * * {
> > * * * * * * * * cout<<head->data<<" ";
> > * * * * * * * * head=head->next;
> > * * * * }

>
> > }

>
> > void DeleteNode(node* head,int i)

>
> Here you pass head by value, you should pass it by reference:
> void DeleteNode(node*& head,int i)
>
>
>
>
>
>
>
> > {
> > * * * * if (!head)
> > * * * * * * * * return;

>
> > * * * * if (head->data==i)
> > * * * * {
> > * * * * * * * * node *tmp=head;
> > * * * * * * * * head=head->next;
> > * * * * * * * * delete tmp;
> > * * * * * * * * return;
> > * * * * }

>
> > }

>
> > int main()
> > {
> > * * * * node *head=NULL;
> > * * * * for (int i=10; i>=0;i--)
> > * * * * {
> > * * * * * * * * node* n=new node;
> > * * * * * * * * n->data=i;
> > * * * * * * * * n->next=head;
> > * * * * * * * * head=n;
> > * * * * }

>
> > * * * * DeleteNode(head,0);
> > * * * * PrintList(head);

>
> > * * * * return 0;

>
> > }


Thanks but why it can delete a node in the middle if I pass by value??
 
Reply With Quote
 
sara
Guest
Posts: n/a
 
      02-15-2011
On Feb 15, 10:00*am, Paavo Helde <(E-Mail Removed)> wrote:
> sara <(E-Mail Removed)> wrote in news:ddf699d6-5277-4a83-8356-
> (E-Mail Removed):
>
>
>
>
>
>
>
>
>
> > On Feb 15, 3:08 am, Michael Doubez <(E-Mail Removed)> wrote:
> >> On 15 f v, 10:40, sara <(E-Mail Removed)> wrote:

>
> >> > Hi All,

>
> >> > I am creating a linked list (containing numbers 0 to 10) and want to
> >> > delete the first element of the list (0) but the result is not
> >> > correct. Could you please help?

>
> >> > Thanks a lot
> >> > Sara

>
> >> > #include <iostream>
> >> > #include <string>
> >> > #include <fstream>
> >> > #include <sstream>

>
> >> > using namespace std;

>
> >> > struct node
> >> > {
> >> > node* next;
> >> > int data;

>
> >> > };

>
> >> > void PrintList(node *head)
> >> > {
> >> > while (head)
> >> > {
> >> > cout<<head->data<<" ";
> >> > head=head->next;
> >> > }

>
> >> > }

>
> >> > void DeleteNode(node* head,int i)

>
> >> Here you pass head by value, you should pass it by reference:
> >> void DeleteNode(node*& head,int i)

>
> >> > {
> >> > if (!head)
> >> > return;

>
> >> > if (head->data==i)
> >> > {
> >> > node *tmp=head;
> >> > head=head->next;
> >> > delete tmp;
> >> > return;
> >> > }

>
> >> > }

>
> >> > int main()
> >> > {
> >> > node *head=NULL;
> >> > for (int i=10; i>=0;i--)
> >> > {
> >> > node* n=new node;
> >> > n->data=i;
> >> > n->next=head;
> >> > head=n;
> >> > }

>
> >> > DeleteNode(head,0);
> >> > PrintList(head);

>
> >> > return 0;

>
> >> > }

>
> > Thanks but why it can delete a node in the middle if I pass by value??

>
> There was no code in your example deleting in the middle.
>
> The problem with head was that there is a local pointer 'head' in the
> main() function. If you delete the node this pointer points to, you have
> to update this pointer value in main() somehow. There are several ways to
> do that, passing by reference is just one of them.
>
> hth
> Paavo


The following code delete the node in the middle but not at the first!

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct node
{
node* next;
int data;
};

void PrintList(node *head)
{
while (head)
{
cout<<head->data<<" ";
head=head->next;
}
}

void DeleteNode(node* head,int i)
{
if (!head)
return;

if (head->data==i)
{
node *tmp=head;
head=head->next;
delete tmp;
return;
}

while (head->next)
{
if (head->next->data==i)
{
node *tmp=head->next;
head->next=head->next->next;
delete tmp;
return;
}
head=head->next;
}
}

int main()
{
node *head=NULL;
for (int i=10; i>=0;i--)
{
node* n=new node;
n->data=i;
n->next=head;
head=n;
}

DeleteNode(head,0);
PrintList(head);

return 0;
}
 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      02-15-2011
On 15 fév, 19:53, sara <(E-Mail Removed)> wrote:
> On Feb 15, 10:00*am, Paavo Helde <(E-Mail Removed)> wrote:
>
>
>
> > sara <(E-Mail Removed)> wrote in news:ddf699d6-5277-4a83-8356-
> > (E-Mail Removed):

>
> > > On Feb 15, 3:08 am, Michael Doubez <(E-Mail Removed)> wrote:
> > >> On 15 f v, 10:40, sara <(E-Mail Removed)> wrote:

>
> > >> > Hi All,

>
> > >> > I am creating a linked list (containing numbers 0 to 10) and want to
> > >> > delete the first element of the list (0) but the result is not
> > >> > correct. Could you please help?

>
> > >> > Thanks a lot
> > >> > Sara

>
> > >> > #include <iostream>
> > >> > #include <string>
> > >> > #include <fstream>
> > >> > #include <sstream>

>
> > >> > using namespace std;

>
> > >> > struct node
> > >> > {
> > >> > node* next;
> > >> > int data;

>
> > >> > };

>
> > >> > void PrintList(node *head)
> > >> > {
> > >> > while (head)
> > >> > {
> > >> > cout<<head->data<<" ";
> > >> > head=head->next;
> > >> > }

>
> > >> > }

>
> > >> > void DeleteNode(node* head,int i)

>
> > >> Here you pass head by value, you should pass it by reference:
> > >> void DeleteNode(node*& head,int i)

>
> > >> > {
> > >> > if (!head)
> > >> > return;

>
> > >> > if (head->data==i)
> > >> > {
> > >> > node *tmp=head;
> > >> > head=head->next;
> > >> > delete tmp;
> > >> > return;
> > >> > }

>
> > >> > }

>
> > >> > int main()
> > >> > {
> > >> > node *head=NULL;
> > >> > for (int i=10; i>=0;i--)
> > >> > {
> > >> > node* n=new node;
> > >> > n->data=i;
> > >> > n->next=head;
> > >> > head=n;
> > >> > }

>
> > >> > DeleteNode(head,0);
> > >> > PrintList(head);

>
> > >> > return 0;

>
> > >> > }

>
> > > Thanks but why it can delete a node in the middle if I pass by value??

>
> > There was no code in your example deleting in the middle.

>
> > The problem with head was that there is a local pointer 'head' in the
> > main() function. If you delete the node this pointer points to, you have
> > to update this pointer value in main() somehow. There are several ways to
> > do that, passing by reference is just one of them.

>
> > hth
> > Paavo

>
> The following code delete the node in the middle but not at the first!
>
> #include <iostream>
> #include <string>
> #include <fstream>
> #include <sstream>
>
> using namespace std;
>
> struct node
> {
> * * * * node* next;
> * * * * int data;
>
> };
>
> void PrintList(node *head)
> {
> * * * * while (head)
> * * * * {
> * * * * * * * * cout<<head->data<<" ";
> * * * * * * * * head=head->next;
> * * * * }
>
> }
>
> void DeleteNode(node* head,int i)


You forgot the reference.

> {
> * * * * if (!head)
> * * * * * * * * return;


The usual pattern uses pointer pointer:
for( node ** it = &head; *it ; it = &((*it)->next) )
// ... rest is left as an exercise

>
> * * * * if (head->data==i)
> * * * * {
> * * * * * * * * node *tmp=head;
> * * * * * * * * head=head->next;
> * * * * * * * * delete tmp;
> * * * * * * * * return;
> * * * * }
>
> * * * * while (head->next)
> * * * * {
> * * * * * * * * if (head->next->data==i)
> * * * * * * * * {
> * * * * * * * * * * * * node *tmp=head->next;
> * * * * * * * * * * * * head->next=head->next->next;
> * * * * * * * * * * * * delete tmp;
> * * * * * * * * * * * * return;
> * * * * * * * * }
> * * * * * * * * head=head->next;
> * * * * }
>
> }


--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Linked List / Deleting any number of element while enumerating Kevin C Programming 1 02-24-2004 06:57 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM



Advertisments