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