Deleting first element of a linked list

Discussion in 'C++' started by sara, Feb 15, 2011.

  1. sara

    sara Guest

    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;
    }
     
    sara, Feb 15, 2011
    #1
    1. Advertising

  2. On 15 fév, 10:40, sara <> 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;
    >
    > }
     
    Michael Doubez, Feb 15, 2011
    #2
    1. Advertising

  3. sara

    sara Guest

    On Feb 15, 3:08 am, Michael Doubez <> wrote:
    > On 15 fév, 10:40, sara <> 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??
     
    sara, Feb 15, 2011
    #3
  4. sara

    sara Guest

    On Feb 15, 10:00 am, Paavo Helde <> wrote:
    > sara <> wrote in news:ddf699d6-5277-4a83-8356-
    > :
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > On Feb 15, 3:08 am, Michael Doubez <> wrote:
    > >> On 15 f v, 10:40, sara <> 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;
    }
     
    sara, Feb 15, 2011
    #4
  5. On 15 fév, 19:53, sara <> wrote:
    > On Feb 15, 10:00 am, Paavo Helde <> wrote:
    >
    >
    >
    > > sara <> wrote in news:ddf699d6-5277-4a83-8356-
    > > :

    >
    > > > On Feb 15, 3:08 am, Michael Doubez <> wrote:
    > > >> On 15 f v, 10:40, sara <> 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
     
    Michael Doubez, Feb 15, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    1
    Views:
    397
    Richard Bos
    Feb 24, 2004
  2. fool
    Replies:
    14
    Views:
    540
    Barry Schwarz
    Jul 3, 2006
  3. Daniel Vukadinovic

    Deleting linked list

    Daniel Vukadinovic, Apr 12, 2006, in forum: C++
    Replies:
    10
    Views:
    567
    Jaspreet
    Apr 13, 2006
  4. joshd
    Replies:
    12
    Views:
    699
    John Carson
    Oct 2, 2006
  5. crea
    Replies:
    2
    Views:
    440
    Nobody
    Dec 28, 2012
Loading...

Share This Page