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

Simple stack implementation, how can I improve the code?

I am not experienced in C++, and wrote the belowthis code for implementing a stack data structure using a linked list.

Can you please point out any flaws, errors, stupid mistakes, general improvements etc that can make this code better? Thanks.

Simple stack implementation, how can I improve the code?

I am not experienced in C++, and wrote the below code for implementing a stack data structure using a linked list.

Can you please point out any flaws, errors, stupid mistakes, general improvements etc that can make this code better? Thanks.

Simple stack implementation

I am not experienced in C++, and wrote this code for implementing a stack data structure using a linked list.

Can you please point out any flaws, errors, stupid mistakes, general improvements etc that can make this code better?

Tweeted twitter.com/#!/StackCodeReview/status/207747045535121408
added 25 characters in body
Source Link
Moeb
  • 259
  • 3
  • 6
#include <iostream>                                                                      
using namespace std;

struct Node;

struct Node {
    int data;
    Node* next;
};

class Stack {
public:
    Node* first;
    Node* last;

    Stack() {
        first = 0;
        last = 0;
    }

    ~Stack() {
         while (first != 0) { pop(); }
    }

    Stack& push(int value) {
        Node* temp = new Node;
        temp->data = value;
        temp->next = 0;
        
        if (first == 0) {
            first = temp;
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
       
        cout << "Pushed " << value << " on the stack\n";

        temp = 0;
        return *this;
    }
    
    Stack& pop() {
        if (first == 0) {
            cout << "No nodes to pop.\n";
        } else {
                // only one node left
                if (first == last) {
                cout << "Popped " << first->data << " off the stack\n";
                delete first;
                first = 0;
                last = 0;                                                                
                return *this;
            }

            Node* temp = first;
            while (temp->next != last) {
                temp = temp->next;
            }
            cout << "Popped " << last->data << " off the stack\n";
            delete last;
            last = temp;
            last->next = 0;
        }
        return *this;
    }
};

int main() {
    Stack s;
    s.pop();
    s.push(1);
    s.push(2);
    s.pop();
    s.push(3).push(4);
    s.pop().pop();
    return 0;
}
#include <iostream>                                                                      
using namespace std;

struct Node;

struct Node {
    int data;
    Node* next;
};

class Stack {
public:
    Node* first;
    Node* last;

    Stack() {
        first = 0;
        last = 0;
    }

    ~Stack() {
         while (first != 0) { pop(); }
    }

    Stack& push(int value) {
        Node* temp = new Node;
        temp->data = value;
        temp->next = 0;
        
        if (first == 0) {
            first = temp;
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
       
        cout << "Pushed " << value << " on the stack\n";

        temp = 0;
        return *this;
    }
    
    Stack& pop() {
        if (first == 0) {
            cout << "No nodes to pop.\n";
        } else {
            
                if (first == last) {
                cout << "Popped " << first->data << " off the stack\n";
                delete first;
                first = 0;
                last = 0;                                                                
                return *this;
            }

            Node* temp = first;
            while (temp->next != last) {
                temp = temp->next;
            }
            cout << "Popped " << last->data << " off the stack\n";
            delete last;
            last = temp;
            last->next = 0;
        }
        return *this;
    }
};

int main() {
    Stack s;
    s.pop();
    s.push(1);
    s.push(2);
    s.pop();
    s.push(3).push(4);
    s.pop().pop();
    return 0;
}
#include <iostream>                                                                      
using namespace std;

struct Node;

struct Node {
    int data;
    Node* next;
};

class Stack {
public:
    Node* first;
    Node* last;

    Stack() {
        first = 0;
        last = 0;
    }

    ~Stack() {
         while (first != 0) { pop(); }
    }

    Stack& push(int value) {
        Node* temp = new Node;
        temp->data = value;
        temp->next = 0;
        
        if (first == 0) {
            first = temp;
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
       
        cout << "Pushed " << value << " on the stack\n";

        temp = 0;
        return *this;
    }
    
    Stack& pop() {
        if (first == 0) {
            cout << "No nodes to pop.\n";
        } else {
                // only one node left
                if (first == last) {
                cout << "Popped " << first->data << " off the stack\n";
                delete first;
                first = 0;
                last = 0;                                                                
                return *this;
            }

            Node* temp = first;
            while (temp->next != last) {
                temp = temp->next;
            }
            cout << "Popped " << last->data << " off the stack\n";
            delete last;
            last = temp;
            last->next = 0;
        }
        return *this;
    }
};

int main() {
    Stack s;
    s.pop();
    s.push(1);
    s.push(2);
    s.pop();
    s.push(3).push(4);
    s.pop().pop();
    return 0;
}
Source Link
Moeb
  • 259
  • 3
  • 6

Simple stack implementation, how can I improve the code?

I am not experienced in C++, and wrote the below code for implementing a stack data structure using a linked list.

Can you please point out any flaws, errors, stupid mistakes, general improvements etc that can make this code better? Thanks.

#include <iostream>                                                                      
using namespace std;

struct Node;

struct Node {
    int data;
    Node* next;
};

class Stack {
public:
    Node* first;
    Node* last;

    Stack() {
        first = 0;
        last = 0;
    }

    ~Stack() {
         while (first != 0) { pop(); }
    }

    Stack& push(int value) {
        Node* temp = new Node;
        temp->data = value;
        temp->next = 0;
        
        if (first == 0) {
            first = temp;
            last = temp;
        } else {
            last->next = temp;
            last = last->next;
        }
       
        cout << "Pushed " << value << " on the stack\n";

        temp = 0;
        return *this;
    }
    
    Stack& pop() {
        if (first == 0) {
            cout << "No nodes to pop.\n";
        } else {
            
                if (first == last) {
                cout << "Popped " << first->data << " off the stack\n";
                delete first;
                first = 0;
                last = 0;                                                                
                return *this;
            }

            Node* temp = first;
            while (temp->next != last) {
                temp = temp->next;
            }
            cout << "Popped " << last->data << " off the stack\n";
            delete last;
            last = temp;
            last->next = 0;
        }
        return *this;
    }
};

int main() {
    Stack s;
    s.pop();
    s.push(1);
    s.push(2);
    s.pop();
    s.push(3).push(4);
    s.pop().pop();
    return 0;
}