morry2341

LinkedStack_Part1

Oct 29th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Runtime.Intrinsics.X86;
  4.  
  5. namespace ConsoleApp1
  6. {
  7.     public class StackedLinkedList
  8.     {
  9.         private class Node
  10.         {
  11.             public int data;
  12.  
  13.             public Node link;
  14.         }
  15.  
  16.         Node top;
  17.  
  18.         public StackedLinkedList()
  19.         {
  20.             this.top = null;
  21.         }
  22.  
  23.         public void push(int i)
  24.         {
  25.             Node tmp = new Node();
  26.  
  27.             if (tmp == null)
  28.             {
  29.                 Console.Write("\nStackoverflow");
  30.                 return;
  31.             }
  32.  
  33.             tmp.data = i;
  34.             tmp.link = top;
  35.  
  36.             top = tmp;
  37.  
  38.         }
  39.  
  40.         public bool IsEmpty()
  41.         {
  42.             return top == null;
  43.         }
  44.  
  45.         public int peek()
  46.         {
  47.             if (!IsEmpty())
  48.             {
  49.                 return top.data;
  50.             }
  51.             else
  52.             {
  53.                 Console.WriteLine("empty stack here");
  54.                 return -1;
  55.             }
  56.         }
  57.  
  58.         public void pop()
  59.         {
  60.             if (top == null)
  61.             {
  62.                 Console.Write("\nStack Upperflow");
  63.                 return;
  64.             }
  65.  
  66.             top = top.link;
  67.         }
  68.  
  69.         public void display()
  70.         {
  71.             if (top == null)
  72.             {
  73.                 Console.Write("\nStack Underflow");
  74.                 return;
  75.             }
  76.             else
  77.             {
  78.                 Node tmp = top;
  79.                 while (tmp != null)
  80.                 {
  81.                     Console.Write(tmp.data);
  82.  
  83.                     tmp = tmp.link;
  84.                     if (tmp != null)
  85.                     {
  86.                         Console.Write(" -> ");
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.        
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment