How to initialize static stack?

Please help. I searched for examples and found none.

I need to initialize a static stack by pushing the first node onto the stack.
Is there a way to do that?

The attempt below gets this compile error on line 91:
error: expected initializer before '.' token
 void A::stack.push(1);
              ^


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
#include <iostream>
using namespace std;

class Stack
{
    private:
	/* structure of a linked list node */
	struct node
	{
	    int data;
	    node *next;
	};
	node* head;
	 
    public:
	//initialize the stack (constructor)
	Stack()
	{
		head = NULL;
	}

	/* deleteNode() function deletes first node that matches data */
	void deleteNode(int data)
	{
	    node* n = head;

	    if(n->data == data) // when node to be deleted is head node
	    {
		n->data = n->next->data; // copy the data of next node to head
	    }
	    else // when not head node
	    {       // traverse stack to find the node that matches data
		    while(n->next != NULL && n->next->data != data)
		    {
			n = n->next;
		    }
	    }

	    if(n->next == NULL) // if node not in Linked List
	    {
		cout << " [ERROR: given data is not present in linked list] ";
		return;
	    }
	 
	    node *temp = n->next; // store address of next node
	    n->next = n->next->next; // remove node from Linked List
	    delete(temp);
	    return; 
	}
	 
	/* push() function inserts a node at the begining */
	void push(int new_data)
	{
	    node *new_head = new node;
	    new_head->data = new_data;
	    new_head->next = head;
	    head = new_head;
	}

	/* peek() function returns data of head node */
	int peek()
	{
	    return head->data;
	}
	 
	/* Utility function to print a linked list */
	void printList()
	{
	    node* n = head;

	    while(n!=NULL)
	    {
		cout << n->data;
		n=n->next;
	    }
	}
};

class A
{
	private:
		static Stack stack;
	public:
		void test()
		{
		    stack.push(2);
		    stack.deleteNode(2);
	       	}
};
// initialize static variable out of class
void A::stack.push(1); // compile error: expected initializer before '.' token

int main()
{
	A a;
	a.test();
}


Thank you.
Last edited on
Why would you want it to be static ?

My suggestion is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ...
// ...
// ...

class A
{
public:
    A( int i = 0 ) { stack.push( i ); }

    void test()
    {
        stack.push(2);
        stack.deleteNode(2);
    }

private :
    Stack stack;
};


and in main :
1
2
3
4
5
6
7
8
int main ()
{
    A a; // head is 0
    A b( 4 ); // head is 4

    a.test();
    b.test();
}


Note:

void A::stack.push( 1 );
The purpose of initialization is to give a variable or object an initial value, not to call a method.

If you really want it to be static, then it should be
Stack A::stack; BUT !
1
2
3
4
5
void test()
{
    stack.push(2);
    stack.deleteNode(2);
}
would not make sense, since the class has no non-static member named stack ( but a static one ) OMG! This is embarrassing. I'm wrong w/ this part. I guess i need to read back my books again :DD

That will cause a segfault.
Last edited on
> I need to initialize a static stack by pushing the first node onto the stack.

Write a one-arg constructor which constructs a stack of size one.
Then define the static member (stack) with this constructor.

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

struct stack
{
    
    stack() = default ; // default constructor, construct empty stack
    explicit stack( int v ) : stack() { push(v) ; } // construct stack of size one

    ~stack() { while( !empty() ) pop() ; }

    bool empty() const { return head == nullptr ; }
    int top() const { assert( !empty() ) ; return head->data ; }

    void pop() { assert( !empty() ) ; auto t = head ; head = head->next ; delete t ; }
    void push( int v ) { head = new node( v, head ) ; }

    /* Utility function to print a linked list */
    std::ostream& print( std::ostream& stm = std::cout )
    {
        stm << "[ " ;
        for( node* n = head ; n ; n = n->next ) stm << n->data << ' ' ;
        return stm << "]\n" ;
    }

    private:

        /* structure of a linked list node */
        struct node
        {
            int data;
            node *next;
            node( int v, node* n ) : data(v), next(n) {}
        };

        node* head = nullptr ;

        // non-copyable
        stack( const stack& ) = delete ;
        stack& operator= ( const stack& ) = delete ;
};

