Skip to main content
Added C++11 tag
Link
hoffmale
  • 6.5k
  • 18
  • 41
added 28 characters in body; edited title
Source Link
yuri
  • 4.6k
  • 3
  • 19
  • 40

stack Stack implemented bywith linked list (C++)

I have looked at some previous posts and changed my code accordingly. Do you have some suggestions about my current code? Thanks a lot.

Here are my specific questions:

1.What should top() return if the stack is empty?

2.Why it’s better to use size_t for count?

3.I didn’t use new to create the stack in main(). Do I need to call the destructor? If no, why? If yes, how should I do?

4.According to the rule of three, how will I define the assignment operator for stack? Can you give one example?

5.I feel I have some misunderstanding about private. I put top_node in the private section, but why I am able to access top_node in the copy constructor?

  1. What should top() return if the stack is empty?

  2. Why it’s better to use size_t for count?

  3. I didn’t use new to create the stack in main(). Do I need to call the destructor? If no, why? If yes, how should I do it?

  4. According to the rule of three, how will I define the assignment operator for stack? Can you give one example?

  5. I feel I have some misunderstanding about private. I put top_node in the private section, but why I am able to access top_node in the copy constructor?

Thanks again~

stack implemented by linked list (C++)

I have looked at some previous posts and changed my code accordingly. Do you have some suggestions about my current code? Thanks a lot.

Here are my specific questions:

1.What should top() return if the stack is empty?

2.Why it’s better to use size_t for count?

3.I didn’t use new to create the stack in main(). Do I need to call the destructor? If no, why? If yes, how should I do?

4.According to the rule of three, how will I define the assignment operator for stack? Can you give one example?

5.I feel I have some misunderstanding about private. I put top_node in the private section, but why I am able to access top_node in the copy constructor?

Thanks again~

Stack implemented with linked list

I have looked at some previous posts and changed my code accordingly. Do you have some suggestions about my current code?

Here are my specific questions:

  1. What should top() return if the stack is empty?

  2. Why it’s better to use size_t for count?

  3. I didn’t use new to create the stack in main(). Do I need to call the destructor? If no, why? If yes, how should I do it?

  4. According to the rule of three, how will I define the assignment operator for stack? Can you give one example?

  5. I feel I have some misunderstanding about private. I put top_node in the private section, but why I am able to access top_node in the copy constructor?

Source Link

stack implemented by linked list (C++)

I have looked at some previous posts and changed my code accordingly. Do you have some suggestions about my current code? Thanks a lot.

Here are my specific questions:

1.What should top() return if the stack is empty?

2.Why it’s better to use size_t for count?

3.I didn’t use new to create the stack in main(). Do I need to call the destructor? If no, why? If yes, how should I do?

4.According to the rule of three, how will I define the assignment operator for stack? Can you give one example?

5.I feel I have some misunderstanding about private. I put top_node in the private section, but why I am able to access top_node in the copy constructor?

The following code compiles and runs.

#include<iostream>
using namespace std;

template<class T>
class MyStack{
public:
    MyStack(): top_node(nullptr), count(0){};

    MyStack(MyStack& st){
        top_node = new Node(st.top());
        Node* temp = st.top_node->next;
        Node* pre = top_node;
        while(temp){
            Node* cur = new Node(temp->val);
            pre->next = cur;
            temp = temp->next;
        }
        count = st.size();
    }

    void push(T item){
        Node* temp = new Node(item);
        if(empty()){
            top_node = temp;
        }else{
            temp->next = top_node;
            top_node = temp;
        }
        count++;    
    }

    void pop(){
        if(empty()){
            cout<<"Nothing to pop!"<<endl;
            return;
        }
        Node* temp = top_node;
        top_node = temp->next;
        delete temp;
    }

    int top() const{
        if(empty()){
            cout<<"Nothing to top!"<<endl;
            return -1;
        }
        return top_node->val;
    }

    size_t size() const{
        return count;
    }

    void print() const{
        Node* temp = top_node;
        while(temp){
            cout<<temp->val<<" ";
            temp = temp->next;
        }
        cout<<endl;
    }

    bool empty() const{
        return count==0;
    }

    ~MyStack(){
        Node* temp = top_node;
        while(temp){
            Node* t = temp;
            temp = temp->next;
            delete t;
        }
        top_node = nullptr;
    }
    
private:
    struct Node{
        T val;
        Node* next;
        Node(T x): val(x), next(nullptr){};
    };
    Node* top_node;
    size_t count;

};

int main(){

    MyStack<char> sasa;
    cout<<"default top: "<<sasa.top()<<endl;
    cout<<"default count: "<<sasa.size()<<endl;

    sasa.pop();
    sasa.push('a');
    sasa.push('p');
    sasa.push('p');
    sasa.print();
    sasa.top();
    cout<<"The current size is "<<sasa.size()<<endl;
    sasa.pop();
    sasa.print();
    cout<<"if empty: "<<sasa.empty()<<endl;

    MyStack<char> sec(sasa);
    sec.print();

    return 0;
}

Thanks again~