Function to display all stacks

What is the error of my void function to print all the displayed stack elements?

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
#include<iostream>

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

Node *head=NULL,*tail=NULL;

void push(int newData){
    Node *newNode = new Node;
    newNode->data = newData;
    newNode->next = head;

    if(head==NULL){
        head = tail = newNode;
    } else {
        newNode->next = head;
        head = newNode;
    }
}





int pop(){
    int tempVal;
    Node *temp;

    if (head == NULL){
        head = tail = NULL;
        std::cout << "Stack Underflow." << std::endl;
        return -1;
    } else {
        temp = head;
        tempVal = temp->data;
        head = head->next;
        delete(temp);
        return tempVal;
    }
}

void Top(){
    if(head==NULL){
        std::cout << "Stack is Empty." << std::endl;
        return;
    } else {
        std::cout << "Top of Stack: " << head->data << std::endl;
    }
}


void printStack()
{
   struct *node temp;
   temp=top;
   while(temp)
   {
       std::cout <<" The stack elements are : " <<temp->data <<std:: endl;
       temp= temp->head;

   }
}
int main(){

    push(1);
    std::cout<<"After the first PUSH top of stack is :";
    Top();
    push(5);
    std::cout<<"After the second PUSH top of stack is :";
    Top();
    pop();
    std::cout<<"After the first POP operation, top of stack is:";
    Top();
    pop();
    std::cout<<"After the second POP operation, top of stack :";
    Top();
    pop();

    return 0;
}
> temp= temp->head;
It's next, not head.
55
56
57
58
59
60
61
62
63
64
void printStack()
{
    Node * temp;    // struct *node temp;
    temp = head;
    while (temp)
    {
        std::cout << " The stack elements are : " << temp->data << std::endl;
        temp = temp->next;    // temp= temp->head;
    }
}

Last edited on
In C++, it should be nullptr not NULL. Also tail isn't used and allocated memory isn't released. Consider:

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
#include <iostream>

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

	Node(int d = 0, Node* n = nullptr) : data(d), next(n) {}
};

Node* head {};

void push(int newData) {
	head = new Node(newData, head);
}

int pop() {
	if (!head) {
		std::cout << "Stack Underflow.\n";
		return -1;
	}

	const auto temp { head };
	const auto tempVal { temp->data };

	head = head->next;
	delete temp;
	return tempVal;
}

void Top() {
	if (!head)
		std::cout << "Stack is Empty.\n";
	else
		std::cout << "Top of Stack: " << head->data << '\n';
}

