Showing posts with label Implementing Stack using Linked List. Show all posts
Showing posts with label Implementing Stack using Linked List. Show all posts

Thursday, December 19, 2019

Implementing Stack using Linked List

QUESTION-

You need to implement a Stack class using linked list. The data members should be private. Implement the following public functions :

1. Constructor -

Initialises the data member (i.e. head to null).

2. push :

This function should take one argument of type int and has return type void. This function should insert an element in the stack. Time complexity should be O(1).

3. pop :

This function takes no input arguments and has return type int. This should removes the last element which is entered and return that element as an answer. Return 0 if stack is empty.

Time complexity should be O(1).

4. top :

This function takes no input arguments and has return type int. This should return the last element which is entered and return that element as an answer. Return 0 if stack is empty.

Time complexity should be O(1).

5. size :

Return the size of stack i.e. count of elements which are present ins stack right now. Time complexity should be O(1).

6. isEmpty :

Checks if the stack is empty or not. Return true or false.


SOURCE CODE-
#include<iostream>
using namespace std;

class Node {
    public :
    int data;
    Node *next;
    
    Node(int data) {
        this -> data = data;
        next = NULL;
    }
};

class Stack {
    Node *head;
    int size; // number of elements present in stack
    
    public :
    
    Stack() {
      size=0;
      head=NULL;
    }
    
    int getSize() {
        return size;
    }
    
    bool isEmpty() {
        return size==-1;
    }
    
    void push(int element) {
        Node* current=new Node(element);
      size++;
      current->next=head;
      head=current;
    }
    
    int pop() {
        // Return 0 if stack is empty. Don't display any other message
      if(head==NULL)
        return 0;
      size--;
      int data=head->data;
        head=head->next;
      return data;
    }
    
    int top() {
        // Return 0 if stack is empty. Don't display any other message
      if(head==NULL)
        return 0;
        return head->data;
    } 
};

#include "Stack.h"
int main() {
    
    Stack st;
    
    int choice;
    cin >> choice;
    int input;
    
    while (choice !=-1) {
        if(choice == 1) {
            cin >> input;
            st.push(input);
        }
        else if(choice == 2) {
            int ans = st.pop();
            if(ans != 0) {
                cout << ans << endl;
            }
            else {
                cout << "-1" << endl;
            }
        }
        else if(choice == 3) {
            int ans = st.top();
            if(ans != 0) {
                cout << ans << endl;
            }
            else {
                cout << "-1" << endl;
            }
        }
        else if(choice == 4) {
            cout << st.getSize() << endl;
        }
        else if(choice == 5) {
            cout << st.isEmpty() << endl;
        }
        cin >> choice;
    }
    
}

INPUT-
1 3 1 4 1 63 1 21 1 9 2 3 1 7 2 4 2 2 3 1 799 1 33 1 35 3 2 2 1 97 2 3 2 1 22 1 999 1 23 1 98 2 2 5 3 -1

OUTPUT-
9
21
7
4
21
63
4
35
35
33
97
799
799
98
23
false
999

Arrays in Solidity Programming Language.

Arrays Solidity supports both generic and byte arrays. It supports both fixed size and dynamic arrays. It also supports multidimensional ...