Horje
stack implementation using linked list in cpp Code Example
stack implementation using linked list in cpp
#include<iostream>
using namespace std;
struct node
{
	int data;
	node* next;
};
class Stack
{
private:
	node* top;
public:
	Stack()
	{
		top = NULL;
	}
	void Push(int n)
	{
		node* temp = new node();
		temp->data = n;
		temp->next = NULL;
		if (top == NULL)
		{
			top = temp;
		}
		else
		{
			temp->next = top;
			top = temp;
		}
	}
	void Pop()
	{
		
		if (top == NULL)
		{
			cout << "Error,Stack is Empty!" << endl;
			return;
		}
		node* temp = top;
		top = top->next;
		delete temp;
	}
	void Top()
	{
		cout << "Top Of Stack: " << top->data << endl;
	}
	void Display()
	{
		node* t = top;
		cout << "Stack: ";
		while (t != NULL)
		{
			cout << t->data << " ";
			t = t->next;
		}
		cout << "\n";
	}
	void IsEmpty()
	{
		node* t = top;
		if (t == NULL)
		{
			cout << "Stack Is Empty!" << endl;
		}
		else
		{
			cout << "Stack Is Not Empty!" << endl;
			Display();
		}
	}
};
int main()
{
	Stack s;
	s.Push(1);
	s.IsEmpty();
	s.Pop();
	s.IsEmpty();

	return 0;
}




Cpp

Related
qt messagebox Code Example qt messagebox Code Example
convert whole string to lowercase c++ Code Example convert whole string to lowercase c++ Code Example
c++ string to wstring Code Example c++ string to wstring Code Example
max three values c++ Code Example max three values c++ Code Example
print array c++ Code Example print array c++ Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8