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 a6a95716c86092b57a2f7578e988a25641d319fd
parent 50225534d076539939fe147a45023352ebed4f8a
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Fri, 27 Oct 2017 02:02:23 +0530

no different from what I made for stacks, argh!
Diffstat:
As3/oops/list.cpp | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 98 insertions(+), 0 deletions(-)

diff --git a/s3/oops/list.cpp b/s3/oops/list.cpp @@ -0,0 +1,98 @@ +#include <iostream> + +class node{ + int data; + node* next; +public: + node(int data){ + this->next = NULL; + this->data = data; + }; + node* updateHead(node* h){ + this->next = h; + return this; + }; + node* updateNode(){ + return this->next; + }; + int getValue(){ + return this->data; + }; +}; + +bool isNULL(node*h){ + if(h == NULL) + return true; + return false; +} + +class list{ + node* head; +public: + list(){head = NULL;}; + friend bool isNULL(node*); + void prepend(int x){ + node* temp = new node(x); + if(temp == NULL){ + std::cout << "Insufficient memory." << '\n'; + return; + } + else{ + if(isNULL(head)) + head = temp; + else + head = temp->updateHead(head); + } + }; + void remove(){ + if(isNULL(head)){ + std::cout << "List is empty." << '\n'; + return; + } + else{ + node*temp = head; + head = head->updateNode(); + std::cout << temp->getValue() << " has been deleted.\n"; + delete temp; + } + }; + void display(){ + node*temp = head; + if(isNULL(temp)){ + std::cout << "List is empty." << '\n'; + return; + } + else + for(int i = 1; temp != NULL; i++, temp = temp->updateNode()) + std::cout << i << ". " << temp->getValue() << '\n'; + }; +}; + +int main(){ + list l; + int choice; + std::cout << "1. Enter 2. Delete 3. Print 4. Exit" << '\n'; + do{ + std::cout << "Your choice> "; + std::cin >> choice; + switch (choice) { + case 1: + std::cout << "Enter element to be prepended: " << '\n'; + std::cin >> choice; + l.prepend(choice); + choice = 1; + break; + case 2: + l.remove(); + break; + case 3: + l.display(); + break; + case 4: + choice = 0; + break; + default: std::cout << "Invalid input, try again." << '\n'; + } + }while(choice); + return 0; +}