what is wrong with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Znode {
float value;
struct znode *next;
};
struct Znode *first = NULL;
//assume the list contains the values 23, 4, 5, 6, -13
Znode *current = *first;

//in a separate function. 
if (first->next->value != 13)
     first = first->next;
first->next = currentNOde->next;
delete [] first;
first = currentNode;

can anyone tell me what is wrong with the function? (it's a efford to delete the node containing -13
The obvious error I see is that line 13 is using delete[], when first appears to be a pointer to a single node and probably was allocated with new, and not new[].
what do u mean?
Does this work?

1
2
3
4
current=first;
while(current-<value!=13)
  current=current->next;
delete current; //without [] 
delete []varname;

Is used to delete an array of dynamically allocated data which means unless you are creating a new array it will throw a exception.
Last edited on
struct is not a data type. the keyword 'struct' should only appear once in that bit of code. the very first line is where it's correctly used. every time after that, it should say Znode variableName;. Znode is the data type and it's also case sensitive. youve got a lot of typos in there. i can't imagine it compiles...
try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Znode {
float value;
Znode *next;
};

Znode *first = NULL;

//assume the list contains the values 23, 4, 5, 6, -13
Znode *current = first;

//in a separate function. 
if (first->next->value != 13)
     first = first->next;
first->next = current->next;
delete first;
first = currentNode;
Last edited on
closed account (S6k9GNh0)
prophetjohn, http://en.wikipedia.org/wiki/C_syntax#Incomplete_types

Although it can be removed...

hunkeelin, I'm not going to say much except that if you want my help, make a test case that implements int main(). if you want help, it's easier to do the strenuous parts yourself. This also helps us see what you're truly trying to accomplish.

Quick notes: Your use of delete is wrong. You need a for loop probably.
Last edited on
One more thing.
In line 3 replace 'znode' to 'Znode'.
Topic archived. No new replies allowed.