LinkedList-

'LinkedList::removeFirst' : not all control paths return a value

what does this error means? below is my 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
#include <stdexcept>
using namespace std;
class node
{
public:
int info;
node *link;
node()
{
link=NULL;
}
node(int info)
{
this->info=info;
link=NULL;
}
};

class LinkedList
{
public:
LinkedList();//constructor
void addFirst(int info);
void addLast(int info);
int removeFirst() ;
void printList();
private:
node *head, *tail;
int size;
};

LinkedList::LinkedList()
{
head=tail=NULL;
size=0;
}
void LinkedList::addFirst(int info)
{
node *newNode=new node(info);
newNode->link = head;
head=newNode;
size++;

if(tail==NULL)
tail=head;
}

void LinkedList::addLast(int info)
{
if(tail==NULL)
{
head=tail=new node(info);
}
else
{
tail->link=new node(info);
}
size++;
}

int LinkedList::removeFirst()
{
if(size==0)
cout<<"no node inside";
else
{
node *temp= head;
head = head->link;
size--;
if(head==NULL)tail=NULL;
int info=temp->info;
delete temp;

return info;
}
}
void LinkedList::printList()
{
node *current=head;
while(current!=NULL)
{
cout<<current->info;
current = current->link;
}
}

#endif 
closed account (D80DSL3A)
The function should return an integer value in all cases.
Yours returns a value only if size != 0.

Add code to return some value even for the case when size = 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int LinkedList::removeFirst()
{
if(size==0)
{
cout<<"no node inside";
return 0;// or choose some other value, but some value should be returned for this case
}
else  // this else is no longer needed, due to the return on line 6.
{
node *temp= head;
head = head->link;
size--;
if(head==NULL)tail=NULL;
int info=temp->info;
delete temp;

return info;
}
Last edited on
Topic archived. No new replies allowed.