class A
{
	private: static stack stk ;

	public:
		static void test( int v )
		{
		    stk.push(v);
		    stk.print() ;
        }
};

// define static variable (initialize stack with size one)
stack A::stk(11);

int main()
{
	A a1, a2, a3 ;
	a1.test(222);
	a2.test(3333) ;
	a3.test(44444) ;
	A::test(555555) ;
}

http://coliru.stacked-crooked.com/a/cd07dc4a8272c852
Hi shadow fiend.
Why would you want it to be static ?
To share one stack among multiple class A objects. Example below.

Hi JLBorges. Initializing in the constructor worked. Thanks for your help.

This works:
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
using namespace std;

class Stack
{
    private:
	/* structure of a linked list node */
	struct node
	{
	    int data;
	    node *next;
	};
	node* head;
	 
    public:
	//initialize the stack (constructor)
	Stack()
	{
		head = NULL;
		push(1); // base node
	}

	/* deleteNode() function deletes first node that matches data */
	void deleteNode(int data)
	{
	    node* n = head;

	    if(n->data == data) // when node to be deleted is head node
	    {
		n->data = n->next->data; // copy the data of next node to head
	    }
	    else // when not head node
	    {       // traverse stack to find the node that matches data
		    while(n->next != NULL && n->next->data != data)
		    {
			n = n->next;
		    }
	    }

	    if(n->next == NULL) // if node not in Linked List
	    {
		cout << " [ERROR: given data is not present in linked list] ";
		return;
	    }
	 
	    node *temp = n->next; // store address of next node
	    n->next = n->next->next; // remove node from Linked List
	    delete(temp);
	    return; 
	}
	 
	/* push() function inserts a node at the begining */
	void push(int new_data)
	{
	    node *new_head = new node;
	    new_head->data = new_data;
	    new_head->next = head;
	    head = new_head;
	}

	/* peek() function returns data of head node */
	int peek()
	{
	    return head->data;
	}
	 
	/* Utility function to print a linked list */
	void printList()
	{
	    node* n = head;

	    cout << " head[ ";
	    while(n!=NULL)
	    {
		cout << n->data << " ";
		n=n->next;
	    }
	    cout << "]base" << endl;
	}
};

class A
{
	private:
	public:
		static Stack stack;
		static void test()
		{
		    stack.push(2);
		    stack.push(3);
		    stack.push(4);

		    cout << "Given Linked List: ";
		    stack.printList();
		 
		    cout << "attempt to delete non-existent node" << endl;
		    stack.deleteNode(9); cout << endl;

		    cout << "deleting base node";
		    stack.deleteNode(1);

		    cout << "\nModified Linked List: ";
		    stack.printList();

		    cout << "Deleting node with value 3 ";
		    stack.deleteNode(3);
		 
		    cout << "\nModified Linked List: ";
		    stack.printList();
		    cout << "head node is " << stack.peek();

		    cout << "\nDeleting head node ";
		    stack.deleteNode(stack.peek());
		 
		    cout << "\nModified Linked List: ";
		    stack.printList();
		 
		    cout << "head node is " << stack.peek();
	       	}
};
// initialize static variable out of class
Stack A::stack;

int main()
{
	A a;
	A b;

	cout << "test a:" << endl;
	a.test();

	cout << endl << endl << "test b:" << endl;
	b.test();
}


output:
test a:
Given Linked List:  head[ 4 3 2 1 ]base
attempt to delete non-existent node
 [ERROR: given data is not present in linked list]
deleting base node
Modified Linked List:  head[ 4 3 2 ]base
Deleting node with value 3
Modified Linked List:  head[ 4 2 ]base
head node is 4
Deleting head node
Modified Linked List:  head[ 2 ]base
head node is 2

test b:
Given Linked List:  head[ 4 3 2 2 ]base
attempt to delete non-existent node
 [ERROR: given data is not present in linked list]
deleting base node [ERROR: given data is not present in linked list]
Modified Linked List:  head[ 4 3 2 2 ]base
Deleting node with value 3
Modified Linked List:  head[ 4 2 2 ]base
head node is 4
Deleting head node
Modified Linked List:  head[ 2 2 ]base
head node is 2
Last edited on
Topic archived. No new replies allowed.