Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,846 questions

51,767 answers

573 users

How to implement stack with struct in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

#define SIZE 6

struct stack {
	stack();
	void push(char ch);
	void pop();
	void print(void);
private:
	char str[SIZE] = "";
	int index;
};

stack::stack() {
	index = 0;
}

void stack::push(char ch)
{
	if (index == SIZE) {
		cout << "Stack full" << endl;
		return;
	}
	str[index] = ch;
	index++;
}

void stack::pop()
{
	if (index == 0) {
		cout << "Stack empty" << endl;
	}
	index--;
}

void stack::print(void) {
	for (int i = 0; i < index; i++)
		cout << str[i];
	cout << endl;
}


int main()
{
	stack s;
	
	s.push('c');
	s.push(':');
	s.push('c');
	s.push('+');
	s.push('+');
	s.print();

	s.pop();
	s.print();

	return 0;
}


/*
run:

c:c++
c:c+

*/

 



answered Mar 31, 2018 by avibootz

Related questions

1 answer 55 views
1 answer 193 views
1 answer 222 views
1 answer 129 views
...