Switch statement wont break out
May 21, 2014 at 3:45pm UTC
Is there a reason this portion of my switch statement wont break ?
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
case 'P' :
case 'p' :
while ( input != 'r' || input != 'R' ) {
cout << "Print Options\n" ;
cout << "A or a - Print in ascending order\n" ;
cout << "D or d - Print in descending order\n" ;
cout << "H or h - Print the head value\n" ;
cout << "T or t - Print the tail value\n" ;
cout << "R or r - Return to main menu\n" ;
cout << "Input: " ;
cin >> input ;
system("cls" ) ;
switch ( input ) {
case 'A' :
case 'a' :
list_obj->print_list ( list_obj->get_head ( ) ) ;
break ;
case 'D' :
case 'd' :
list_obj->print_reverse_list ( list_obj->get_tail ( ) ) ;
cout << endl ;
break ;
case 'H' :
case 'h' :
if ( list_obj->get_head ( ) != NULL )
cout << list_obj->get_head( )->get_key ( ) << endl ;
break ;
case 'T' :
case 't' :
if ( list_obj->get_tail ( ) != NULL )
cout << list_obj->get_tail( )->get_key ( ) << endl ;
break ;
case 'R' :
case 'r' :
break ;
}
}
break ;
Its a switch within a switch and the second switch is part of the code for one of the cases of the first.
May 21, 2014 at 3:55pm UTC
The while condition on line 3 can never evaluate to false.
May 21, 2014 at 7:16pm UTC
thank you, I actually caught it after I posted this. Now I'm running not a deleteion problem. If I add 1 value ( changing the head from null to !null) then delete that value, I cannot insert any more values into the list. The head pointer just points at the deleted node. I have tried setting the head pointer back to null but it's still an address here's the 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
void dbl_list::delete_node ( int to_delete ) {
node * temp = find_node ( to_delete ) ;
if ( temp == NULL ) {
cout << to_delete << " was not found in the list\n\n" ;
}
else if ( temp->get_key ( ) == to_delete ) {
if ( temp->get_next ( ) == NULL ) {
if ( temp->get_previous ( ) == NULL ) {
set_head ( NULL ) ;
set_tail ( NULL ) ;
}
}
}
else {
return ;
}
Any ideas how to delete the head yet still be able to insert ?
Thanks.
May 21, 2014 at 7:24pm UTC
Also, sometimes it will not even delete the head. I call the delete function and it will not crash but when you print the list the value still remains in the head.
May 21, 2014 at 9:10pm UTC
I'm guessing node is a struct? What does it look like?
May 21, 2014 at 9:13pm UTC
1 2 3 4 5
int key ;
node * previous ;
node * next ;
node * head ;
I decided to just make it the private data of the class instead of a struct.
May 21, 2014 at 9:20pm UTC
What is 'node', and what does your insert function look like?
May 21, 2014 at 9:41pm UTC
node is the name of the class
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
void dbl_list::add_node ( int to_add ) {
node * temp = get_head ( ) ;
node * new_node = new node ( ) ;
if ( get_head ( ) == NULL && get_tail ( ) == NULL ) {
temp = new node ( ) ;
temp->set_key ( to_add ) ;
set_head ( temp ) ;
set_tail ( temp ) ;
temp->set_next ( NULL ) ;
temp->set_previous ( NULL ) ;
}
else {
while ( temp->get_next ( ) != NULL && temp->get_key ( ) < to_add ) {
temp = temp->get_next ( ) ;
}
// ===================================================================================================================
if ( temp->get_next ( ) == NULL ) {
// At the only node in the list
if ( temp->get_previous ( ) == NULL ) {
if ( temp->get_key ( ) > to_add ) {
new_node->set_key ( temp->get_key ( ) ) ;
new_node->set_previous ( temp ) ;
temp->set_next ( new_node ) ;
temp->set_key ( to_add ) ;
}
else {
new_node->set_key ( to_add ) ;
new_node->set_previous ( temp ) ;
temp->set_next ( new_node ) ;
}
}
// At the last element
else if ( temp->get_previous ( ) != NULL ) {
if ( temp->get_key ( ) > to_add ) {
new_node->set_key ( temp->get_key ( ) ) ;
new_node->set_previous ( temp ) ;
temp->set_key ( to_add ) ;
temp->set_next ( new_node ) ;
}
else {
new_node->set_key ( to_add ) ;
new_node->set_previous ( temp ) ;
temp->set_next ( new_node ) ;
}
}
}
// ====================================================================================================================
else if ( temp->get_key ( ) > to_add ) {
if ( temp->get_previous ( ) == NULL && temp->get_next ( ) != NULL ) {
new_node->set_key ( to_add ) ;
new_node->set_next ( temp ) ;
temp->set_previous ( new_node ) ;
set_head ( new_node ) ;
}
else if ( temp->get_previous ( ) != NULL && temp->get_next ( ) == NULL ) {
new_node->set_key ( temp->get_key ( ) ) ;
new_node->set_previous ( temp ) ;
temp->set_next ( new_node ) ;
temp->set_key ( to_add ) ;
}
else if ( temp->get_previous( )->get_key ( ) < to_add ) {
new_node->set_key ( to_add ) ;
new_node->set_next ( temp ) ;
new_node->set_previous ( temp->get_previous ( ) ) ;
temp->get_previous()->set_next ( new_node ) ;
temp->set_previous ( new_node ) ;
}
}
}
check_tail ( head ) ;
}
May 21, 2014 at 10:01pm UTC
In the first if statement you set temp to a new node even though you already created a new node new_node. It's not breaking your program, but it is a memory leak.
When you create a new node you should check that it isn't null just to make sure it was actually created.
You could do
new_node->set_key ( to_add );
right after you create new_node since you should be doing it no matter what.
In every case you should have
1 2
new_node->set_previous ( )
new_node->set_next ( )
and
1 2 3 4 5
temp->get_next ( )->set_previous ( )
temp->set_previous ( )
// or
temp->get_previous ( )->set_next ( )
temp->set_next ( )
(I think, this last part is a bit confusing)
Last edited on May 21, 2014 at 10:07pm UTC
May 22, 2014 at 1:19pm UTC
Right, I fixed the new node issue, but I guess I'm still confused on how to delete the head node without it breaking the list so I cannot insert anymore.
Topic archived. No new replies allowed.