Thursday, November 3, 2016

A program to copy a queue elements to a new queue keeping the initial queue unchanged. also add the both queues.

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

struct head1
{
    int count1;
    struct node *frnt1;
    struct node *rear1;
}*head1;

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

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

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

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

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

    head->count++;
}

int dequeue()
{
    int x;

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

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

    head->count--;
    return x;
}

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

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

    head1->count1++;
}

void print1()
{
    struct node *temp1;

    temp1=head1->frnt1;

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

    cout<<endl<<"Total number of data in new queue is : "<<head1->count1<<endl;
}

int size_q()
{
    return head->count;
}

void print()
{
    struct node *temp;

    temp=head->frnt;

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

void print2()
{
    struct node *temp;

    temp=head->frnt;

    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl<<"Total number of data after adding the two queues is : "<<head->count+head1->count1<<endl;
}

int main()
{
    create_head();
    create_head1();
    enqueue(35);
    enqueue(31);
    enqueue(12);
    enqueue(98);
    enqueue(65);
    enqueue(9);

    int x=size_q();

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

    }

    print();
    print1();

    head->rear->next=head1->frnt1;
    print2();

    return 0;

}

Tuesday, November 1, 2016

10324 - Zeros and Ones Solution

Problem link-click here

it's an easy implement based string processing problem. just read the problem statement carefully.

an accepted code is as follows:

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

int main()
{
    string str;
    int q=1,n;

    while(cin>>str)
    {
        cout<<"Case "<<q<<":"<<endl;


        bool s;

        cin>>n;

        while(n--)
        {
            int a,b;

            cin>>a>>b;

            if(b<a)
            {
                int temp=a;
                a=b;
                b=temp;
            }

            char ch;
            s=true;

            ch=str[a];

            for(int i=a+1;i<=b;i++)
            {
                if(ch!=str[i])
                {
                    s=false;
                    break;
                }
            }


        if(s)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }

        }

        q++;
    }

    return 0;
}