nie-ii-year

lab stuff from undergrad second year.
git clone http://git.hanabi.in/repos/nie-ii-year.git
Log | Files | Refs | LICENSE

commit 5411f4b69440ffe0e10df0c5d120914044a76bd9
parent 67f13e3e3b2a6c7914f7083a5775e7bcf7bf6ec1
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Fri, 27 Oct 2017 00:38:17 +0530

More cloning material
Diffstat:
As3/oops/stack.cpp | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 101 insertions(+), 0 deletions(-)

diff --git a/s3/oops/stack.cpp b/s3/oops/stack.cpp @@ -0,0 +1,101 @@ +#include <iostream> +using namespace std; +class node{ + int data; + node* next; +public: + node(int data){ + this->next = NULL; + this->data = data; + }; + node* prepend(node* h){ + this->next = h; + return this; + }; + node* oldElementBecomesHead(){ + return this->next; + }; + int getCurrentValue(){ + return this->data; + } +}; + +bool isNULL(node*h){ + if(h == NULL) + return true; + return false; +} + +class stack { + node* head; + int input; +public: + stack(){ + head = NULL; + }; + friend bool isNULL(node*); + void operator+(int input){ + node* temp = new node(input); + if(isNULL(temp)){ + std::cout << "Insufficient memory." << std::endl; + return; + } + else { + if(isNULL(head) == true) + head = temp; + else + head = temp->prepend(head); + } + }; + void operator-(){ + if(isNULL(head)) + std::cout << "Stack underflow." << std::endl; + else{ + node *temp = head; + head = head->oldElementBecomesHead(); + std::cout << temp->getCurrentValue() << " has been popped." << std::endl; + delete temp; + } + }; + void display(){ + node* temp = head; + if(isNULL(temp)){ + std::cout << "Stack is empty." << std::endl; + return; + } + else{ + while (temp != NULL) { + std::cout << temp->getCurrentValue() << " "; + temp = temp->oldElementBecomesHead(); + } + std::cout << std::endl; + } + }; +}; + +int main() { + stack s; + int choice, input; + do{ + std::cout << "1. Push 2. Pop 3. Display 4. Quit\nYour choice> "; + std::cin >> choice; + switch (choice) { + case 1: + std::cout << "Enter an element to be pushed: "; + std::cin >> input; + s + input; + break; + case 2: + -s; + break; + case 3: + s.display(); + break; + case 4: + choice = 0; + break; + default: std::cout << "Invalid choice, try again." << std::endl; + } + }while(choice); + return 0; +}