Answer the question
In order to leave comments, you need to log in
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]);
}
}
}
Answer the question
In order to leave comments, you need to log in
Use the ready Stack
Read book 1-2.
looked quickly:
...
else if (head % 2 != 0) //не нужен, достаточно else { .. }
...
public int Pop()//извлечение
{
return (stack[head]);
// head--; -< надо снять коммент
}
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 questionAsk a Question
731 491 924 answers to any question