Skip to main content
deleted 4 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

C++: Implementation of Stack using Linked List

C++: Implementation of Stack using Linked List

Please review my code and help me to improve it. I have written this code using template. Can I use more C++11? Is using top in this code is necessary?

#include <iostream>


template<class T>
class Stack{
 struct Node{
 T data;
 Node *next;
 Node(T val):data(val),next(nullptr) {}
 };
 Node *head;
 int top;

 public:
     Stack(){
     top=-1;
     head=nullptr;
     }
    ~Stack();
     bool stack_empty();
     void push(T);
     T pop();

     void display(std::ostream& out = std::cout) const
     {
       Node *node=head;
       while(node!=nullptr){
        out<<node->data<<" ";
        node=node->next;
       }
     }
};
template<class T>
bool Stack<T>::stack_empty(){
  if(top==-1)
    return true;
  else
    return false;
}

template<class T>
void Stack<T>::push(T val){
  
  if(top>=-1){
    top++;
    Node *node=new Node(val);
    Node *tmp=head;
    if(tmp==nullptr) head=node;
    else{
        while(tmp->next!=nullptr)
            tmp=tmp->next;
        tmp->next=node;
    }
  }
  else
    std::cout<<"Overflow"<<std::endl;
}

template<class T>
T Stack<T>::pop(){
  if(stack_empty()==true)
    std::cout<<"Underflow"<<std::endl;
  else{
    top--;
    Node *node=head;
     while(node->next!=nullptr){
        node=node->next;
     }
    return node->data;
  }
}

template<class U>
std::ostream & operator <<(std::ostream& os, const Stack<U>& s){
s.display(os);
return os;
}

template<class T>
Stack<T>::~Stack(){
Node *tmp=nullptr;
  while(head){
    tmp=head;
    head=head->next;
    delete tmp;
  }
  head=nullptr;
}
int main()
{
 Stack<int> stack1;

 stack1.push(3);
 stack1.push(4);
 stack1.push(11);
 stack1.push(30);
 std::cout<<stack1<<std::endl;
 std::cout<<stack1.pop()<<std::endl;
    return 0;
}

C++: Implementation of Stack using Linked List

Please review my code and help me to improve it. I have written this code using template. Can I use more C++11? Is using top in this code is necessary?

#include <iostream>


template<class T>
class Stack{
 struct Node{
 T data;
 Node *next;
 Node(T val):data(val),next(nullptr) {}
 };
 Node *head;
 int top;

 public:
     Stack(){
     top=-1;
     head=nullptr;
     }
    ~Stack();
     bool stack_empty();
     void push(T);
     T pop();

     void display(std::ostream& out = std::cout) const
     {
       Node *node=head;
       while(node!=nullptr){
        out<<node->data<<" ";
        node=node->next;
       }
     }
};
template<class T>
bool Stack<T>::stack_empty(){
  if(top==-1)
    return true;
  else
    return false;
}

template<class T>
void Stack<T>::push(T val){
  
  if(top>=-1){
    top++;
    Node *node=new Node(val);
    Node *tmp=head;
    if(tmp==nullptr) head=node;
    else{
        while(tmp->next!=nullptr)
            tmp=tmp->next;
        tmp->next=node;
    }
  }
  else
    std::cout<<"Overflow"<<std::endl;
}

template<class T>
T Stack<T>::pop(){
  if(stack_empty()==true)
    std::cout<<"Underflow"<<std::endl;
  else{
    top--;
    Node *node=head;
     while(node->next!=nullptr){
        node=node->next;
     }
    return node->data;
  }
}

template<class U>
std::ostream & operator <<(std::ostream& os, const Stack<U>& s){
s.display(os);
return os;
}

template<class T>
Stack<T>::~Stack(){
Node *tmp=nullptr;
  while(head){
    tmp=head;
    head=head->next;
    delete tmp;
  }
  head=nullptr;
}
int main()
{
 Stack<int> stack1;

 stack1.push(3);
 stack1.push(4);
 stack1.push(11);
 stack1.push(30);
 std::cout<<stack1<<std::endl;
 std::cout<<stack1.pop()<<std::endl;
    return 0;
}

