Friday, April 17, 2015

Stack Implementation using Single Link List

Stack Implementation using Single Link List

#include <iostream>
using namespace std;
struct Node{
int value;
Node *next;

};

int main(){

for( int i=0;i<=79;i++){
cout<<'*';
}
cout<<"                     Built & Designed by Arslan Malik Aareez"<<endl;
cout<<"                          www.CWorldbyAS.blogspot.com"<<endl;
cout<<"                     Facebook : www.facebook.com/arslan.ud.din"<<endl;
cout<<"                     Twitter  : www.twitter.com/itsaareez"<<endl;
for( int i=0;i<=79;i++){
cout<<'*';
}

int menu;
int sel;

struct Node *p = new Node;
struct Node *head = new Node;
struct Node *r = new Node;
head->next = NULL;
head->value=0;
    ask:
cout<<"Enter Your Choice\n"<<endl;
    cout<<"1. Push"<<endl;
    cout<<"2. Pop"<<endl;
    cout<<"3. Print"<<endl;
    cout<<"4. Exit"<<endl;
    cin>>menu;
    system("cls");
switch (menu){
case 1:
cout<<"Enter Value to Push : ";
cin>>p->value;
p->next = NULL;
system("cls");
head = p;
cout<<"Do you want to push another value?"<<endl;
cout<<"1. Yes"<<endl;
cout<<"2. No"<<endl;

cin>>sel;
system("cls");
    while (sel!=2){
     struct Node *q = new Node;
      cout<<"\nEnter Value to Push : ";    
cin>>q->value;
q->next=NULL;
p->next = q;
     p = p->next;
     system("cls");
        cout<<"Do you want to push another value?"<<endl;
        cout<<"1. Yes"<<endl;
        cout<<"2. No"<<endl;
        cin>>sel;
        system("cls");

    }
    goto ask;

break;
case 2:
p = head;
if (p->next==NULL && p->value == 0){
cout<<"\nStack is Empty\n\n";
}
while (p->next!=NULL){
r = p;
p = p->next;
}
r->next = NULL;
p->value = 0;

goto ask;

break;
case 3:
system("cls");
p= head;

cout<<"Stack contains the following values "<<endl;

while (p->next!=NULL){
if (p->value==0)
{
cout<<" ";
}
else
{
cout<<" "<<p->value;
}

p=p->next;

}
cout<<" "<<p->value;
cout<<"\n";
goto ask;

break;

case 4:
return 0;
break;
}


}

No comments:

Post a Comment