Linkedlist in a Class

Sep 3, 2022 at 2:42am
Hello, please help me how do I put these codes in a class (.cpp and header file)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
 #include <iostream>
#include <stdio.h>
using namespace std;

class Node
{
    public:
        int data;
        Node* next;

};

class Stack
{
    public:
        Node* top;
        Stack()
        {
            top = NULL;
        }


void push(int data)
{
    Node* node = new Node();
    node -> data = data;
    node -> next = top;
    top = node;
}

void pop()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        top = top -> next;
        delete temp;
    }
}

void display()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        while(temp!=NULL)
        {
            cout << temp -> data << " ";
            temp = temp -> next;
        }
        cout << endl;
    }
}
};


int main()
{
    Stack stack;

    int choice, data;

    while(1)
    {
        cout << "1. Push to stack" << endl;
        cout << "2. Pop from stack" << endl;
        cout << "3. Display stack" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        if(choice == 1)
        {
            cout << "Enter data to push: ";
            cin >> data;
            stack.push(data);
        }
        else if(choice == 2)
        {
            stack.pop();
        }
        else if(choice == 3)
        {
            stack.display();
        }
        else if(choice == 4)
        {
            break;
        }
        else
        {
            cout << "Invalid choice." << endl;
        }
    }
    return 0;
}
Last edited on Sep 3, 2022 at 2:43am
Sep 3, 2022 at 3:10am
You'll have to split up declarations and definitions. That or use inline functions. Variables generally go in a private or protected section, and getter/setter functions are used to modify them.

Declarations of functions for the person class go in a header person.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef PERSONCLASSWRAPPER
#define PERSONCLASSWRAPPER

#include <iostream>
#include <string>

class person
{
    public:
        person();
        void setString(std::string newText);
        void print();
    private:
        std::string text;

};

#ifndef 


Person class definitions go in person.cpp;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "person.h"
person::person()
{
    text = "This is set in person's constructor.";
}

void person::print()
{
    std::cout << text << std::endl;
}
void person::setText(std::string newText)
{
    text = newText;
}


And finally it comes together in main.cpp;
1
2
3
4
5
6
7
8
9
#include "person.h"

int main()
{
    person bob;
    bob.print();
    bob.setText("Hello world!");
    bob.print();
}
Last edited on Sep 3, 2022 at 3:16am
Sep 3, 2022 at 3:32am
How do i do that when i have 2 class (Node & Stack)? Thats bothering me
Last edited on Sep 3, 2022 at 3:33am
Sep 3, 2022 at 3:58am
So you have a header and a source file for each class. If either class is part of the other you include the header of that class in the source file for the other class.

Or have a single header and source file for both classes.

Sep 3, 2022 at 4:48am
Can you provide an example? I want to separate the class Node & class Stack by creating 2 header, is it possible? I tried to do it but it gives me an error.

Stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "Stack.h"

void push(int data)
{
    Node* node = new Node();
    node -> data = data;
    node -> next = top;
    top = node;
}

void pop()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        top = top -> next;
        delete temp;
    }
}

void display()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        while(temp!=NULL)
        {
            cout << temp -> data << " ";
            temp = temp -> next;
        }
        cout << endl;
    }
}



main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include "Stack.h"
#include "Node.h"
using namespace std;

int main()
{
    Stack stack;

    int choice, data;

    while(1)
    {
        cout << "1. Push to stack" << endl;
        cout << "2. Pop from stack" << endl;
        cout << "3. Display stack" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        if(choice == 1)
        {
            cout << "Enter data to push: ";
            cin >> data;
            stack.push(data);
        }
        else if(choice == 2)
        {
            stack.pop();
        }
        else if(choice == 3)
        {
            stack.display();
        }
        else if(choice == 4)
        {
            break;
        }
        else
        {
            cout << "Invalid choice." << endl;
        }
    }
    return 0;
}


Node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
#ifndef NODE_H
#define NODE_H


class Node
{
     public:
        int data;
        Node* next;
};


#endif // NODE_H


Stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H


class Stack
{
    public:
        Node* top;
        Stack()
        {
            top = NULL;
        }
};



#endif // STACK_H

Sep 3, 2022 at 5:31am
Your Stack class uses the Node struct, you need to include the Node header in your Stack source file. Before your Stack header include.
Sep 3, 2022 at 5:38am
The errors has been reduced, but I still have errors it says that class Stack has no member name push/pop/display. And 'top' was not declared in this scope.
Sep 3, 2022 at 5:52am
In your stack.cpp file, you need to tell the compiler that the functions are owned by the stack class.
A function definition for the stack class should look like this:
1
2
3
4
void stack::pop()
{
    //code for pop function
}

Last edited on Sep 3, 2022 at 6:05am
Sep 3, 2022 at 6:16am
Oh i see, its now working. Thanks a lot to those who help :D
Topic archived. No new replies allowed.