void printStack() {
	std::cout << "The stack elements are : ";

	for (auto temp {head}; temp; temp = temp->next)
		 std::cout << temp->data << ' ';

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

void clear() {
	while (head) {
		const auto tmp { head->next };

		delete head;
		head = tmp;
	}
}

int main() {
	push(1);
	std::cout << "After the first PUSH ";
	Top();

	push(5);
	std::cout << "After the second PUSH ";
	Top();

	printStack();

	pop();
	std::cout << "After the first POP operation ";
	Top();

	pop();
	std::cout << "After the second POP operation ";
	Top();

	clear();
}



After the first PUSH Top of Stack: 1
After the second PUSH Top of Stack: 5
The stack elements are : 5 1
After the first POP operation Top of Stack: 1
After the second POP operation Stack is Empty.

Last edited on
You can still use NULL in C++, but nullptr has a few advantages. Not that it really matters for what you're doing here.
Last edited on
Yeah - but it's not good practice. When in C++ do as C++ ...
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
#include <iostream>

struct stack
{
    stack() = default ;
    ~stack() { while( !empty() ) pop() ; }

    // non-copyable for now. TO DO: implement copy and move
    stack( const stack& ) = delete ;
    stack& operator= ( const stack& ) = delete ;


    bool empty() const noexcept { return top_of_stack == nullptr ; }
    int& top() { return empty() ? throw "empty" : top_of_stack->value ; }
    int top() const { return empty() ? throw "empty" : top_of_stack->value ; }

    void push( int v ) { top_of_stack = new node { v, top_of_stack } ; }
    void pop()  noexcept { if( !empty() ) { node* p = top_of_stack ; top_of_stack = top_of_stack->next ; delete p ; } }

    std::ostream& print( std::ostream& stm = std::cout ) const
    {
        if( empty() ) return stm << "empty\n" ;
        for( node* p = top_of_stack ; p ; p = p->next ) stm << p->value << " => " ;
        return stm << "nullptr\n" ;
    }

    private:
        struct node { int value ; node* next = nullptr ; } ;
        node* top_of_stack = nullptr ;
};

int main()
{
    stack stk ;

    for( int v : { 1, 5, 12, 34, 52, 86, 99 } )
    {
        stk.push(v) ;
        stk.print( std::cout << "after push " << v << " : " ) ;
    }

    std::cout << "-------------------------------\n" ;

    while( !stk.empty() )
    {
        std::cout << "top == " << stk.top() << ", " ;
        stk.pop() ;
        stk.print( std::cout << "after pop : " ) ;
    }
}

https://coliru.stacked-crooked.com/a/e8cbc63f00c6b5c3
Yeah - using a stack class is much better. I just updated the code using the existing methodology which only had a public class (could have been struct) for the node...

Last edited on
but why create your own linked-list (unless this is part of the exercise)? Why not use the standard library list? Based upon the original code (not using a class), then consider:

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
#include <iostream>
#include <forward_list>

std::forward_list<int> Stack;

void push(int newData) {
	Stack.push_front(newData);
}

int pop() {
	if (Stack.empty()) {
		std::cout << "Stack Underflow.\n";
		return -1;
	}

	const auto val { Stack.front() };

	Stack.pop_front();
	return val;
}

void Top() {
	if (Stack.empty())
		std::cout << "Stack is Empty.\n";
	else
		std::cout << "Top of Stack: " << Stack.front() << '\n';
}

void printStack() {
	std::cout << "The stack elements are : ";

	for (const auto& s : Stack)
		std::cout << s << ' ';

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

int main() {
	push(1);
	std::cout << "After the first PUSH ";
	Top();

	push(5);
	std::cout << "After the second PUSH ";
	Top();

	printStack();

	pop();
	std::cout << "After the first POP operation ";
	Top();

	pop();
	std::cout << "After the second POP operation ";
	Top();
}

... and as a class, then consider:

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
#include <iostream>
#include <forward_list>
#include <initializer_list>

struct Stack {
	Stack() = default;

	Stack(std::initializer_list<int> il) {
		for (const auto& i : il)
			push(i);
	}

	bool empty() const noexcept {
		return stack.empty();
	}

	int& top() {
		return empty() ? throw "empty" : stack.front();
	}

	int top() const {
		return empty() ? throw "empty" : stack.front();
	}

	void push(int v) {
		stack.push_front(v);
	}

	void pop() {
		if (empty())
			throw "empty";

		stack.pop_front();
	}

	friend std::ostream& operator<<(std::ostream& stm, const Stack& stk) {
		if (stk.stack.empty())
			return stm << "empty\n";

		for (size_t i {}; const auto& s : stk.stack)
				stm << (i++ ? " => " : "") << s;

		return stm;
	}

protected:
	std::forward_list<int> stack;
};

int main() {
	Stack stk {2, 3, 5, 7};

	std::cout << "Initial stack: " << stk << '\n';

	for (int v : { 1, 5, 12, 34, 52, 86, 99 }) {
		stk.push(v);
		std::cout << "after push " << v << " : " << stk << '\n';
	}

	std::cout << "-------------------------------\n";

	while (!stk.empty()) {
		std::cout << "top == " << stk.top() << ", ";
		stk.pop();
		std::cout << "after pop : " << stk << '\n';
	}
}



Initial stack: 7 => 5 => 3 => 2
after push 1 : 1 => 7 => 5 => 3 => 2
after push 5 : 5 => 1 => 7 => 5 => 3 => 2
after push 12 : 12 => 5 => 1 => 7 => 5 => 3 => 2
after push 34 : 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
after push 52 : 52 => 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
after push 86 : 86 => 52 => 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
after push 99 : 99 => 86 => 52 => 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
-------------------------------
top == 99, after pop : 86 => 52 => 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
top == 86, after pop : 52 => 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
top == 52, after pop : 34 => 12 => 5 => 1 => 7 => 5 => 3 => 2
top == 34, after pop : 12 => 5 => 1 => 7 => 5 => 3 => 2
top == 12, after pop : 5 => 1 => 7 => 5 => 3 => 2
top == 5, after pop : 1 => 7 => 5 => 3 => 2
top == 1, after pop : 7 => 5 => 3 => 2
top == 7, after pop : 5 => 3 => 2
top == 5, after pop : 3 => 2
top == 3, after pop : 2
top == 2, after pop : empty

Topic archived. No new replies allowed.