Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Stacks
- {
- class Stack<T>
- {
- private Node<T> head;
- public Stack()
- {
- head = null;
- }
- public bool isEmpty()
- {
- return head == null;
- }
- public void push(T x)
- {
- head = new Node<T>(x, head);
- }
- public T pop()
- {
- if (isEmpty())
- return default(T);
- T val = head.getValue();
- head = head.getNext();
- return val;
- }
- public T top()
- {
- if (isEmpty())
- return default(T);
- return head.getValue();
- }
- public override string ToString()
- {
- string baseStr = "[";
- while (!isEmpty())
- {
- if (baseStr.Length > 1)
- baseStr += ", ";
- baseStr += pop().ToString();
- }
- baseStr += "]";
- return baseStr;
- }
- }
- }
Add Comment
Please, Sign In to add comment