Skip to content

Commit 2e6b71e

Browse files
uru0120Panquesito7
andauthored
feat: Adding switch-case and removing if-else-if (TheAlgorithms#1727)
* Adding switch-case and removing if-else-if * [feat/fix]: Update Co-authored-by: David Leal <[email protected]>
1 parent 7309ef1 commit 2e6b71e

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

data_structures/stack_using_linked_list.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,23 @@ void show() {
3636
int main() {
3737
int ch, x;
3838
do {
39+
std::cout << "\n0. Exit or Ctrl+C";
3940
std::cout << "\n1. Push";
4041
std::cout << "\n2. Pop";
4142
std::cout << "\n3. Print";
42-
std::cout << "\nEnter Your Choice : ";
43+
std::cout << "\nEnter Your Choice: ";
4344
std::cin >> ch;
44-
if (ch == 1) {
45-
std::cout << "\nInsert : ";
46-
std::cin >> x;
47-
push(x);
48-
} else if (ch == 2) {
49-
pop();
50-
} else if (ch == 3) {
51-
show();
45+
switch(ch){
46+
case 0: break;
47+
case 1: std::cout << "\nInsert : ";
48+
std::cin >> x;
49+
push(x);
50+
break;
51+
case 2: pop();
52+
break;
53+
case 3: show();
54+
break;
55+
default: std::cout << "Invalid option!\n"; break;
5256
}
5357
} while (ch != 0);
5458

0 commit comments

Comments
 (0)