C# Program to Implement Double Stack Using Class

There is a lot of confusion C# Program to Implement Double Stack Using Class We are sure this article will give you a clear result. Let’s go into the article.

C# Program to Implement Double Stack Using Class

C# Program to Implement Double Stack Using Class

//C# program to implement Double Stack using class.

using System;

//Declaration of Double Stack
class DoubleStack
{
    int top1;
    int top2;
    int MAX;
    int []ele;

    //Initialization of Double Stack
    public DoubleStack(int size)
    {
        ele  = new int[size];
        top1 = -1;
        top2 = size;
        MAX  = size; 
    }
    
    //Push Operation on Stack1
    public void PushStack1(int item )
    {
        if( top2 == top1 + 1 )
        {
            Console.WriteLine("Stack Overflow Stack1");
            return;
        }
 
        top1++;
        ele[top1] = item;
 
        Console.WriteLine("Inserted item in Stack1 : {0}",item);  
    }

    //Push Operation on Stack2
    public void PushStack2(int item)
    {
        if( top2 == top1 + 1 )
        {
            Console.WriteLine("Stack Overflow Stack2");
            return;
        }
 
        top2--;
        ele[top2] = item;
        Console.WriteLine("Inserted item in Stack2 : {0}",item);  
    }
    
    //Pop Operation on Stack1
    public void PopStack1()
    {
        int item=0;
        if(top1 == -1 )
        {
            Console.WriteLine("Stack Underflow Stack1");
            return;
        }
 
        item = ele[top1--];
        Console.WriteLine("Poped Item: "+item);
    }
    //Pop Operation on Stack2
    public void PopStack2()
    {
        int item=0;
        if( top2 == MAX )
        {
            Console.WriteLine("Stack Underflow Stack2");
            return;
        }
 
        item = ele[top2++];
        Console.WriteLine("Poped Item: "+item);
    }

}

class Demo
{
    static void Main(string[] args)
    {
        DoubleStack S=new DoubleStack(5);

        S.PushStack1(10);
        S.PushStack1(20);
        S.PushStack1(30);
        S.PushStack2(40);
        S.PushStack2(50);
        S.PushStack2(60);

        S.PopStack1();
        S.PopStack1();
        S.PopStack1();

        S.PopStack2();
        S.PopStack2();
        S.PopStack2();

        Console.WriteLine();
    }
}
Inserted item in Stack1 : 10
Inserted item in Stack1 : 20
Inserted item in Stack1 : 30
Inserted item in Stack2 : 40
Inserted item in Stack2 : 50
Stack Overflow Stack2
Poped Item: 30
Poped Item: 20
Poped Item: 10
Poped Item: 50
Poped Item: 40
Stack Underflow Stack2

Press any key to continue . . .

Final Words

We hope the C# Program to Implement Double Stack Using Class article has given good results for your confusion. See you in the next article.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment