Problem with an else in a circular linked list

Hello,

I wrote a code for a circular linked list however I have a problem with an else condition. In my "delete node" part (which I give it below) my compiler does not accept the else (I marked in the code I provided which else is it) I put and states " Expected a statement! ". Please, any piece of advice would be appreciated.

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
  void deletenode(int k) {          
     node* ptr = nodeExists(k);   
        if (ptr == NULL) {   
            cout << "No node exists with key value of : " << k << endl;
        }
        else {        
            if (ptr == head) {   
                if (head->next == NULL) {   
                    head = NULL;   
                    cout << "Head node unlinked! List is empty!";  
                }
                else {   
                    node* sptr = head; 
                    while (sptr->next != head) {
                        sptr = sptr->next;
                    }
                    sptr->next = head->next;   
                    head = head->next;    
                    cout << "Node unlinked with keys value: " << k << endl;    
                }
            }
        }
        else      // Here is the problem
        {   
           node* temp = NULL;  
           node* prevptr = head;   
           node* currentptr = head->next;   
           while (currentptr != NULL) {    
               if (currentptr->key == k) {   
                   temp = currentptr;   
                   currentptr = NULL;   
               }
               else {   
                   prevptr = prevptr->next;  
                   currentptr = currentptr->next;   
               }
           }
           prevptr->next = temp->next;
           cout << "Node unlinked with keys value :" << k << endl;
        }
    }
Last edited on
the problem is partially book formatting :)
if you fix the {} to align, rather than the compact textbook style, you can quickly see that there is no if for that offending else.
you have
if(//line 3
else //line 6
... something is missing here..
else //line 23

after using an admittedly obnoxious online formatter to exaggerate what I am saying, see how the 2 elses are at the same level of indent? They were in yours too, but the {} alignment makes it super hard to see which block goes with which statement.
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
 void deletenode(int k)
 {
 	node *ptr = nodeExists(k);
 	if (ptr == NULL)
 	{
 		cout << "No node exists with key value of : " << k << endl;
 	}
 	else
 	{
 		if (ptr == head)
 		{
 			if (head->next == NULL)
 			{
 				head = NULL;
 				cout << "Head node unlinked! List is empty!";
 			}
 			else
 			{
 				node *sptr = head;
 				while (sptr->next != head)
 				{
 					sptr = sptr->next;
 				}
 				sptr->next = head->next;
 				head = head->next;
 				cout << "Node unlinked with keys value: " << k << endl;
 			}
 		}
 	}
 	else	// Here is the problem

 	{
 		node *temp = NULL;
 		node *prevptr = head;
 		node *currentptr = head->next;
 		while (currentptr != NULL)
 		{
 			if (currentptr->key == k)
 			{
 				temp = currentptr;
 				currentptr = NULL;
 			}
 			else
 			{
 				prevptr = prevptr->next;
 				currentptr = currentptr->next;
 			}
 		}
 		prevptr->next = temp->next;
 		cout << "Node unlinked with keys value :" << k << endl;
 	}
 }
Last edited on
It doesn't seem to be related to any previous if statement.

What if statement is it supposed to be connected to?

you can't write:

if(condition)
{
code
}

else
{
code
}

else
{
code
}

you can write the following if you'd like:

if(condition)
{
code
}

else if
{
code
}

else
{
code
}
Last edited on
Thank you guys. I corrected the "else" of the 6th line to "else if" and now it is ok. Thanks again!
note that else if is just cramming 2 statements on one line, it is common to do that in c++.
That is, "else if" isnt special in c++ (it is in some languages), its just another if inside the else block, written in a friendly to humans way.
Topic archived. No new replies allowed.