Професионална програма
Loading...
+ Нов въпрос
Itsa_Georgieva avatar Itsa_Georgieva 1 Точки

C# Advanced May 2021 - Iterators and Comparators - Exercise - 03. Stack

3.   Stack

Create your custom stack. You are aware of the Stack's structure. There is a collection to store the elements and two functions (not from the functional programming) - to push an element and to pop it. Keep in mind that the first element, which is popped is the last in the collection. The Push method adds an element at the top of the collection and the Pop method returns the top element and removes it. Push and Pop will be the only commands and they will come in the following format:

"Push {element1}, {element2}, … {elementN}

 Pop

… "

Write your custom implementation of Stack<T> and implement IEnumerable<T> interface. Your implementation of the GetEnumerator() method should follow the rules of the Abstract Data Type – Stack (return the elements in reverse order of adding them to the stack).

Input

  • The input will come from the console as lines of commands.
  • and pop will be the only possible commands, followed by integers for the push command and no another input for the pop command.

Output

  • When you receive END, the input is over. the stack twice and print all elements each on new line.

Constraints

  • The elements in the push command will be valid integers
  • The commands will always be valid always be Push, Pop or END).
  • If Pop command could not be executed as expected e.g. no elements in the stack), print on the console: "No elements".

Examples

Input

Output

Push 1, 2, 3, 4

Pop

Pop

END

2

1

2

1

Push 1, 2, 3, 4

Pop

Push 1

END

1

3

2

1

1

3

2

1

Push 1, 2, 3, 4

Pop

Pop

Pop

Pop

Pop

END

No elements

 

https://pastebin.com/mauZUWmJ - класа Stack

https://pastebin.com/5hZzXrY7 - StartUp

Преписвам буквално кода от лекцията, нулевите тестове минават, както и 1-ви тест, но 3-ти тест ми дава runtime error и 50/100 в judge.

 

 

 

0
C# Advanced 18/06/2021 14:36:28
icowwww avatar icowwww 1577 Точки

Здравей,

В Pop хвърляш throw new ArgumentException("No elements");, а в main метода се опитваш да хванеш catch (AggregateException ae).

Трябва да са еднакви:

Примерна промяна в main метода- ред 37: https://pastebin.com/fxXCUypy

0
Itsa_Georgieva avatar Itsa_Georgieva 1 Точки

Ама разбира се...Благодаря! Сега остава да попрочета за Exception-ите, че нищо не ми говорят blush

0