以链表形式无限打印栈
Infinite printing of stack in linked list form
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct node{
int data=0;
node *next=NULL;
};
class linked_stack{
node *top; //top is the head here
int size;
public:
linked_stack(){
size=0;
node *top=NULL;
}
void push(int data){
node *newNode=new node;
newNode->data=data;
newNode->next=top;
top=newNode;
size++;
}
int pop(){
if(top==NULL){
cout<<"UNDERFLOW";
return -1;
}
else{
size--;
node *temp=top;
top=top->next;
temp->next=NULL;
int popped=temp->data;
delete(temp);
return popped;
}
}
void display(){
node *ptr=top;
while(ptr!=NULL){
cout<<ptr->data<<" ";
ptr=ptr->next;
}
cout<<endl;
}
};
int main(){
linked_stack *stack=new linked_stack();
stack->push(2);
stack->pop();
stack->push(23);
stack->pop();
stack->push(45);
stack->push(36);
stack->push(2);
stack->display();
}
我刚刚开始学习堆栈,在这段代码中我创建了一个链表形式的堆栈。
上面执行的代码显示输出为 2 36 45 2 36 45 2 36 45 2 。 . . .till infinity 谁能在这里找到错误? (请忽略此括号文本,只是试图达到字数限制!)
我对你的代码做了一些编辑,分析了一些
临界情况。它适用于我并打印:2 36 45
class linked_stack{
node *top; //top is the head here
int size;
public:
linked_stack(){
size=0;
node *top=nullptr;
}
void push(int data){
node *newNode=new node;
newNode->data=data;
newNode->next=top;
top=newNode;
size++;
}
int pop(){
if(top==nullptr){
cout<<"UNDERFLOW";
return -1;
}
size--;
if(top->next == nullptr){
top = nullptr;
return -1;
}
node *temp=top;
top=top->next;
temp->next=NULL;
int popped=temp->data;
delete(temp);
return popped;
}
void display(){
node *ptr=top;
while(ptr!=nullptr){
cout<<ptr->data<<" ";
ptr=ptr->next;
}
cout<<endl;
}
};
当我 运行 我在调试器中的原始代码显示错误框“程序接收到信号 SIGSEGV,分段错误。”但随之而来的是输出 window,其中打印了“2 36 45”。
但代码在“在线 GDB C++ 编译器”上运行良好,但在“DevC++”编译器中显示错误(可能是这里的编译器问题)
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct node{
int data=0;
node *next=NULL;
};
class linked_stack{
node *top; //top is the head here
int size;
public:
linked_stack(){
size=0;
node *top=NULL;
}
void push(int data){
node *newNode=new node;
newNode->data=data;
newNode->next=top;
top=newNode;
size++;
}
int pop(){
if(top==NULL){
cout<<"UNDERFLOW";
return -1;
}
else{
size--;
node *temp=top;
top=top->next;
temp->next=NULL;
int popped=temp->data;
delete(temp);
return popped;
}
}
void display(){
node *ptr=top;
while(ptr!=NULL){
cout<<ptr->data<<" ";
ptr=ptr->next;
}
cout<<endl;
}
};
int main(){
linked_stack *stack=new linked_stack();
stack->push(2);
stack->pop();
stack->push(23);
stack->pop();
stack->push(45);
stack->push(36);
stack->push(2);
stack->display();
}
我刚刚开始学习堆栈,在这段代码中我创建了一个链表形式的堆栈。
上面执行的代码显示输出为 2 36 45 2 36 45 2 36 45 2 。 . . .till infinity 谁能在这里找到错误? (请忽略此括号文本,只是试图达到字数限制!)
我对你的代码做了一些编辑,分析了一些 临界情况。它适用于我并打印:2 36 45
class linked_stack{
node *top; //top is the head here
int size;
public:
linked_stack(){
size=0;
node *top=nullptr;
}
void push(int data){
node *newNode=new node;
newNode->data=data;
newNode->next=top;
top=newNode;
size++;
}
int pop(){
if(top==nullptr){
cout<<"UNDERFLOW";
return -1;
}
size--;
if(top->next == nullptr){
top = nullptr;
return -1;
}
node *temp=top;
top=top->next;
temp->next=NULL;
int popped=temp->data;
delete(temp);
return popped;
}
void display(){
node *ptr=top;
while(ptr!=nullptr){
cout<<ptr->data<<" ";
ptr=ptr->next;
}
cout<<endl;
}
};
当我 运行 我在调试器中的原始代码显示错误框“程序接收到信号 SIGSEGV,分段错误。”但随之而来的是输出 window,其中打印了“2 36 45”。
但代码在“在线 GDB C++ 编译器”上运行良好,但在“DevC++”编译器中显示错误(可能是这里的编译器问题)