C++ LinkedList explanation

Please explain to me how these codes work. That will make me easily understand.
Line# 11,12,13,14
Line# 26,27,28
Line# 40,41,42,43,44

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
#include "Stack.h"
#include "Node.h"

Stack::Stack()
{
    top = NULL;
}

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

}

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

void Stack::display()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        while(temp!=NULL)
        {
            cout << temp -> data << " ";
            temp = temp -> next;
        }
        cout << endl;
    }
}
Last edited on
if line 11 confuses you, you need to read this first.
https://cplusplus.com/doc/tutorial/pointers/

and this as well:
https://cplusplus.com/doc/tutorial/dynamic/

note that NULL has been replaced by nullptr in modern code.

once you have a handle on the syntax from the 2 articles, you will see what most of this is doing, but in short...
push makes a new item and attaches it to the front of the list.
so the list starts out empty, equal to null, and say we are adding integers, it looks like this
null
(push 3)
3 - null
push 2
2 - 3 - null
and null will be used to realize you hit the end of the list.

pop will do the reverse, if there is something not null on the list, it removes the first one, so
2 - 3 - null
removes 2 giving
3 - null and the 2 is returned to the user to be consumed.

display just shows everything in your stack, and is an example of using null to stop iterating and realize its the end of the list. Most lists also have a peek to see the top value without removing it.
Last edited on
A procedural note, swap the two includes. Put #include "Node.h" before #include "Stack.h" since your stack class has Node data members.

Your current setup may compile with the headers included as they are, but other projects you work with multiple includes on may not.
This code originally came from this post
https://cplusplus.com/forum/general/284766/

where I changed the code to more 'modern' C++ Looks like it's been ignored ...

Node.h
1
2
3
4
5
6
7
struct Node {
	int data {};
	Node* next {};

	Node() {}
	Node(int d) : data(d) {}
};


Stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Node.h"

class Stack {
	Node* top {};

public:
	Stack() = default;
	~Stack();
	void push(int data);
	void pop();
	void display();

	Stack(const Stack&) = delete;
	Stack& operator=(const Stack&) = delete;
};


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
#include <iostream>
#include "Stack.h"

Stack::~Stack() {
	while (top) {
		const auto tmp { top->next };

		delete top;
		top = tmp;
	}
}

void Stack::push(int data) {
	const auto node { new Node(data) };

	node->next = top;
	top = node;
}

void Stack::pop() {
	if (!top)
		std::cout << "Stack is empty!\n";
	else {
		const auto temp { top };

		top = top->next;
		delete temp;
	}
}

void Stack::display() {
	if (!top)
		std::cout << "Stack is empty!\n";
	else {
		for (auto temp { top }; temp; temp = temp->next)
			std::cout << temp->data << " ";

		std::cout << '\n';
	}
}

Last edited on
Topic archived. No new replies allowed.