Saturday 27 August 2016

Stack by linked list

//here is a program that will create a linked stack

#include<iostream>
#include<stdio.h>
using namespace std;
struct linked_lists
{
    int data;
    struct linked_lists *next;
};
typedef struct linked_lists node;
void create(node *p);
void show(node *p);
node* push(node *p);
node* pop(node *p);
int main()
{
    node *head;
    head=new node;
    create(head);
    show(head);
    int c;
    bool x=true;
    while(x)
    {
        cout<<"1 push"<<endl;
        cout<<"2 pop"<<endl;
        cout<<"3 exit"<<endl;
        cin>>c;
        switch(c)
        {
        case 1:
            head=push(head);
            show(head);
            break;
        case 2:
            head=pop(head);
            show(head);
            break;
        case 3:
            x=false;
            break;

        }
    }
    return 0;
}
void create(node *p)
{
    char c;
    scanf("%d%c",&p->data,&c);
    if(c=='\n')
    {
        p->next=NULL;
    }
    else
    {
        p->next=new node;
        create(p->next);
    }
    return;
}
void show(node *p)
{
    if(p==NULL)
    {
        cout<<"empty"<<endl;
    }
    while(p)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
    return;
}
node* push(node *p)
{
    cout<<"value:";
    node *n,*h;
    h=p;
    n=new node;
    cin>>n->data;
    n->next=NULL;
    if(p==NULL){
        p=n;
        return p;
    }
    while(p->next)
    {
        p=p->next;
    }
    p->next=n;
    return h;

}
node* pop(node *p)
{
    node *h;
    h=p;
    //if(p==NULL)
    //{
    //    return NULL;
    //}
    if(p->next==NULL)
    {
        p=p->next;
        return p;
    }
    while(p->next->next!=NULL)
    {
        p=p->next;
    }
    delete p->next;
    p->next=NULL;
    return h;
}

CODEFORCES 709A-Juicer Solution


  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int n,b,d;
  6.     cin>>n>>b>>d;
  7.     int a[n],waste=0,count=0;
  8.     for(int i=0;i<n;i++){
  9.         cin>>a[i];
  10.         if(a[i]>b)
  11.             continue;
  12.         waste+=a[i];
  13.         if(waste>d)
  14.         {
  15.             count++;
  16.             waste=0;
  17.         }
  18.     }
  19.     cout<<count<<endl;
  20.     return 0;
  21. }

Sunday 21 August 2016

Lightoj 1387-Setu solution



  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4.      int t;
  5.      cin>>t;
  6.      for(int i=1;i<=t;i++){
  7.           int n;
  8.           cin>>n;
  9.           int money=0;
  10.           cout<<"Case "<<i<<":\n";
  11.           while(n--){
  12.               string x;
  13.               cin>>x;
  14.               if(x[0]=='d')
  15.                  {
  16.                     int g;
  17.                     cin>>g;
  18.                     money+=g;
  19.                   }
  20.                else
  21.                  cout<<money<<endl;
  22.          }
  23.      }
  24.      return 0;
  25. }

Sunday 14 August 2016

  • SHOWING THE SECOND LAST ELEMENT OF A LINKED LISTS

    Language=C


    #include<stdio.h>
    #include<stdlib.h>

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

    typedef struct linked_lists node;

    void create( node *list );

    void show_before_last( node *list );

    int main()
    {
        node *head;

        head = ( node* ) malloc( sizeof( node ) );

        create( head );
        show_before_last( head );

        return 0;
    }
    void create( node *list )
    {
        char c;

        scanf( "%d%c",&list->data,&c );

        if(c == '\n')

            list->next = NULL;

        else
        {
            list->next = ( node* ) malloc ( sizeof( node ) );

            create( list->next );
        }
        return;
    }
    void show_before_last( node *list )
    {
        while( list->next->next )

            list = list->next;

        printf( "%d\n",list->data );

        return;
    }

Wednesday 3 August 2016

  • SWAPPING TO NUMBERS WITHOUT TAKING ANY TEMPORARY VARIABLE

    /*We generally swap or interchange to variables by taking a temporary variable.Here is a simple program for swapping to numbers without using any intermediate variable*/

    Language=C

    #include<stdio.h>
    int main()
    {
        int a,b;

        printf( "Enter the value of a and b=" );
        scanf( "%d%d",&a,&b );

        printf( "a and b before swapping\n" );
        printf( "%d %d\n",a,b );

        a = a+b;
        b = a-b;
        a = a-b;

        printf( "a and b after swapping\n" );
        printf( "%d %d\n",a,b );

        return 0;
    }

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...