Geek
Geek

Reputation: 59

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();
}

I have just started learning stacks and in this code I have created a stack in linked list form .

The above code on executing the shows the output as 2 36 45 2 36 45 2 36 45 2 . . . .till infinity Can anyone find the error here? (plz ignore this bracket text just trying to reach the word limit!)

Upvotes: 1

Views: 94

Answers (2)

Geek
Geek

Reputation: 59

when I ran my original code in debugger it showed an error box "Program received signal SIGSEGV, Segmentation fault." But along with this there was an output window where "2 36 45" was printed .

BUT code is working fine on "online GDB C++ compiler" but showing the error in "DevC++" compiler(maybe it could be the compiler issue here)

Upvotes: 0

Andrea Mugnai
Andrea Mugnai

Reputation: 361

I made some edits to your code, analyzing some borderline cases. It works for me and prints: 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;
        }
};

Upvotes: 1

Related Questions