Please help me in the below program
Write a class named 'Stack" that has two methods named 'push' and 'pop'. The class has the following members:
Member variables : int array []
int n ------------------------> size of the array
int top
Constructor : to initialize n, array and top.
Member methods : void push(int value) ---> to push or insert value in array at index top.
void pop () ----> to delete value from top of array
void display () ---> to print the array

Urgent help needed....Please help!!!!!!!!!!!

Thanks in advance
Shakeel

You're off to a great start here: two posts pleading for help with absolutely no demonstrated effort to solve the problems.

You do realize you're going to have to actually think about these problems, yes? Post the code you have. Post intelligent questions about the parts that are confusing you.

Hi
This is one way you can think.
In the constructor, set the size of the stack and the top to zero. In the push method, copy all elements in the array to a new array with the size of the old array plus one. Then add the new element to the last place in the array and increment top and size. In the pop method, do the same except you make the new array smaller than the old array and decrement size and top. The display method can just iterate over the whole array and print all element in it.

Hope it helps

Thanks Nicke ..
I would definitely try it.. will let you know
____

Dear Ezzaral...
that was very rude.. I am a newbie... just logged in... anyways.. would definitely put my efforts in the next post....

I'll point you to the prominent announcement at the top of the forum: http://www.daniweb.com/forums/announcement9-2.html

Simply posting your assignment with "Urgent help needed" doesn't show a lot of effort and sounds identical to "Do my homework for me!" and that's just not how things work around here.

dear ezzaral i have tried this program before ...tihs is what i tried

class stack
{
int n;
int array[] = new int[n];
int top;
public stack()
{
n = 10;
int array[]= {1,2,3,4,5,6,7,8,9,10};
top = 2;
}
void push(int value)
{
for(int i = 0; i<=n; i++)
{
if( i == top)
array[i] = value;
}
}
void pop()
{
array[top] = 0;
}
void disp()
{
for (int i = 0; i<array.length; i++)
{
System.out.println(array[i]);
}
}
}

when i compile the program it is compiling but while i m assigning the value to the variables it is giving
array index out of bounds exception
if u will point out the mistake i did i would be very happy
thanx

oh...plzzzzzzzz can any1 help me out wid this?????

oh...plzzzzzzzz can any1 help me out wid this?????

Read the announcement Ezzaral left you along with the links from that link. Also read this.

http://catb.org/esr/faqs/smart-questions.html

One, code tags.

[code=JAVA] // code here

[/code]

Two, format your code so it's readable. Code tags don't help much without indentation.

Three, don't use "plzzzzz", "any1", "wid", "u".

You have a problem in lines 3 and 4. n isn't initialized.

Also you have declared a class-scope variable

int array[] = new int[n];

Then another variable with the same name at method-scope in the constructor.

int array[]= {1,2,3,4,5,6,7,8,9,10};

In the constructor you are modifying the method-scope 'array', not the class-scope one.
When the constructor is finished it's 'array' will be destroyed and your class-scope 'array' will still be empty.

Also another thing:
Outside the constructor you do this:

int n;
int array[] = new int[n];

In that way you set the array to be 0 size, since 'n' is zero. I believe that 'n' should be the argument of the constructor:

int N;
int array[] = null;
int top;
public stack(int size)
{
N = size;
array[]= new int[N]
top = 0;
}

Why do you set 'top' to be 2. It makes no sense. Top ,as you said, should point at the top of the stack. If you initialize it to '0' then top would be the place where the next int should be place, since arrays start form '0'.

Meaning that you don't need to iterate the array at the pop, push methods. Whenever you want to add an int,
put it where the 'top' index shows: array[top] = value; and increase the 'top' so next time when you call push, it will put the next int at the increased 'top'

The opposite thing will happen when you want to pop. You need to remove the "top" element of the stack.
So you will return the top element and decrease 'top' by one. Be VERY careful because 'top' points to where the next element needs to be put. So you need to decide whether
first to return the array[top] and then to decrease, OR
first decrease the 'top' and then return the array[top]

When you pop elements actually you don't remove them form tjhe array. You can't. But since array is private that is not a problem because no one sees what is inside. All they see are pop,push. So by increasing and decreasing the top, you make sure that the next time someone wants to pop, they will get the top element. They will not know that the others elements are there because you also decrease the 'top'. If they want to push they will put it at top overwriting the previous element as if it wasn't there.

So when you want to print the elements don't use: i<array.length; but use 'top' as the limit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.