stack
This is the form of the question I wrote :
Write a program that reads n integers from the input, places them on a stack, then clears the numbers from the stack and outputs them.
! I want to keep this short and simple, without classes or objects. :vibin:
! But one more thing: use a question mark for the pop function, so that it doesn't throw an error when it returns null .
code :
class Program
{
private int n; private int[] stack; private int top = -1; public Program(int size) { n = size; stack = new int[n]; } public void Push(int k) { if (top == n - 1) { Console.WriteLine("The stack is full."); return; } stack[++top] = k; } public int Pop() { if (top == -1) { Console.WriteLine("The stack is empty.");
return -1; } return stack[top--]; } static void Main() { Console.WriteLine("Enter the number of numbers:"); int n = int.Parse(Console.ReadLine()); Program p = new Program(n); for (int i = 0; i < n; i++) { Console.Write($"عدد {i + 1}: "); int num = int.Parse(Console.ReadLine()); p.Push(num); } Console.WriteLine("Numbers popped from the stack:"); while (p.top >= 0) { Console.WriteLine(p.Pop()); } } }
private int n; private int[] stack; private int top = -1; public Program(int size) { n = size; stack = new int[n]; } public void Push(int k) { if (top == n - 1) { Console.WriteLine("The stack is full."); return; } stack[++top] = k; } public int Pop() { if (top == -1) { Console.WriteLine("The stack is empty.");
return -1; } return stack[top--]; } static void Main() { Console.WriteLine("Enter the number of numbers:"); int n = int.Parse(Console.ReadLine()); Program p = new Program(n); for (int i = 0; i < n; i++) { Console.Write($"عدد {i + 1}: "); int num = int.Parse(Console.ReadLine()); p.Push(num); } Console.WriteLine("Numbers popped from the stack:"); while (p.top >= 0) { Console.WriteLine(p.Pop()); } } }
33 Replies
Ho3in
You're right, but I wrote this myself. I just wanted to see how an expert would answer this question.
Quoted by
<@72080813948153856> from #database (click here)
React with ❌ to remove this embed.
how would i do it? use a
Stack<T>
instead of writing the stack logic myself
but that's probably not what your instructor would want you to doNot by presenting
I want to keep this short and simple, without classes or objectsnot possible. C# is an object oriented language.
Program
is a class
typically you don't create instances of Program
the right way, without using the existing Stack<T>
class, would be to write a Stack
class yourself to encapsulate the logic of creating the stack and pushing/popping
then create an instance of that stack class, read each number and push it on the stack, and then pop them all off like you're doing
i don't understand what you mean by thismaybe if you really wanna shorten it you could've used a
List<T>
as a stack would be a lil weirdNon, le but est le suivant : je veux simuler la pile en la présentant comme : int[] stack = new int[n];
i don't speak french
they need a stack
No, the goal is this: I want to simulate the stack by presenting it as: int[] stack = new int[n];
you're not simulating a stack, you're implementing an actual stack
are you allowed to use Lists or only arrays if you wanna simulate it ?
My teacher wrote with a presentation and said to start with a presentation.
if they're using anything from
System.Collections.Generic
they might as well be using Stack<T>
, but i guess i'm making assumptions about what their instructor has made availableyeah instructions are unclear
i think the language barrier is too big for me to be able to help, as i'm not understanding what you mean. sorry
np
thanks
are you an Arab?
assumed that based on the print statement in your code
I'd say you should implement your own
Stack<T>
class based on an a T[]
, with or without resizing.im Persian iran
ah okay
I haven't learned enough in class yet, so I needed a presentation.
that doesnt mean anything in this context in english, I'm afraid
what do you mean by presentation here ?
like if you can elaborate further, then we could help you better
maybe they mean it as in "appearance" i.e. the assignment is for them to write theyir own stack implementation

would explain the use of "simulate" too
yeah ig
The question given by the teacher is as follows:
Question 1
Write a program that reads n integers from the input, then puts them on a stack, then clears the numbers in the stack and puts them on the output. By presenting
Something like this but this seems to be used with a class or advanced, for example, statics before defining variables I don't want to be
using System;
class Program
{
static int[] stack;
static int top = -1;
static int n;
// Push function to add an element to the stack
static void Push(int k)
{
if (top < n - 1)
{
stack[++top] = k;
}
else
{
Console.WriteLine("The stack is full.");
}
}
// Pop function to remove an element from the stack
static int? Pop()
{
if (top == -1)
{
Console.WriteLine("The stack is empty.");
return null; // Return null instead of throwing an error
}
return stack[top--];
}
static void Main()
{
Console.WriteLine("Enter the number of numbers:");
n = int.Parse(Console.ReadLine());
stack = new int[n];
// Pushing numbers onto the stack
for (int i = 0; i < n; i++)
{
Console.Write($"Enter number {i + 1}: ");
int num = int.Parse(Console.ReadLine());
Push(num);
}
// Popping numbers from the stack and displaying them
Console.WriteLine("Numbers popped from the stack:");
int? poppedNumber;
while ((poppedNumber = Pop()) != null)
{
Console.WriteLine(poppedNumber);
}
}
}
Well if you dont want this then as mg said just use the
Stack<T>
and if something wrong ask your teacher for more clarificationWhat is this? Is it a class?
yes it's a predefined class
https://learn.microsoft.com/en-us/dotnet/api/system.collections.stack?view=net-9.0
Better explanation
https://www.programiz.com/csharp-programming/stack
The code I saw from my professor was like the one I sent, but it didn't use the class you mentioned, nor did it use these statics in the first variable, and it was a little simpler. You can
well without looking at your professor's code we can't compare that with anything