DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Implement 3 Stacks using single array - C#

Aniruddha Deshpande user avatar by
Aniruddha Deshpande
·
Jun. 01, 12 · · Code Snippet
Like (0)
Save
Tweet
1.50K Views

Join the DZone community and get the full member experience.

Join For Free
3 Stacks are Implemented in this class using 1 array.
/// 
        /// 3 Stacks are Implemented in this class using 1 array
        /// 
        public class StacksUsingSingleArray
        {
            private static int _arrSize = 99;
            private int[] _arr = new int[_arrSize];
            private int[] _stacksAvailableSlots = new int[3];
            private int _stackSize = 0;

            public StacksUsingSingleArray()
            {
                _stackSize = _arrSize / 3;
                _stacksAvailableSlots[0] = 0 * _stackSize;
                _stacksAvailableSlots[1] = 1 * _stackSize; //33
                _stacksAvailableSlots[2] = 2 * _stackSize; //66

            }

            /// 
            /// Push item on stack
            /// 
            /// stack number (ie. 0/1/2)
            /// 
            public void PushOnStack(int stackNum, int data)
            {
                _arr[_stacksAvailableSlots[stackNum]++] = data;
            }

            /// 
            /// Pop item from stack
            /// 
            /// stack number (ie. 0/1/2)
            public int PopStack(int stackNum)
            {
                return _arr[--_stacksAvailableSlots[stackNum]];
            }

            /// 
            /// Gives count of items in a stack
            /// 
            /// stack number (ie. 0/1/2)
            /// 
            public int StackCount(int stackNum)
            {
                return (_stacksAvailableSlots[stackNum] - (_stackSize * stackNum));
            }

            /// 
            /// Printing contents of stack
            /// 
            /// stack number (ie. 0/1/2)
            public void PrintStack(int stackNum)
            {
                for (int i = stackNum * _stackSize; i < _stacksAvailableSlots[stackNum]; i++)
                {
                    Console.WriteLine(_arr[i]);
                }
            }
        }
Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing Between REST and GraphQL
  • Enough Already With ‘Event Streaming’
  • 11 Reasons To Use Selenium for Automation Testing
  • Why Great Money Doesn’t Retain Great Devs w/ Stack Overflow, DataStax & Reprise

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo