双链表堆栈删除函数不起作用
Doubly Linked List Stack Delete func not working
我已经实现了双链表,以便在堆栈中推送和弹出值。我的弹出功能不起作用。我已经对这个程序进行了几次试运行,所有这些都是在纸上完成的。如有可能,请提供指导。
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
struct node
{
node * prev;
int val;
node * next;
};
class myStack
{
node * first;
node * cur;
node * prev0;
public:
myStack (): first(NULL), cur(NULL), prev0(NULL){}
void push();
void pop();
void noutput();
void output();
~myStack(){}
};
void myStack :: push()
{
cur = new node;
cur->prev = NULL;
cur->next = NULL;
cout<<"Enter the Number to push in Stack :"<<endl;
cin>>cur->val;
cout<<endl;
if(first == NULL)
{
first = prev0 = cur;
}
else
{
prev0->next = cur;
cur->prev = prev0;
prev0 = cur;
}
}
void myStack :: pop()
{
prev0 = cur->prev;
delete cur;
prev0->next = NULL;
cur = prev0;
}
void myStack :: noutput()
{
cur = first;
system("cls");
cout<<setw(70)<<"NODE VIEW"<<endl;
cout<<setw(55)<<"Prev"<<" Cur"<<" Next";
cout<<endl<<endl;
while(cur)
{
cout<<setw(55)<<cur->prev<<" | "<<cur<<" | "<<cur->next<<endl;
cur = cur->next;
}
system("pause");
}
void myStack :: output()
{
cur = first;
system("cls");
cout<<setw(60)<<"STACK VIEW"<<endl<<endl;
while(cur)
{
cout<<setw(55)<<" | "<<cur->val<<endl;
cur = cur->next;
}
system("pause");
}
int main()
{
myStack q;
char op;
int key;
for(int i = 0; i < 1; )
{
system("cls");
cout<<"Press 1 to push value :"<<endl;
cout<<"Press 2 to pop value :"<<endl;
cout<<"Press 3 to Print Node View :"<<endl;
cout<<"Press 4 to Print Stack View :"<<endl;
cout<<"Press esc to exit :"<<endl;
op = _getch();
key = op;
if(key == 49)
{
q.push();
}
else if(key == 50)
{
q.pop();
}
else if(key == 51)
{
q.noutput();
cout<<"nn";
}
else if(key == 52)
{
q.output();
cout<<"nn";
}
else if(key == 27)
i++;
else
{
cout<<"a Invalid Value Enter Again:"<<endl;
system("pause");
}
}
system("pause");
return 0;
}
void myStack :: pop()
{
if((first != null) && (first == prev0))
{
delete first;
first = prev0 = cur = null;
}
else
{
prev0 = cur->prev;
delete cur;
prev0->next = NULL;
cur = prev0;
}
}
相关文章:
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么我的数组双精度函数不起作用?
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ Arduino - 随机函数不起作用
- 为什么我的数组或函数不起作用?
- 从类中的另一个文件请求函数不起作用
- 双链表堆栈删除函数不起作用
- 将 cmake 代码段转换为函数 - 不起作用
- 为什么我的从base64解码的函数不起作用?
- 为什么当我们使用等于'='符号比较器函数时,c ++的内置排序函数不起作用?
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 子类化 STL 容器:范围构造函数不起作用
- 带有此指针的模板类多重继承构造函数不起作用?
- 以下打印树函数不起作用为什么?
- 为什么内存集函数不起作用?
- 调用填充我的主体数组的函数不起作用
- 带有自定义数字的阶乘函数不起作用
- 如何QDialog raise()函数不起作用
- Lambda 表达式闭包函数不起作用
- C++:在没有 lvalue 的情况下调用时复制构造函数不起作用