V
V
vreddd2016-05-20 21:50:47
C++ / C#
vreddd, 2016-05-20 21:50:47

C# Implementation of array-based stack methods, what's wrong?

class Program
    {
        static void Main(string[] args)
        {
            int element;
            Random random = new Random();
            Console.WriteLine("введите количество элементов стека?");
            int n;
            n = Convert.ToInt32(Console.ReadLine());

            Stack stk = new Stack(n);
            int head = 0;
            for (int i = 0; i < n; i++)//заполнила стек
            {
                element = random.Next(0, 200);
                if (stk.Init() != head)
                {
                    stk.Push(element);
                }
            }

            for (int i = 0; i < n; i++)
            {
                Console.Write(stk.List() + " ");
            }
                Console.WriteLine();
           head = n;
            int a;
            for (int i = n; i > 0; i--)//новый стек
            {
                if (head % 2 == 0)
                { a = stk.Pop();
                    Console.Write(a - 1 + " ");
                }
                else
                if (head % 2 != 0)
                {
                    a = stk.Pop();
                    Console.Write(a+" ");
                }
                Console.Write(head);
                head--; 
            }
           Console.ReadLine();
        } 
    }

    class Stack
    {
        int[] stack;
        int head = 0;
        public Stack(int size)
        {
            stack = new int[size];
        }
        public void Push(int element)//добавление
        {
            stack[head] = element;
            head++;
        }
        public int Pop()//извлечение
        {
           
            return (stack[head]);
           // head--;
        }
        public int Init()//инициализация
        {
            return stack.Length;
        }
        public int List()//вывод
        {
            head--;
            return (stack[head]);
        }
    }
}

The stack itself outputs, but the new stack, in which each even element is reduced by 1, does not. Please tell me what is wrong. I am 0 in programming, so I hope for understanding and help, thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrDywar Pichugin, 2016-05-20
@Dywar

Use the ready Stack
Read book 1-2.
looked quickly:

...
else  if (head % 2 != 0) //не нужен, достаточно else { .. }
...
public int Pop()//извлечение
        {
           
            return (stack[head]);
           // head--; -< надо снять коммент
        }

G
Georgy Pelageykin, 2016-05-21
@ArXen42

Not so long ago I did a task on the stack on an array, it turned out more or less decently like (you can fasten the implementation of IEnumerable without any problems). Code . According to the assignment, it was required to do this business in VB.Net, although I wrote in C #, the source codes were not preserved, but there is not much difference.
I also recommend looking at the MS / Mono stack implementation, it is essentially the same, but more fancy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question