change operation on stack

i m learning algorithm for changing Ith element of stack
so change algorithm is
CHANGE(S,TOP,I)
1 if TOP -I +1 <=0
write('UNDERFLOW');
2 Return (S[TOP -I +1]

so in this algorithm what is I? I think it is index of element that we want to change..so if i have 3 elements in the stack S and i want to to change 3rd element then in 2 step top=3 - 3 +1 i.e 1 but it is last element.
For example my bottom of the stack element is 2 then 3 and then 4 so my TOP stack element is 4 and bottom of the stack element is 2...and bottom element index is 1 then 2 and then 3 so top element have index 3...
so how step 2 works?
Usually you only manipulate the top element of a stack.

the concept of a stack is last in first out (LIFO). See http://en.wikipedia.org/wiki/Stack_%28data_structure%29

1 but it is last element.
Yes, the element on the bottom is the first element that you put on the stack

and bottom element index is 1 then 2 and then 3 so top element have index 3
You may implement it so, but that's not the way it's implemented in that example

so how step 2 works?
The greater the index the older the item returned
becouse the STL stack can only access the top element, this would be the correct version:
1
2
3
4
5
6
7
8
9
template<typename T>
T change(stack<T> s, int i)
{
      for (int j=1; j<i; j++)
      {
            s.pop();
            }
      return s.top();
      }

BUT, this just gives you the ith element! To change it, you need something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename T>
stack<T> change(stack<T> s, int i, T a)
{
      stack<T> temp=s;
      for (int j=0; j<i; j++)
      {
            temp.pop();
            }
      temp.push(a);
      for (int j=1; j<i; j++)
      {
            temp.push(s.top());
            s.pop();
            }
      return temp;
      }

or, even better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template<typename T>
void change(stack<T>& s, int i, T a)
{
      stack<T> temp1=s, temp2;
      for (int j=0; j<i; j++)
      {
           s.pop();
           }
      s.push(a);
      for (int j=1; j<i; j++)
      {
           temp2.push(temp1.top());
           temp1.pop();
           }
      for (int j=1; j<i; j++)
      {
           s.push(temp2.top());
           temp2.pop();
           }
      }

I was coding this all out of my mind, without testing for real, so tell me if i'm wrong somewhere
EDIT: I just tested it, it all works, yay!
Last edited on
code777 what are saying plz explain in detail i can't understand
bump
bump no. 2
if you don't want to give answer then don't but you cant say anything like that
i m beginner so my level is not as you have...i think you don't have answer of my question
Last edited on
Hm? It's just a day...

i m beginner so my level is not as you have
How can I know what your level is?

Well, this (I guess BASIC) program uses an array for the stack.
The index of an array is ascending.

Adding an item to an array means:

(push() in the example of viliml)
1 TOP = TOP + 1
2 S[TOP] = item

the stack logic (remember LIFO?) requires that the first item is on top
hence the index to get an item is calculated from the top which is TOP - I

pop() would look like this:

1 item = S[TOP]
2 TOP = TOP - 1

If that doesn't answer your question then I don't know your issue
The answers on all of your questions in the first post CSharpque:
so in this algorithm what is I?

The index from the top of the item you want to change.
so if i have 3 elements in the stack S and i want to to change 3rd element then in 2 step top=3 - 3 +1 i.e 1 but it is last element.

Yes.
For example my bottom of the stack element is 2 then 3 and then 4 so my TOP stack element is 4 and bottom of the stack element is 2...and bottom element index is 1 then 2 and then 3 so top element have index 3...

Exactly.
so how step 2 works?

It return s the Ith element from the top.
thanks viliml...now i got the point... thanks for your answer
Topic archived. No new replies allowed.