my code stock.com

Reham ALblihed

Stack Using Linked List
by Reham ALblihed

Opration
- push
- pop
- is empty
- print

Snippet options

Download: Download snippet as stack-using-linked-list.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include<iostream>
using namespace std;

struct node{
	int info;
	node *next;
};

class stack{
private:
	node *top;
public:
	stack(){
		top = NULL;
	}

	bool isempty();
	void push(int);
	int pop();
	void traverese();

};


bool stack::isempty(){
	if (top == NULL)
		return true;
	else
		return false;
}

void stack::push(int item){
	node *nw = new node;
	nw->info = item;
		nw->next = top;
		top = nw;
}
int stack::pop(){
	if (isempty()){
		cout << "  ";
		return -1;
	}
	else{
		int t = top->info;
		node *p = top;
		top = top->next;
		delete p;
		return t;
	}
}

void stack::traverese(){

	node *curr;
	for (curr = top; curr; curr = curr->next){
		cout << curr->info;
	}
	cout << endl;
}

int main(){
	int item;
	int size;
	stack x;
	cout << i;
	cout << endl;
	cout << "Enter element size";
	cin >> size;
	for (int i = 0; i < size; i++){
		cin >> item;
		x.push(item);
	}
	
	x.traverese();
	x.pop();
	x.traverese();
	

}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.