swapping the first and last element in a stack using c++

▼魔方 西西 提交于 2019-12-11 23:33:53

问题


i want to make a function to take a stack (as array) and for it to return the stack swapping the first element with the last element in the Stack so i'll use a temp stack for the data and i'll use but how will i know when will i reach the end of the stack ?

i've wrote the implementation of stack as an array but i need help with the function swap

void Swap(Stack x)
{
Stack tmp(100);
int top1 = x.pop;

for (int i = 0;; i++)
{
x.pop = tmp.push;
}

}

i know its wrong but i'm not sure any help would be appreciated ,thanks Edit i wrote the function at first this way and found i can't take the parameters

void stack::Swap()
{
Stack tmp(100);
int top1 =  this->pop;

for (int i = 0;; i++)
{
this->pop = tmp.push
}

};

Edit here is a code from an answer

Stack Swap(Stack x){

int mytop,mybottom;

mytop=x.pop();

int tmp[x.length-2],i=0;

while(!x.isEmpty()){

    mybottom=x.pop();

    tmp[i++]=mybottom;


    }

Stack returnIt;

returnIt.push(mytop);

for(i=0;i<=x.length -3;i++){

    returnIt.push(tmp[i]);

    }

returnIt.push(mybottom);

return returnIt;

}

回答1:


IDEA : Store top and bottom of stack in a variable and elements between top and bottom in an array. Now you just push bottom of original stack into new stack and than elements in original order and than finally the top of original stack.

code.

#include <bits/stdc++.h>
using namespace std;
void rev(stack<int>&x){
    int sz=x.size(),mytop,mybottom;
    mytop=x.top();
    x.pop();
    int tmp[sz-1],i=0;
    while(!x.empty()){
        mybottom=x.top();
        tmp[i++]=mybottom;
        x.pop();
        } 
    stack<int> returnIt;
    returnIt.push(mybottom);
    for(i=0;i<=sz-3;i++){
        returnIt.push(tmp[i]);
        }
    returnIt.push(mytop);
    while(!returnIt.empty()){
        int tt=returnIt.top();
        x.push(tt);
        returnIt.pop();
    }
    }
int main() {
    // your code goes here
    stack<int>x;
    x.push(1);
    x.push(2);
    x.push(3);
    x.push(4);
    x.push(5);
    stack<int>y=x;
    cout<<"Before reversing : ";
    while(!y.empty()){
        int tt=y.top();
        cout<<tt;
        y.pop();
    }

    rev(x);
    cout<<"\nAfter reversing : ";
    while(!x.empty()){
        cout<<x.top();
        x.pop();
    }
    return 0;
}



回答2:


I dont know if i really understood your question correctly but it looks like you want the first element of a stack to be the last and the last to be the first. If so, you havent really understood the way how a stack works and when to use it, because a stack is like a stack in real life. For example you go a stack of Pringles (yes the potato chips) in one of the classic cylinders. Now a stack allows you just to access the element on top of the stack (the last object that were added), thats because the stack will crash if you try to take out one of the center. But if you want to get a element in the center you have to take so much elements from the top till you reached that element, but if you doing something like that, you are using a stack wrong! A stack is to collect Objects in order they were added (like a list) but just with access to the top. You can use this for example for a undo function in an editor, there you add all the actions you did onto a stack. Now if you use the undo function, one after the next action can be undone, but if you now do a different action, theres a new element on top of the stack and these you have taken of cant be placed on that again, so they are just broken (You can eat them if you want) (like a time paradoxon: "You cant undo something you havent done and you cant redo something you will never have done").

I hope i helped you understanding the basic job of a stack, but if you have any other question or I miss understood your question, correct me.

But if you really want to do, what you are trying, you have to think about, how you could do something like that in reallife: You have to take every element from the stack, one by one, and then build a new stack with the same order in the middle, but the first index you put on the stack is the first from the old stack.

I never really programmed in C++, but in pseudo code it could look like this:

Stack stack;
List<StackElement> list;
//Converting stack to list for better access
for(int i = 0; i<stack.size;i++)
{
    list.add(stack.top);
    stack.pop;
}
//Add the last element from the list (the last from the stack) on top of the stack (thats now the only object in the stack)
stack.push(list.get(list.size()-1));
list.remove(list.size()-1); //Remove this element
//Save our last element
StackElement lastElement = list.get(0);
list.remove(0);
//Inserting the mid
for(int i = 0; i<list.size;i++)
{
    stack.push(list.get(i));
}
//Now put the last element from the old stack on top of the new
stack.push(lastElement):

I am sorry for my bad english, I hope you understood the basic thing i wanted to say. And the code is just theory, it can be wrong!



来源:https://stackoverflow.com/questions/36188490/swapping-the-first-and-last-element-in-a-stack-using-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!