Implementation of Stack using Linked List

Please review my code and help me to improve it. I have written this code using template. Can I use more C++11? Is using top in this code is necessary?

#include <iostream>


template<class T>
class Stack{
 struct Node{
 T data;
 Node *next;
 Node(T val):data(val),next(nullptr) {}
 };
 Node *head;
 int top;

 public:
     Stack(){
     top=-1;
     head=nullptr;
     }
    ~Stack();
     bool stack_empty();
     void push(T);
     T pop();

     void display(std::ostream& out = std::cout) const
     {
       Node *node=head;
       while(node!=nullptr){
        out<<node->data<<" ";
        node=node->next;
       }
     }
};
template<class T>
bool Stack<T>::stack_empty(){
  if(top==-1)
    return true;
  else
    return false;
}

template<class T>
void Stack<T>::push(T val){
  
  if(top>=-1){
    top++;
    Node *node=new Node(val);
    Node *tmp=head;
    if(tmp==nullptr) head=node;
    else{
        while(tmp->next!=nullptr)
            tmp=tmp->next;
        tmp->next=node;
    }
  }
  else
    std::cout<<"Overflow"<<std::endl;
}

template<class T>
T Stack<T>::pop(){
  if(stack_empty()==true)
    std::cout<<"Underflow"<<std::endl;
  else{
    top--;
    Node *node=head;
     while(node->next!=nullptr){
        node=node->next;
     }
    return node->data;
  }
}

template<class U>
std::ostream & operator <<(std::ostream& os, const Stack<U>& s){
s.display(os);
return os;
}

template<class T>
Stack<T>::~Stack(){
Node *tmp=nullptr;
  while(head){
    tmp=head;
    head=head->next;
    delete tmp;
  }
  head=nullptr;
}
int main()
{
 Stack<int> stack1;

 stack1.push(3);
 stack1.push(4);
 stack1.push(11);
 stack1.push(30);
 std::cout<<stack1<<std::endl;
 std::cout<<stack1.pop()<<std::endl;
    return 0;
}
Source Link
coder
  • 2.5k
  • 4
  • 32
  • 60

C++: Implementation of Stack using Linked List

Please review my code and help me to improve it. I have written this code using template. Can I use more C++11? Is using top in this code is necessary?

#include <iostream>


template<class T>
class Stack{
 struct Node{
 T data;
 Node *next;
 Node(T val):data(val),next(nullptr) {}
 };
 Node *head;
 int top;

 public:
     Stack(){
     top=-1;
     head=nullptr;
     }
    ~Stack();
     bool stack_empty();
     void push(T);
     T pop();

     void display(std::ostream& out = std::cout) const
     {
       Node *node=head;
       while(node!=nullptr){
        out<<node->data<<" ";
        node=node->next;
       }
     }
};
template<class T>
bool Stack<T>::stack_empty(){
  if(top==-1)
    return true;
  else
    return false;
}

template<class T>
void Stack<T>::push(T val){
  
  if(top>=-1){
    top++;
    Node *node=new Node(val);
    Node *tmp=head;
    if(tmp==nullptr) head=node;
    else{
        while(tmp->next!=nullptr)
            tmp=tmp->next;
        tmp->next=node;
    }
  }
  else
    std::cout<<"Overflow"<<std::endl;
}

template<class T>
T Stack<T>::pop(){
  if(stack_empty()==true)
    std::cout<<"Underflow"<<std::endl;
  else{
    top--;
    Node *node=head;
     while(node->next!=nullptr){
        node=node->next;
     }
    return node->data;
  }
}

template<class U>
std::ostream & operator <<(std::ostream& os, const Stack<U>& s){
s.display(os);
return os;
}

template<class T>
Stack<T>::~Stack(){
Node *tmp=nullptr;
  while(head){
    tmp=head;
    head=head->next;
    delete tmp;
  }
  head=nullptr;
}
int main()
{
 Stack<int> stack1;

 stack1.push(3);
 stack1.push(4);
 stack1.push(11);
 stack1.push(30);
 std::cout<<stack1<<std::endl;
 std::cout<<stack1.pop()<<std::endl;
    return 0;
}