From 2e6b71e20620b4d2bde5009d2da8efe99eabc26c Mon Sep 17 00:00:00 2001 From: Urwashi Kumrawat <76898392+uru0120@users.noreply.github.com> Date: Tue, 12 Oct 2021 23:19:39 +0530 Subject: [PATCH] feat: Adding switch-case and removing if-else-if (#1727) * Adding switch-case and removing if-else-if * [feat/fix]: Update Co-authored-by: David Leal --- data_structures/stack_using_linked_list.cpp | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/data_structures/stack_using_linked_list.cpp b/data_structures/stack_using_linked_list.cpp index 3dcf03f8a..202c5c9b6 100644 --- a/data_structures/stack_using_linked_list.cpp +++ b/data_structures/stack_using_linked_list.cpp @@ -36,19 +36,23 @@ void show() { int main() { int ch, x; do { + std::cout << "\n0. Exit or Ctrl+C"; std::cout << "\n1. Push"; std::cout << "\n2. Pop"; std::cout << "\n3. Print"; - std::cout << "\nEnter Your Choice : "; + std::cout << "\nEnter Your Choice: "; std::cin >> ch; - if (ch == 1) { - std::cout << "\nInsert : "; - std::cin >> x; - push(x); - } else if (ch == 2) { - pop(); - } else if (ch == 3) { - show(); + switch(ch){ + case 0: break; + case 1: std::cout << "\nInsert : "; + std::cin >> x; + push(x); + break; + case 2: pop(); + break; + case 3: show(); + break; + default: std::cout << "Invalid option!\n"; break; } } while (ch != 0);