Monday, 1 July 2013

source code: Stack using C++ [user friendly]

#include <iostream>
using namespace std;

int top=-1;
int stack[100];


void stklist()
{
    cout <<"\nstack you just made is:\n";
cout <<"| ___ |\n";

    for(int i=top;i>=0;i--)
       cout <<"|_ " <<stack[i] <<" _|\n";

cout <<"| ___ |\n";
}


void check(int length)
{
if(top+1 ==length)
    {
       cout <<"stack OVERFLOW WARING!!\n";
       return;
    }
else if (top==0)
{
cout <<"stack is emply :/\n";
}
}


void push()
{
bool proceed= true;
int data, length, p;

cout <<"\n\nwhat would be the lenth?\nans: ";
cin>> length;

check(length);

while(proceed)
{
cout <<"wat u wanna put into the stack?\nans: ";
cin>> data;

stack[++top]=data;

cout <<"any more?\n press 1 for yes n 0 for no:";
cin>> p;

try

{
if(p>1 || p<0)
throw 1;
else{
proceed=p;
}
}

catch(...)
{
cout <<"como man, it says 1 or 0, dont be a douch!\n";
cout <<"do it straight now, got any more?\n press '1' for YES or '0' for NO:";
cin>> p;
}
stklist();
}
}


int pop()
{
    int temp;
   
if(top==-1)
    {
       cout <<"stack is in underflow state. ";
       return(-1);
    }
   
temp= stack[top];
    top--;
    return temp;
}


int main()
{  
int k;
char ch;
while(1)
{
cout <<"1: push\n2: pop\n3: exit.\n";
cout <<"enter ur choice: "; cin >> ch;

switch(ch)
{
case '1':
{
push();
break;
}

case '2':
{
cout << "data poped: " << pop();
stklist();
break;
}

case '3':
cout <<"\n\n\n\n\nthank you\n\n\n";
return 3;
}

cout <<"\n\nrepeating\n";
} ;

}

No comments:

Post a Comment