What's the error?
hooshdar3 (143)
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?
ciphermagi (694)
Jan 24, 2012 at 2:29pm UTC
First thing that's wrong with it is that it's not indented or in code brackets. I fixed that below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#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;
}
You can get much the same functionality by looking up vectors, lists, deques and iterators, but it looks like the for loop won't activate because you've set a termination condition that's too ambiguous.
hooshdar3 (143)
Jan 31, 2012 at 7:49pm UTC
OK thank you.I corrected the fault, but now the problem is that it doesn't delete a node!
Here is my new code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#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->next != NULL;a = a->next)
{
if ( a->next->data == '7' )
{//deletes node #9
cout << "here" ;
a->next = a->next->next;
delete a->next->next ;
}
}
for (Node *a = head;a->next != NULL;a = a->next)
{
cout << a->data;
if ( a->next)
cout << "->" ;
}
cin.get();
return 0;
}
Topic archived. No new replies allowed.