



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Program in C# on stack with fixed array Linked List
Typology: Essays (high school)
1 / 5
This page cannot be seen from the preview
Don't miss anything!




using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Stack { class Stack { private int[] ele; private int top; private int max; public Stack(int size) { ele = new int[size];//Maximum size of Stack top = -1; max = size; }
public void push(int item) { if (top == max - 1) { Console.WriteLine("Stack Overflow"); return; } else { ele[++top] = item; } }
public int pop() { if (top == -1) { Console.WriteLine("Stack is Empty"); return -1; } else { Console.WriteLine("{0} popped from stack ", ele[top]); return ele[top--]; } }
public int size() { if (top == -1) { Console.WriteLine("Stack is Empty"); } else
Console.WriteLine("Number of elements on the stack is ", top); } return -1; }
public void capacity() { Console.WriteLine("Total capacity of stack is {0}", max);
} public void printStack() { if (top == -1) { Console.WriteLine("Stack is Empty"); return; } else { for (int i = 0; i <= top; i++) { Console.WriteLine("{0} pushed into stack", ele[i]); } } } }
// Driver program to test above functions class Program { static void Main() { char choice='Y'; Stack p = new Stack(5); int opt; // to input the choice do { Console.Write("\nHere are the options :\n"); Console.Write("1-Push (Adding a data).\n2-Pop (Taking out a data from last point).\n3-Size of the list (Number of elements present).\n4-Capacity (Tatal size of the array).\n5-Exit.\n"); Console.Write("\nInput your choice :"); opt = Convert.ToInt32(Console.ReadLine());
switch (opt) { case 1: Console.Write("\nEnter a number to add/ push(): "); p.push(Convert.ToInt32(Console.ReadLine())); break;
case 2: p.pop(); break;
case 3: p.size();
Node newNode = new Node(value);
if (top == null)
{
newNode.next = null;
else
{
newNode.next = top;
top = newNode;
Console.WriteLine("{0} pushed to stack", value);
}
internal void pop()
{
if (top == null)
{
Console.WriteLine("Stack Underflow. Deletion not possible");
return;
}
Console.WriteLine("Item popped is {0}", top.data);
top = top.next;
}
internal void peek()
if (top == null)
{
Console.WriteLine("Stack Underflow.");
return;
Console.WriteLine("{0} is on the top of Stack", this.top.data);
}
class Program { static void Main(string[] args) { LinkListStack p = new LinkListStack(); p.push(25); p.push(12); p.pop(); p.peek();
} } }