Deleting first element of a linked list

S

sara

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;
}
 
M

Michael Doubez

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)
 
S

sara

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

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

sara

(e-mail address removed):











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;
}
 
M

Michael Doubez

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top