Friday, April 17, 2015

Queues Implementation using Single Link List

Queues Implementation using Single Link List

#include <iostream>
using namespace std;
struct Node {
int value;
Node *next;
};
int main(){
int menu;
int sel;
int count=0;
struct Node *p = new Node;
struct Node *head = new Node;

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<<'*';
}

    ask:
    
cout<<"1. Insert\n2. Delete\n3. Print\n4. Exit"<<endl;
    cin>>menu;
    switch(menu){
     case 1:
     count++;
     cout<<"Enter Value to insert : ";
     cin>>p->value;
     p->next = NULL;
     head = p;
     system("cls");
     cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
     cin>>sel;
            system("cls");
     while (sel!=2){
  struct Node *q = new Node;
count++;
     cout<<"Enter Value to insert : ";
     cin>>q->value;
         q->next = NULL;
     p->next = q;
     p = p->next;
     system("cls");
     cout<<"Do you want to enter any other value?\n\n1. Yes\n2. No";
     cin>>sel;
     system("cls");
     }
     goto ask;
     break;
     case 2:
     count--;
     p = head;
     head = p->next;
     p->value = 0;
     p->next = NULL;
     system("cls");
     goto ask;
     break;
    
     case 3:
     system("cls");
     p = head;
     if (count==0){
         system("cls");

cout<<"Queue is empty.";
     }
     else{
cout<<"Your queue has the following values"<<endl;
    
     while (p->next!=NULL){
     cout<<" "<<p->value;
     p = p->next;
     }
     cout<<" "<<p->value;
     }
     cout<<"\n\n";
system("pause");
      system("cls");
goto ask;
break;
    
     case 4:
     return 0;
     break;
    }


}

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;
}


}