问题描述
我想创建一个函数,通过使用堆栈模型而不是使用类“堆栈”来反转链表。我被困在创建一个函数 stack.push(Node temp)*;
那么如何创建一个充当 stack.push(Node* temp) 的函数?
例如,如果我使用类堆栈,第 5 行 S.Push(temp) 接受 Node* temp。
void Reverse() {
stack <Node*> S;
Node*temp = Head;
while(temp != NULL){
S.push(temp);
temp = temp->next;
}
temp = S.top();
Head = temp;
S.pop();
while(!S.empty()){
temp->next = S.top();
S.pop();
temp = temp->next;
}
temp->next = NULL;
}
我创建的内容:
class stacks{
public:
//constructor
stacks(){
Node* top = NULL;
}
void Push(Node* x){
struct Node* y;
if(y==NULL){
y = x;
}
else
while(y->next == NULL){
y = y->next;
}
y->next = x;
}
void Reverse() {
stacks tempo;
//top is already set as global variable
Node* temp = top;
while(temp != NULL){
tempo.Push(temp);
temp = temp->next;
}
top=tempo.Top();
tempo.Pop();
temp->next = tempo.Top();
while(!tempo.empty())
{
temp = temp->next;
tempo.Pop();
temp->next = tempo.Top();
}
temp->next = NULL;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)