Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Runtime.Intrinsics.X86;
- namespace ConsoleApp1
- {
- public class StackedLinkedList
- {
- private class Node
- {
- public int data;
- public Node link;
- }
- Node top;
- public StackedLinkedList()
- {
- this.top = null;
- }
- public void push(int i)
- {
- Node tmp = new Node();
- if (tmp == null)
- {
- Console.Write("\nStackoverflow");
- return;
- }
- tmp.data = i;
- tmp.link = top;
- top = tmp;
- }
- public bool IsEmpty()
- {
- return top == null;
- }
- public int peek()
- {
- if (!IsEmpty())
- {
- return top.data;
- }
- else
- {
- Console.WriteLine("empty stack here");
- return -1;
- }
- }
- public void pop()
- {
- if (top == null)
- {
- Console.Write("\nStack Upperflow");
- return;
- }
- top = top.link;
- }
- public void display()
- {
- if (top == null)
- {
- Console.Write("\nStack Underflow");
- return;
- }
- else
- {
- Node tmp = top;
- while (tmp != null)
- {
- Console.Write(tmp.data);
- tmp = tmp.link;
- if (tmp != null)
- {
- Console.Write(" -> ");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment