Advertisement
haquaa

Untitled

Jan 1st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace LinkedList
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Stack stk = new Stack();
  13.  
  14.             stk.add(5);
  15.             stk.add(4);
  16.             stk.add(2);
  17.             stk.add(7);
  18.  
  19.             while (!stk.empty())
  20.             {
  21.                 Console.WriteLine(stk.top());
  22.                 stk.pop();
  23.             }
  24.  
  25.             Console.WriteLine(stk.size());
  26.         }
  27.     }
  28.  
  29.     class Node
  30.     {
  31.         public int info;
  32.         public Node next;
  33.  
  34.         public Node(int val)
  35.         {
  36.             info = val;
  37.             next = null;
  38.         }
  39.     }
  40.  
  41.     class Stack
  42.     {
  43.         Node start = null;
  44.  
  45.         public void add(int val)
  46.         {
  47.             Node tmp = new Node(val);
  48.  
  49.             if (start == null)
  50.             {
  51.                 start = tmp;
  52.                 return;
  53.             }
  54.  
  55.             tmp.next = start;
  56.             start = tmp;
  57.         }
  58.  
  59.         public bool empty()
  60.         {
  61.             return (start == null);
  62.         }
  63.  
  64.         public void pop()
  65.         {
  66.             if (start == null)
  67.             {
  68.                 Console.WriteLine("Stack is empty!");
  69.                 return;
  70.             }
  71.             start = start.next;
  72.         }
  73.  
  74.         public int top()
  75.         {
  76.             return start.info;
  77.         }
  78.  
  79.         public int size()
  80.         {
  81.             int cnt = 0;
  82.             Node q = start;
  83.             while (q != null)
  84.             {
  85.                 q = q.next;
  86.                 cnt++;
  87.             }
  88.  
  89.             return cnt;
  90.         }
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement