C# Program to Implement Double Stack Using Structure

Do you know what information the article is going to tell you ?. C# Program to Implement Double Stack Using Structure We are going to see clearly about this.

C# Program to Implement Double Stack Using Structure

C# Program to Implement Double Stack Using Structure

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

using System;

//Declaration of Double Stack
struct 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

C# Program to Implement Double Stack Using Structure We hope you find the information you are looking for through this article and we hope all your doubts are resolved through this 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