C# program to implement Double Stack using structure

Here, we are going to learn how to implement Double Stack using structure in C#?
Submitted by Nidhi, on November 05, 2020 [Last updated : March 27, 2023]

Double Stack Using Structure

Here, we will implement a double-stack using structure; we can implement two stacks inside a single array using structure.

Double Stack Implementation Using Structure in C#

The source code to implement a Double Stack using structure is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//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();
    }
}

Output

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 . . .

Explanation

In the above program, we created a structure DoubleStack that contain three data members array of elements, "top1", "top2", and MAX.

The Stack structure contains PushStack1(), PushStack2(), PopStack1() and PopStack2 methods.

The PushStack1() method is used to insert the item into stack1 from the start of the array. The PushStack2() method is used to insert the item into stack2 from the end of the array.

The PopStack1() method is used to remove an element from the stack1.The PopStack2() method is used to remove an element from the stack2.

Now look to the Demo class, the Demo class contains the Main() method. The Main() method is the entry point for the program. Here, we created the reference of DoubleStack structure and perform Push and Pop operations on the double stack.

C# Data Structure Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.