Advertisement
haquaa

Untitled

Jan 12th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Queue
  7. {
  8.     class Queue
  9.     {
  10.         Node front = null;
  11.         Node rear = null;
  12.  
  13.         public void push(int val)
  14.         {
  15.             Node tmp = new Node(val);
  16.  
  17.             if (front == null)
  18.                 front = tmp;
  19.             else
  20.                 rear.next = tmp;
  21.  
  22.             rear = tmp;
  23.         }
  24.  
  25.         public void pop()
  26.         {
  27.             if(front == null)
  28.             {
  29.                 Console.WriteLine("Queue is empty");
  30.             }
  31.             front = front.next;
  32.         }
  33.  
  34.         public int top()
  35.         {
  36.             if (front == null)
  37.             {
  38.                 Console.WriteLine("Queue is empty");
  39.             }
  40.             return front.info;
  41.         }
  42.  
  43.         public bool empty()
  44.         {
  45.             return (front == null);
  46.         }
  47.     }
  48.  
  49.  
  50. }
  51. ___________________________________________________
  52. using System;
  53. using System.Collections.Generic;
  54. using System.Linq;
  55. using System.Text;
  56.  
  57. namespace Stack
  58. {
  59.     class Stack
  60.     {
  61.         Node start = null;
  62.  
  63.         public void push(int val)
  64.         {
  65.             Node tmp = new Node(val);
  66.  
  67.             if (start != null)
  68.             {
  69.                 tmp.next = start;
  70.             }
  71.  
  72.             start = tmp;
  73.         }
  74.  
  75.         public void pop()
  76.         {
  77.             if (start == null)
  78.             {
  79.                 return;
  80.             }
  81.  
  82.             start = start.next;
  83.         }
  84.  
  85.         public int top()
  86.         {
  87.             if (start == null)
  88.             {
  89.                 return 0;
  90.             }
  91.  
  92.             return start.info;
  93.         }
  94.  
  95.         public bool empty()
  96.         {
  97.             return (start == null);
  98.         }
  99.  
  100.     }
  101. }
  102. __________________________
  103. using System;
  104. using System.Collections.Generic;
  105. using System.Linq;
  106. using System.Text;
  107.  
  108. namespace QueueArray
  109. {
  110.     class Queue
  111.     {
  112.         int front = -1;
  113.         int rear = -1;
  114.         int[] arr = new int[2000];
  115.  
  116.         public void push(int val)
  117.         {
  118.             if (rear == arr.Length-1)
  119.             {
  120.                 Console.WriteLine("overflow");
  121.                 return;
  122.             }
  123.  
  124.             if (front == -1)
  125.             {
  126.                 front++;
  127.             }
  128.             arr[++rear] = val;
  129.         }
  130.  
  131.         public void pop()
  132.         {
  133.             if (front == -1 || front > rear)
  134.             {
  135.                 Console.WriteLine("underflow");
  136.             }
  137.  
  138.             front++;
  139.         }
  140.  
  141.         public int top()
  142.         {
  143.             return arr[front];
  144.         }
  145.  
  146.         public bool empty()
  147.         {
  148.             return (front > rear || (front == -1 && rear == -1));
  149.         }
  150.     }
  151. }
  152. _________________________________________________________
  153. using System;
  154. using System.Collections.Generic;
  155. using System.Linq;
  156. using System.Text;
  157.  
  158. namespace StackArray
  159. {
  160.     class Stack
  161.     {
  162.         public int[] arr = new int[5];
  163.         public int top = -1;
  164.  
  165.         public void push(int val)
  166.         {
  167.             if (top  == arr.Length-1)
  168.             {
  169.                 Console.WriteLine("overflow");
  170.                 return;
  171.             }
  172.  
  173.             arr[++top] = val;
  174.         }
  175.  
  176.         public void pop()
  177.         {
  178.             if (top == -1)
  179.             {
  180.                 Console.WriteLine("empty");
  181.                 return;
  182.             }
  183.  
  184.             top--;
  185.         }
  186.  
  187.         public int Top()
  188.         {
  189.             if (top == -1)
  190.             {
  191.                 return 0;
  192.             }
  193.  
  194.             return arr[top];
  195.         }
  196.  
  197.         public bool isempty()
  198.         {
  199.             return (top == -1);
  200.         }
  201.     }
  202. }
  203. _________________________________________
  204.  
  205. using System;
  206. using System.Collections.Generic;
  207. using System.Linq;
  208. using System.Text;
  209.  
  210. namespace Tree
  211. {
  212.     class Tree
  213.     {
  214.         public Node root = null;
  215.  
  216.         public void insert(int val)
  217.         {
  218.             if (root == null)
  219.             {
  220.                 root = new Node(val);
  221.                 return;
  222.             }
  223.  
  224.             root.insertNode(val);
  225.         }
  226.  
  227.         public void PreOrder(Node x)
  228.         {
  229.             if (x == null) return;
  230.             PreOrder(x.left);
  231.  
  232.             PreOrder(x.right);
  233.  
  234.             Console.WriteLine(x.info);
  235.         }
  236.  
  237.         public void print()
  238.         {
  239.             PreOrder(root);
  240.         }
  241.  
  242.         /*public int size = 0;
  243.         public void helper(Node x)
  244.         {
  245.             if (x == null) return;
  246.  
  247.             size++;
  248.             helper(x.left);
  249.             helper(x.right);
  250.         }
  251.  
  252.         public int count(Node Root)
  253.         {
  254.             helper(Root);
  255.             return size;
  256.         }*/
  257.  
  258.     }
  259.  
  260.     class Node
  261.     {
  262.         public int info;
  263.         public Node left;
  264.         public Node right;
  265.  
  266.         public Node(int val)
  267.         {
  268.             info = val;
  269.             left = right = null;
  270.         }
  271.  
  272.         public void insertNode(int val)
  273.         {
  274.             if (val >= info)
  275.             {
  276.                 if (right == null)
  277.                 {
  278.                     right = new Node(val);
  279.                     return;
  280.                 }
  281.                 right.insertNode(val);
  282.             }
  283.             else
  284.             {
  285.                 if (left == null)
  286.                 {
  287.                     left = new Node(val);
  288.                     return;
  289.                 }
  290.                 left.insertNode(val);
  291.             }
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement