// Stack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Node{
public:
int data;
Node *next;
};
class Stack {
public:
Node *top;
Node* current;
Node *bottom;
Stack(){
top=NULL;
current=NULL;
bottom=NULL;
}
void gettop(){
cout<<top->data;
}
void pop(){
if(top==NULL)
{
cout<<"Stack is empty: \n";
}
else{
Node *temp;
temp =top;
top=top->next;
delete(temp);
}
}
void push(){
int n;
cout<<"\nEnter the number you want to push : ";
cin >>n;
if(top==NULL){
Node *temp = new Node;
temp->data=n;
temp->next=NULL;
top=temp;
bottom=temp;
delete(temp);
}
else{
Node *temp = new Node;
temp->data=n;
temp->next=top;
top=temp;
}
}
void display(){
current=top;
while(current!=bottom){
cout<<current->data;
cout<<endl;
current=current->next;
}
cout<<current->data;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Stack s;
int option;
cout<<"Enter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;
while(option!='e'){
switch(option){
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.gettop();
break;
case 4:
s.display();
break;
}
cout<<"\nEnter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;
}
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Node{
public:
int data;
Node *next;
};
class Stack {
public:
Node *top;
Node* current;
Node *bottom;
Stack(){
top=NULL;
current=NULL;
bottom=NULL;
}
void gettop(){
cout<<top->data;
}
void pop(){
if(top==NULL)
{
cout<<"Stack is empty: \n";
}
else{
Node *temp;
temp =top;
top=top->next;
delete(temp);
}
}
void push(){
int n;
cout<<"\nEnter the number you want to push : ";
cin >>n;
if(top==NULL){
Node *temp = new Node;
temp->data=n;
temp->next=NULL;
top=temp;
bottom=temp;
delete(temp);
}
else{
Node *temp = new Node;
temp->data=n;
temp->next=top;
top=temp;
}
}
void display(){
current=top;
while(current!=bottom){
cout<<current->data;
cout<<endl;
current=current->next;
}
cout<<current->data;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Stack s;
int option;
cout<<"Enter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;
while(option!='e'){
switch(option){
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.gettop();
break;
case 4:
s.display();
break;
}
cout<<"\nEnter stack operation\n1: Push()\n2: Pop()\n3: Top\n4: Display Stack\nPress 'e' to exit\n ";
cin >> option;
}
return 0;
}