Advertisement
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;
- namespace LinkedList
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack stk = new Stack();
- stk.add(5);
- stk.add(4);
- stk.add(2);
- stk.add(7);
- while (!stk.empty())
- {
- Console.WriteLine(stk.top());
- stk.pop();
- }
- Console.WriteLine(stk.size());
- }
- }
- class Node
- {
- public int info;
- public Node next;
- public Node(int val)
- {
- info = val;
- next = null;
- }
- }
- class Stack
- {
- Node start = null;
- public void add(int val)
- {
- Node tmp = new Node(val);
- if (start == null)
- {
- start = tmp;
- return;
- }
- tmp.next = start;
- start = tmp;
- }
- public bool empty()
- {
- return (start == null);
- }
- public void pop()
- {
- if (start == null)
- {
- Console.WriteLine("Stack is empty!");
- return;
- }
- start = start.next;
- }
- public int top()
- {
- return start.info;
- }
- public int size()
- {
- int cnt = 0;
- Node q = start;
- while (q != null)
- {
- q = q.next;
- cnt++;
- }
- return cnt;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement