Saturday, October 29, 2016

Take input twenty marks from console and keep it in a file and find total as well as average using file in C++

Corresponding Code is given below:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    double a,x,total=0;

    ofstream out("marks.txt");

    cout<<"Enter 20 marks : ";

    for(x=0;x<20;x++)
    {
        cin>>a;
        out<<a<<endl;
    }

    out.close();

    ifstream in;
    in.open("marks.txt");

    while(!in.eof())
    {
        in>>a;
        total=total+a;

    }

    in.close();

    double average=total/20;

    cout<<"total is : "<<total<<endl<<"average is : "<<average<<endl;

    return 0;
}

Thursday, October 27, 2016

Binary tree implementation (search,preorder,postorder,inorder,insert operations)

There's various way to implement but i have implemented following below systems.

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int data;
    struct node *left;
    struct node *right;

};

struct node* create_node(int data)
{
  struct node* node1 = new node();
    node1->data=data;
    node1->left=NULL;
    node1->right=NULL;

    return node1;
}

struct node* insert(struct node* root, int data)
{
    if(root==NULL)
    {
        root=create_node(data);
    }
    else if(data<root->data)
    {
        root->left=insert(root->left,data);
    }
    else
    {
        root->right=insert(root->right,data);
    }

    return root;
}

void pre_order(struct node* n)
{
    if(n)
    {
        cout<<n->data<<" ";
        pre_order(n->left);
        pre_order(n->right);
    }
}

void post_order(struct node* n)
{
    if(n)
    {
        post_order(n->left);
        post_order(n->right);
        cout<<n->data<<" ";
    }
}

void in_order(struct node* n)
{
    if(n)
    {
        in_order(n->left);
        in_order(n->right);
        cout<<n->data<<" ";
    }
}

bool search(struct node* root,int data)
{
    if(root==NULL)
    {
        return false;
    }
    else if(root->data==data)
    {
        return true;
    }
    else if(data<root->data)
    {
        return search(root->left,data);
    }
    else
    {
        return search(root->right,data);
    }
}

int main()
{
    struct node *root=NULL;

    while(1)
    {

    cout<<"\t------------------------"<<endl;
    cout<<"\t\tTREE"<<endl;
    cout<<"\t------------------------"<<endl;

    int n,a,b;
    cout<<"\t1.INSERT\n\t2.SEARCH\n\t3.PRE_ORDERPRINT\n\t4.POST_ORDERPRINT\n\t5.IN_ORDERPRINT\n\t6.EXIST\n\n"<<endl;
    cin>>n;

    switch(n)
    {
  case 1:
      cout<<"Enter data : ";
      cin>>a;
    root=insert(root,a);
    break;
  case 2:
      cout<<"Data to be searched : ";
    cin>>b;
    if(search(root,b)==true)
    {
        cout<<"Found\n"<<endl;
    }
    else
    {
        cout<<"Not found\n"<<endl;
    }
    break;

  case 3:
    pre_order(root);
    cout<<endl;
    break;
  case 4:
    post_order(root);
    cout<<endl;
    break;
  case 5:
    in_order(root);
    cout<<endl;
    break;
  case 6:
    return 0;


    }
    }
    return 0;
}

Saturday, October 22, 2016

12279 - Emoogle Balance Solution

Problem link-click here

this problem is very easy and for the beginners as well. just read the statements very carefully then i guess you will find the following line and this line is everything what you need to do.

"Emoogle Balance = number of times Emoogle is supposed to give a treat according to the book - number of times he has actually given the treat".

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,i=1;

    while(cin>>t && t!=0)
    {
        int a,c=0,c1=0;

        while(t--)
        {
            cin>>a;

            if(a==0)
            {
                c++;
            }
            else
            {
                c1++;
            }
        }

        cout<<"Case "<<i<<": "<<c1-c<<endl;

        i++;
    }

    return 0;
}

Friday, October 21, 2016

11988 - Broken Keyboard (a.k.a. Beiju Text) solution

Problem link-click here

you have to go through the term List in C++ to solve this problem easily. please visit the link below to learn details about list:

list in c++

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;

    while(cin>>str)
    {
        list<char> l;
        list<char>::iterator iter;
        int i=0;

        iter=l.end();

        while(str[i])
        {
            if(str[i]=='[')
            {
                iter=l.begin();
            }
            else if(str[i]==']')
            {
               iter=l.end();
            }
            else
            {
                l.insert(iter,str[i]);
            }
            i++;
        }

        for(iter=l.begin();iter!=l.end();iter++)
        {
            cout<<*iter;
        }

        cout<<endl;
    }

    return 0;
}

Thursday, October 13, 2016

write a program to check the contents of two queues and return true if they are identical and false if they are not

#include<bits/stdc++.h>
using namespace std;

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

struct head
{
int count=0;
struct node *front;
struct node *rear;
}*head;

struct head1
{
int count1=0;
struct node *front1;
struct node *rear1;
}*head1;

void create_node()
{
node=(struct node*) malloc (sizeof(struct node));
node->next=NULL;
}

void create_head()
{
head=(struct head*) malloc (sizeof(struct head));
head->count=0;
head->front=NULL;
head->rear=NULL;
}

void create_head1()
{
head1=(struct head1*) malloc (sizeof(struct head1));
head1->count1=0;
head1->front1=NULL;
head1->rear1=NULL;
}

void enqueue(int data)
{
    create_node();
    node->data=data;

    if(head->count==0)
    {
        head->front=node;
        head->rear=node;
    }
    else
    {
        head->rear->next=node;
        head->rear=node;
    }

    head->count++;
}

void enqueue1(int data)
{
    create_node();
    node->data=data;

    if(head1->count1==0)
    {
        head1->front1=node;
        head1->rear1=node;
    }
    else
    {
        head1->rear1->next=node;
        head1->rear1=node;
    }

    head1->count1++;
}

int dequeue()
{
int x;

if(head->count==1)
{
x=head->front->data;
free(head->front);
head->front=NULL;
head->rear=NULL;
}
else
{
struct node *temp;
temp=head->front;

x=temp->data;

head->front=temp->next;
free(temp);
}

head->count--;

return x;
}

int dequeue1()
{
int x;

if(head1->count1==1)
{
x=head1->front1->data;
free(head1->front1);
head1->front1=NULL;
head1->rear1=NULL;
}
else
{
struct node *temp;
temp=head1->front1;

x=temp->data;

head1->front1=temp->next;
free(temp);
}

head1->count1--;

return x;
}

void print()
{
struct node *temp;

temp=head->front;

cout<<"The queue1 data are : ";

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head->count<<endl<<endl;
}

void print1()
{
struct node *temp;

temp=head1->front1;

cout<<"The queue2 data are : ";

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head1->count1<<endl<<endl;
}

void process()
{
int s=0;

if(head->count!=head1->count1)
{
cout<<"Not identical"<<endl;
}
else
{
int y=head->count;

while(y--)
{
int j,k;

j=dequeue();
k=dequeue1();

if(j!=k && s==0)
{
s=1;
enqueue(j);
enqueue1(k);
}
else
{
enqueue(j);
enqueue1(k);
}
}

if(s==1)
{
cout<<"\nNot Identical"<<endl<<endl;
}
else
{
cout<<"\nIdentical"<<endl<<endl;
}
}
}

int main()
{
create_head();
create_head1();

cout<<"Before exccution : "<<endl;

enqueue(76);
enqueue(86);
enqueue(98);
enqueue(65);
enqueue(9);
enqueue(65);
enqueue(43);
print();

enqueue1(64);
enqueue1(86);
enqueue1(98);
enqueue1(65);
enqueue1(9);
enqueue1(65);
enqueue1(43);
print1();
process();

cout<<"\nAfter exccution : "<<endl;

print();
print1();

return 0;
}


write a program to create a stack from a queue. at the end of the program the queue should be unchanged.

#include<bits/stdc++.h>
using namespace std;


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

struct head
{
int count=0;
struct node *front;
struct node *rear;
}*head;

struct head1
{
int count1;
struct node *next1;
}*head1;

struct head2
{
int count2;
struct node *next2;
}*head2;

void create_node()
{
node=(struct node*) malloc (sizeof(struct node));
node->next=NULL;
}

void create_head()
{
head=(struct head*) malloc (sizeof(struct head));
head->count=0;
head->front=NULL;
head->rear=NULL;
}

void create_head1()
{
head1=(struct head1*) malloc (sizeof(struct head1));
head1->count1=0;
head1->next1=NULL;
}

void create_head2()
{
head2=(struct head2*) malloc (sizeof(struct head2));
head2->count2=0;
head2->next2=NULL;
}

void enqueue(int data)
{
    create_node();
    node->data=data;

    if(head->count==0)
    {
        head->front=node;
        head->rear=node;
    }
    else
    {
        head->rear->next=node;
        head->rear=node;
    }

    head->count++;
}

int dequeue()
{
int x;

if(head->count==1)
{
x=head->front->data;
free(head->front);
head->front=NULL;
head->rear=NULL;
}
else
{
struct node *temp;
temp=head->front;

x=temp->data;

head->front=temp->next;
free(temp);
}

head->count--;

return x;
}

void push1(int x)
{
create_node();
node->data=x;

node->next=head1->next1;
head1->next1=node;

head1->count1++;
}

int  pop()
{
int x;

if(head1->count1==1)
{
x=head1->next1->data;
free(head1->next1);
head1->next1=NULL;
}
else
{
struct node *temp;

temp=head1->next1;
x=temp->data;
head1->next1=temp->next;
free(temp);
}

head1->count1--;
return x;
}

void push2(int x)
{
create_node();
node->data=x;

node->next=head2->next2;
head2->next2=node;

head2->count2++;
}

void print()
{
struct node *temp;

temp=head->front;

cout<<"The queue data are : ";

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head->count<<endl<<endl;
}

void print1()
{
struct node *temp;

temp=head1->next1;

cout<<"The stack 1 data are : ";

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head1->count1<<endl<<endl;
}

void print2()
{
struct node *temp;

temp=head2->next2;

cout<<"The stack2 data are : ";

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head2->count2<<endl<<endl;
}

void process()
{
int x=head1->count1;

while(x--)
{
push2(pop());
}
}


int main()
{
create_head();
create_head1();
create_head2();

enqueue(35);
enqueue(43);
enqueue(856);
enqueue(65);
enqueue(2);
enqueue(32);
enqueue(23);
print();

int x=head->count;

while(x--)
{
int y=dequeue();
push1(y);
enqueue(y);
}

print1();
process();
print2();
print();

}

Write a program to create a queue from a stack. After the queue has been created,the top of the stack should be the front of the queue and the base of the stack should be the rear of the queue

#include<bits/stdc++.h>
using namespace std;

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

struct head
{
int count;
struct node *p;
}*head;

struct head1
{
int count1;
struct node *front;
struct node *rear;
}*head1;

void create_node()
{
node=(struct node*) malloc (sizeof(struct node));
node->next=NULL;
}

void create_head()
{
head=(struct head*) malloc (sizeof(struct head));
head->count=0;
head->p=NULL;
}

void create_head1()
{
head1=(struct head1*) malloc (sizeof(struct head1));
head1->count1=0;
head1->front=NULL;
head1->rear=NULL;
}

void push(int x)
{
create_node();
node->data=x;

node->next=head->p;
head->p=node;

head->count++;
}

int  pop()
{
int x;

if(head->count==1)
{
x=head->p->data;
free(head->p);
head->p=NULL;
}
else
{
struct node *temp;

temp=head->p;
x=temp->data;
head->p=temp->next;
free(temp);
}

head->count--;
return x;
}

void print()
{
struct node *temp;

temp=head->p;

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head->count<<endl<<endl;
}

void enqueue(int data)
{
    create_node();
    node->data=data;

    if(head1->count1==0)
    {
        head1->front=node;
        head1->rear=node;
    }
    else
    {
        head1->rear->next=node;
        head1->rear=node;
    }

    head1->count1++;
}

void print1()
{
struct node *temp;

temp=head1->front;

cout<<"The queue data are : ";

while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}

cout<<"\ntotal data "<<head1->count1<<endl;
cout<<"Front of the queue : "<<head1->front->data<<endl;
cout<<"The rear of the queue : "<<head1->rear->data<<endl<<endl;
}



int main()
{
create_head();
create_head1();

push(16);
push(46);
push(54);
push(75);
push(54);
push(64);
push(43);
push(76);
cout<<"\nBefore constructing queue the stack data are : "<<endl;
print();


    int x=head->count;

    while(x--)
{
enqueue(pop());
}

print1();

cout<<"After constructing queue the stack data are : "<<endl;
print();

return 0;
}

Program to delete all the negative numbers from a queue without changing the order of other elements (using linked list)

#include<bits/stdc++.h>
using namespace std;

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

struct head
{
    int count;
    struct node *front;
    struct node *rear;
}*head;

void create_head()
{
    head=(struct head*) malloc (sizeof(struct head));
    head->count=0;
    head->front=NULL;
    head->rear=NULL;
}

void create_node()
{
    node=(struct node*) malloc (sizeof(struct node));
    node->next=NULL;
}

void enqueue(int data)
{
    create_node();
    node->data=data;

    if(head->count==0)
    {
        head->front=node;
        head->rear=node;
    }
    else
    {
        head->rear->next=node;
        head->rear=node;
    }

    head->count++;
}

void dequeue(struct node *temp)
{
    struct node *temp1;

    temp1=temp->next;
    temp->next=temp1->next;
    free(temp1);
    temp=temp->next;
    head->count--;
}

void main_process()
{
    struct node *temp;

    temp=head->front;

    while(temp->next!=NULL)
    {
        if(temp->next->data<0)
        {
            dequeue(temp);
        }
        else
        {
            temp=temp->next;

        }

    }
}

void print()
{
    struct node *temp;
    temp=head->front;

    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<"\ntotal number of data : "<<head->count<<endl;
}

int main()
{
    create_head();
    enqueue(65);
    enqueue(54);
    enqueue(-3);
    enqueue(8);
    enqueue(-7);
    enqueue(43);
    enqueue(-98);
    enqueue(14);
    enqueue(65);
    enqueue(-76);
    cout<<"\nBefore desired operation : ";
    print();
    cout<<endl;
    main_process();
    cout<<"After desired operation : ";
    print();
    cout<<endl;
    return 0;
}

Monday, October 10, 2016

UVA 12049 - Just Prune The List Solution

Problem link-Click here

I have solved this problem using MAP and SET of STL. so go through the map and set then try to see the code otherwise it's almost impossible to understand my code.

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,t;

    scanf("%d",&t);

    while(t--)
    {
        cin>>n>>m;

        map<int,int> m1;
        map<int,int> m2;
        set<int> s;
        set<int>::iterator it;
        int x,y;

        while(n--)
        {
            cin>>x;
            m1[x]++;
            s.insert(x);

        }

        while(m--)
        {
            cin>>y;
            m2[y]++;
            s.insert(y);
        }

        int sum=0;

        for(it=s.begin();it!=s.end();it++)
        {
            if(m1[*it]!=m2[*it])
            {
                sum+= abs(m1[*it]-m2[*it]);
            }
        }

        cout<<sum<<endl;
    }

    return 0;
}

Sunday, October 9, 2016

UVA 12895 - Armstrong Number Solution

problem link-click here


#include<bits/stdc++.h>
using namespace std;

int power(int x,int y)
{
    int sum=x;

    for(int i=1;i<y;i++)
    {
        sum=sum*x;
    }

    return sum;
}

int main()
{
    char str[20];

    int n;

    cin>>n;

    while(n--)
    {
        cin>>str;
        long long int sum=0,s;
        s=atoi(str);

        for(int i=0;i<strlen(str);i++)
        {
            sum=sum+power((str[i]-48),strlen(str));
        }

        if(sum==s)
        {
            cout<<"Armstrong"<<endl;
        }
        else
        {
            cout<<"Not Armstrong"<<endl;
        }
    }

    return 0;
}

Friday, October 7, 2016

UVA 10420 - List of Conquests Solution

Problem link-Click here

You have to go through MAP and SET of STL in C++. So i will recommend you to go through those terms carefully and seeing this code otherwise it's impossible to understand the following code.

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;

    string str,h;
    map<string,int> m;
    set<string> s;
    set<string>::iterator iter;

    cin>>n;
    cin.ignore();

    while(n--)
    {
        cin>>h;
        getline(cin,str);

        m[h]++;
        s.insert(h);
    }

    for(iter=s.begin();iter!=s.end();iter++)
    {
        cout<<*iter<<" "<<m[*iter]<<endl;
    }


    return 0;
}

UVA 445 - Marvelous Mazes Solutions

problem link - click here

please read the statement carefully then you must get a way to solve it. you can use multiple if else statements to solve this problem or can use switch to solve. there will be given a string and you have to create a maze. the conditions are given so follow that. remember only A-Z, *(asterikh), digits (0 to 9) will be used as input. so think about these and forget handling the others.

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;

    while(getline(cin,str))
    {
        int count=0;

        for(int i=0;i<str.length();i++)
        {
            switch(str[i])
            {
                case 'b':
                    while(count--)
                    {
                        cout<<" ";
                    }
                    count = 0;
                    break;

                case '1':
                    count = count+1;
                    break;
                case '2':
                    count = count+2;
                    break;
                case '3':
                    count = count+3;
                    break;
                case '4':
                    count = count+4;
                    break;
                case '5':
                    count = count+5;
                    break;
                case '6':
                    count = count+6;
                    break;
                case '7':
                    count= count+7;
                    break;
                case '8':
                    count = count+8;
                    break;
                case '9':
                    count = count+9;
                    break;
                case '!':
                    cout<<endl;
                    break;
                default:
                    while(count--)
                    {
                        cout<<str[i];
                    }

                    count = 0;

            }

        }

        cout<<endl;
    }
    return 0;
}

Linked list implementation code in c++

#include<bits/stdc++.h>
using namespace std;

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

struct head
{
    int count;
    struct node *p;
}*head;

void create_node(int data)
{
    node=(struct node*)malloc(sizeof(struct node));
    node->data=data;
    node->next=NULL;
}

void create_head()
{
    head=(struct head*)malloc(sizeof(struct head));
    head->p=NULL;
    head->count=0;
}

void insert(int data)
{
    create_node(data);

    int n,n1;
    cout<<"1.1st position\n2.last position\n3.nth position\n";
    cout<<"Enter your choice : ";
    cin>>n;

    switch(n)
    {
    case 1:

        node->next=head->p;
        head->p=node;
        break;

    case 2:

        struct node *temp;
        temp=head->p;

        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        temp->next=node;
        break;

    case 3:

        cout<<"Enter position to insert : ";
        cin>>n1;

        struct node *temp1;
        temp1=head->p;

        while(n1>2)
        {
            temp1=temp1->next;
            n1--;
        }

        node->next=temp1->next;
        temp1->next=node;
        break;
    }

    head->count++;
}

void print()
{
    struct node *temp;

    temp=head->p;

    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }

    cout<<endl<<"Total number of data is : "<<head->count<<endl<<endl;
}

void insert_anywhere(int data)
{
    int data1,n;

    cout<<"Enter a target data : ";
    cin>>data1;
    cout<<"1.Before\n2.After\n";
    cout<<"Enter your choice : ";
    cin>>n;

    create_node(data);

    switch(n)
    {
    case 1:
        int s;
        s=0;
        struct node *temp;
        temp=head->p;

        while(temp->next->data!=data1)
        {
            temp=temp->next;
        }

        node->next=temp->next;
        temp->next=node;

        break;

    case 2:
        temp=head->p;

        while(temp->next!=NULL && temp->data!=data1)
        {
            temp=temp->next;
        }

        node->next=temp->next;
        temp->next=node;

        break;
    }

    head->count++;
}

bool search(int data)
{
    struct node *temp;
    int s=0;
    temp=head->p;

    while(temp!=NULL)
    {
        if(temp->data==data)
        {
            s=1;
            break;
        }
        temp=temp->next;
    }

    if(s==0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

void vanish_or_destroy()
{
    free(head->p);
    head->p=NULL;
    head->count=0;

    print();
}

void delete1()
{
    int n;
    cout<<"1.delete 1st element\n2.delete last element\n3.delete nth element"<<endl;
    cout<<"Enter your choice : ";
    cin>>n;

    int c,n1;


    switch(n)
    {
    case 1:
         struct node *temp;
         temp=head->p;
         head->p=temp->next;
         free(temp);
        break;

    case 2:

         temp=head->p;

         while(temp->next->next!=NULL)
         {
             temp=temp->next;
         }
        free(temp->next);
        temp->next=NULL;

        break;

    case 3:

        cout<<"Enter the position : ";
        cin>>n1;

        temp=head->p;
        c=1;

        while(c<(n1-1))
        {
            temp=temp->next;
            c++;
        }

        struct node *temp1;
        temp1=temp->next;
        temp->next=temp1->next;
        free(temp1);

        }

        head->count--;

}

void delete_anywhere()
{
    int n;

    cout<<"Enter a number to delete : ";
    cin>>n;

    struct node *temp;

    if(search(n)==true)
    {
        temp=head->p;

        while(temp->next->data!=n)
        {
            temp=temp->next;
        }

        struct node *temp1;

        temp1=temp->next;
        temp->next=temp1->next;
        free(temp1);
    }
    else
    {
        cout<<"Data isn't exist"<<endl;
    }
    head->count--;
}

int main()
{
    int n;

    create_head();

    while(1)
    {
        cout<<"----->>>>LINKED LIST OPERATIONS<<<<-----"<<endl;
    cout<<"\t1.INSERT\n\t2.DELETE\n\t3.SEARCH\n\t4.PRINT\n\t5.VANISH\n\t6.INSERT ANYWHERE\n\t7.DELETE ANYWHERE\n\t8.EXIT\n\n";
    cout<<"ENTER YOUR CHOICE : ";
    cin>>n;

    switch(n)
    {
    case 1:
        int data;
        cout<<"ENTER A DATA TO INSERT : ";
        cin>>data;

        insert(data);
        break;

    case 2:
        delete1();
        break;

    case 3:
        cout<<"ENTER A NUMBER TO SEARCH : ";
        cin>>data;
        if(search(data)==true)
        {
          cout<<"FOUND"<<endl;
        }
        else
        {
            cout<<"NOT FOUND"<<endl;
        }
        break;

    case 4:

        print();
        break;

    case 5:
        vanish_or_destroy();
        break;

    case 6:
        cout<<"ENTER A DATA TO INSERT : ";
        cin>>data;
        insert_anywhere(data);
        break;

    case 7:
        delete_anywhere();
        break;

    case 8:
        exit(0);
    }

    }
    return 0;
}

Queue implementation using linked list in C++

I have tried to implement all the queue operations in a single code using linked list. If you are not familiar with linked list then you are suggested to look at my previous post illustrating linked list only. thank you.

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int data;
    struct node *next;

}*node;

struct head
{
    int count;
    struct node *frnt;
    struct node *rear;
}*head;

void create_node()
{
    node = (struct node*) malloc (sizeof(struct node));
    node->next = NULL;
}

void create_head()
{
    head = (struct head*) malloc (sizeof(struct head));
    head->count = 0;
    head->frnt = NULL;
    head->rear = NULL;
}

void enqueue(int data)
{
    create_node();

    if(head->count==0)
    {
        node->data = data;
        head->frnt = node;
        head->rear = node;
    }
    else
    {
        node->data = data;
        head->rear->next = node;
        head->rear = node;
    }

    head->count++;
}

void dequeue()
{
    if(head->count==1)
    {
        free(head->frnt);
        head->frnt = NULL;
        head->rear = NULL;
    }
    else
    {
        struct node *temp;
        temp = head->frnt;
        head->frnt = temp->next;
        free(temp);
    }

    head->count--;
}

void print()
{
    struct node *temp;

    temp = head->frnt;

    cout<<"\tThe data are : ";

    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp = temp->next;
    }
    cout<<endl;

    cout<<"\tTotal number of data in queue is : "<<head->count<<endl;
}

void isempty()
{
    if(head->count==0)
    {
        cout<<"\tThe queue is empty"<<endl;
    }
    else
    {
        cout<<"\tThe queue has currently "<<head->count<<" number of data"<<endl;
    }
}

int size()
{
    cout<<"\tThe size of the queue is : "<<head->count<<endl;
}

void front()
{
    cout<<"\tThe front data is : "<<head->frnt->data<<endl;
}

void rear()
{
    cout<<"\tThe rear data is : "<<head->rear->data<<endl;
}

void swap1(int a,int b)
{
    struct node *temp,*temp1,*temp2;
    int s=0,s1=0;

    temp = head->frnt;

    while(temp!=NULL)
    {
        if(temp->data==a)
        {
            s=1;
            temp1 = temp;

        }

        if(temp->data == b)
        {
            s1=1;
            temp2 = temp;
        }

        temp = temp->next;
    }

    if(s==1 && s1==1)
    {
        cout<<"\n\tBefore Swapping :\n";
        print();
        temp1->data = b;
        temp2->data = a;
        cout<<"\n\tAfter Swapping :\n";
        print();
    }
    else
    {
        cout<<"\n\tOOPS!!!!!!!! Not Found."<<endl;
    }
}

void destroy()
{
    while(head->count!=0)
    {
        dequeue();
    }

}


int main()
{
    create_head();
    int n,s,a,b;

    cout<<"\t************************"<<endl;
    cout<<"\tOVERALL QUEUE OPERATIONS"<<endl;
    cout<<"\t************************"<<endl;

    while(1)
    {
    cout<<"\n\t->1.Enqueue\n\t->2.Dequeue\n\t->3.Destroy\n\t->4.Head->front\n\t->5.Head->rear\n\t->6.Queue size\n\t->7.Isempty Or Not\n\t->8.Swap two numbers\n\t->9.Print Queue\n\t->10.Exit\n"<<endl;
    cout<<"\tPlease enter your choice : ";
    cin>>n;

    switch(n)
    {
    case 1:
        cout<<"\n\tEnter a data to enqueue : ";
        cin>>s;
        enqueue(s);
        break;
    case 2:
        dequeue();
        break;
    case 3:
        destroy();
        break;
    case 4:
        front();
        break;
    case 5:
        rear();
        break;
    case 6:
        system("COLOR D5");
        size();
        break;
    case 7:
        isempty();
        break;
    case 8:
        cout<<"\n\tPlease enter two numbers to swap : ";
        cin>>a>>b;
        swap1(a,b);
        break;
    case 9:
        system("COLOR 6C");
        print();
        break;
    case 10:
        system("COLOR E9");
        exit(0);
    default:
        cout<<"OOPS! Follow input criterion."<<endl;
        break;
    }

    }

    return 0;
}

Thursday, October 6, 2016

UVA 146 - ID Codes Solution

#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;

    while(cin>>s && s!="#")
    {
        if(next_permutation(s.begin(),s.end()))
        {
            cout<<s<<endl;
        }
        else
        {
            cout<<"No Successor"<<endl;
        }
    }

    return 0;
}

Tuesday, October 4, 2016

UVA 11991 - Easy Problem from Rujia Liu? Solution

Problem link-Click here

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

vector<int> v[1000015];

int main()
{
    int n,m;

    for(int i=0;i<1000015;i++)
    {
        v[i].clear();
    }

    while(cin>>n>>m)
    {
        int i=0,t,k,u;

        while(n--)
        {
            cin>>t;
            v[t].push_back(i+1);
            i++;
        }

        while(m--)
        {
           cin>>k>>u;

            if(v[u].size()<k)
            {
                cout<<0<<endl;
            }
            else
            {
                cout<<v[u][k-1]<<endl;
            }
        }
    }

    return 0;
}

Sunday, October 2, 2016

UVA 10340 - All in All Solutions

Problem link-click here

it's an easy brute force implemented base string problem. read the problem statement carefully and go according to it.

an accepted code is as follows:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
    string s,s1;
    while(cin>>s)
    {
        int l,l2,z=0,x,y=0;
        cin>>s1;
         l=s.length();
         l2=s1.length();
        for(x=0; x<l2; x++)
        {
            if(s[y]==s1[x])
            {
                y++;
                z++;
            }
        }
        if(l==z)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

Saturday, October 1, 2016

UVA 11728 - Alternate Task Solution

problem link-click here

This problem is easy to implement if you can understand it clearly. 1st of all pre-calculate the factors sum till 1000 and keep the sum in the corresponding index. for example, suppose the sum of the factors of 101 is 1+101 = 102. so keep array[101] = 102. similarly keep 1 to 1000 all the factors sum to its corresponding index. now for a number N just check for 1000 to 1 iteratively which index's sum is equal to N. print the index number and if not found just print -1.

an accepted code is as follows:

#include<bits/stdc++.h>
using namespace std;

vector<int> v;


void pre()
{
    for(int i=1;i<=1000;i++)
    {
        int sum=0;

        for(int j=1;j<=i;j++)
        {
            if(i%j==0)
            {
                sum=sum+j;
            }
        }

        v.push_back(sum);
    }
}

int main()
{
    pre();

    int n,q=1;

    while(cin>>n && n)
    {
        int s=0,p;

        for(int i=v.size();i>=0;i--)
        {
            if(v[i]==n)
            {
                s=1;
                p=i+1;
                break;
            }
        }

        if(s==1)
        {
            printf("Case %d: %d\n",q,p);
        }
        else
        {
            printf("Case %d: %d\n",q,-1);
        }

        q++;

    }

    return 0;
}