Jan 24, 2012 at 2:21pm UTC
Hello. I am just trying to delete a list element, But I get a run-time error(don't know the swource):
Here is what I did:
#include <iostream>
using namespace std;
main(){
struct Node{
int data;
Node *next;
}*ptr;
Node *head;
ptr = new Node;
ptr->data = 1;
head = ptr;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 2;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 3;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 4;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 5;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 6;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 7;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 8;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 9;
ptr->next = new Node;
ptr = ptr->next;
ptr->data = 10;
ptr->next = NULL;
for(Node *a = head;a;a = a->next)
{
cout << a->data;
if( a->next)
cout << "->";
if( a->next->data == '8' )
{//deletes node #9
cout << "here";
// a->next = a->next->next;
// a->next->next = NULL;
}
}
cin.get();
return 0;
}
What's wrong with my code?