forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListLengthEvenOdd.cpp
76 lines (68 loc) · 1.29 KB
/
LinkedListLengthEvenOdd.cpp
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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Node
{
public:
Node* next;
int data;
};
class LinkedList
{
public:
int length;
Node* head;
LinkedList();
~LinkedList();
void add(int data);
void print();
};
LinkedList::LinkedList(){
this->length = 0;
this->head = NULL;
}
LinkedList::~LinkedList(){
cout << "LIST DELETED";
}
void LinkedList::add(int data){
Node* node = new Node();
node->data = data;
node->next = this->head;
this->head = node;
this->length++;
}
void LinkedList::print(){
Node* head = this->head;
int i = 1;
while(head){
cout << i << ": " << head->data << endl;
head = head->next;
i++;
}
}
int main(int argc, char const *argv[])
{
string text;
int x;
LinkedList* list = new LinkedList();
cout << "Enter the list. Enter 'stop' to finish" << endl;
while(true){
cin >> text;
if(text == "stop"){
break;
}
else{
x = stoi(text);
list->add(x);
}
}
cout << "List Length: " << list->length << endl;
if(list->length % 2 == 0){
cout << "The length is even" << endl;
}
else{
cout << "The length is odd" << endl;
}
return 0;
}