Implementation of stack using C++

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class stack
{
    int *a,top,size;
    public:stack(int n)
    {
        top=-1;size=n;
        a=new int [n];
    }
    friend stack operator + (stack , int );
    friend stack operator — (stack);
    friend int empty (stack);
    friend int overflow(stack);
    friend ostream &operator <<(ostream& , stack);

};
stack operator + (stack s1,int num)
{
    s1.a[++s1.top]=num;
    return s1;
}
stack operator — (stack s1)
{
    //clrscr();
    cout<<“\nThe deleted Item is : “<<s1.a[s1.top–];
    return s1;
}
int overflow(stack s1)
{
    if(s1.top==s1.size-1)
     return 1;
    else
    return 0;
}
int empty(stack s1)
{
    if(s1.top==-1)
     return 1;
    else
    return 0;
}
ostream &operator <<(ostream &print,stack s1)
{
    if(empty(s1))
     cout<<“\nThe stack Empty”;
    else
    {
        cout<<“\nThe Elements of stack are : “;
        for(int i=s1.top;i>=0;i–)
        {
            cout<<s1.a[i]<<”  “;
        }
    }
    return print;
}
int main()
{
    int n,choice;
    int num;
    //clrscr();
    cout<<“\nEnter the stack size: “;
    cin>>n;
    stack s1(n);
    while(1)
    {
        cout<<“\n\n\n*****\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.EXIT*****”;
        cout<<“\nEnter the choice: “;
        cin>>choice;
        switch(choice)
        {
            case 1:
              if(overflow(s1))
               cout<<“\nStack Overflow!!!Pop out the Elements\n”;
              else
              {
                  cout<<“\nEnter the number to be added to the stack: “;
                  cin>>num;
                  s1=s1+num;
              }
              break ;
              case 2:if(empty(s1))
                     cout<<“\nEmpty Stack”;
                     else
                     {
                         s1=–s1;
                     }
                     break ;
              case 3:cout<<s1;
                     break ;
              default: exit(0);
        }
    }
}

Advertisements

Link lists:11 functions

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

bool emptytest;//TO TEST FOR EMPTY LIST
typedef struct node* NODE;
struct node
{
    int info;
    NODE link;
};//dont miss ; at the end of the structure

NODE allocate(NODE temp)//function to allocate memory for created nodes
{
    temp=(NODE)malloc(sizeof(struct node));
    return temp;
    /*no need to return temp as the operation is performed on the pointer (all elements of type NODE
      are pointers ), the change is observed implicitly)*/
}
void field()
{
    int i;
    printf(“\n\t\t”);
    for( i=0;i<10;i++)
     printf(“*”);
    printf(“\n”);
}
NODE insertfront(NODE first,int item)

{
    emptytest=false;
    NODE temp;
    temp=allocate(temp);

    temp->info=item;
    temp->link=first;
    return temp;

}
NODE deletefront(NODE first)
{
    NODE temp;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO Nodes Exist”);
        field();

        return first;
    }

    temp=allocate(temp);
    temp=first;

    first=first->link;
    field();
    printf(“The first node with item %d is deleted”,temp->info);
    field();
    free(temp);
    if(first==NULL)
    {
        emptytest=true;
    }
    return first;

}
NODE insertrear(NODE first,int item)
{
    NODE current,temp;
    current=allocate(current);
    emptytest=false;
    if(first==NULL)
    {

        current->info=item;
        return current;

    }
    temp=allocate(temp);
    current=first;
    while(current->link!=NULL)
    {
        current=current->link;
    }
    current->link=temp;
    temp->info=item;
    temp->link=NULL;
    return first;

}
NODE deleterear(NODE first)
{
    NODE current,prev;
    if(first==NULL)
    {

        field();
        printf(“\n\tNo elements in the List”);
        field();
        return first;
    }
    if(first->link==NULL)
    {
        field();
        printf(“\t\tOnly one node was present and is deleted “);
        printf(“\nThe data in the field was: %d”,first->info);
        field();

        free(first);
        return NULL;
    }
    current=allocate(current);
    prev=allocate(prev);
    current=first;
    prev=NULL;
    while(current->link!=NULL )
    {
        prev=current;
        current=current->link;

    }
    prev->link=NULL;
    field();
    printf(“\tTHE LAST NODE WITH INFORMATION %d is deleted”,current->info);
    free(current);
    if(first==NULL)
    {
        emptytest=true;//you can make use of these flag values in the program if you want to
    }
    return first;

}
void display(NODE first)
{

    NODE current;
    current=allocate(current);
    current=first;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO NODES EXIST”);
        field();
        return ;
    }
    printf(“\n”);

    while(current->link!=NULL)
    {
        printf(“|%d|->”,current->info);
        current=current->link;
    }
    printf(“|%d|”,current->info);
    printf(“\n\n”);
}
void countnode(NODE first)
{
    NODE current;
    int count=0;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO nodes Exist”);
        field();
        return ;
    }
    current=allocate(current);
    current=first;
    while(current!=NULL)
    {
        current=current->link;
        ++count;
    }
    field();
    printf(“\t\tThe no.of.nodes in the list=%d”,count);
    field();
}
void searchelement(NODE first,int key)
{
    NODE temp;
    int count=0;
    if(first==NULL)
    {
        field();
        printf(“\t\tThe List is Empty “);
        field();
        return ;
    }
    temp=allocate(temp);
    temp=first;
    while(temp)
    {
        count++;
        if(temp->info==key)
        {
            field();
            printf(“\t\tThe key found at location no %d in the list “,count);
            return ;
        }
        temp=temp->link;
    }
    field();
    printf(“Key not found”);
    field();
}

NODE reverse(NODE first)
{
    NODE previous,current;
    char* ch;
    if(first==NULL)
    {
        field();
        printf(“\t\tList is Empty”);
        field();
        return ;
    }
    previous=allocate(previous);
    current=allocate(current);
    previous=NULL;
    while(first!=NULL)
    {
        current=first;
        first=first->link;
        current->link=previous;
        previous=current;
    }
    field();

   return previous;
}
NODE deleteinfo(NODE first,int key)
{
    NODE cur,prev;

    if(first==NULL)
    {
        field();
        printf(“\t\tList empty”);
        field();
        return first;
    }
    cur=allocate(cur);
    if(first->info==key)
    {
        cur=first;
        first=first->link;
        free(cur);
        field();
        printf(“\t\tThe element was in first node and is deleted “);
        field();
        return first;
    }
    prev=allocate(prev);
    prev=NULL;
    cur=first;
    while(cur!=NULL && cur->info!=key)
    {
        prev=cur;
        cur=cur->link;

    }
    if(cur==NULL)
    {
        field();
        printf(“\t\tElement not found “);
        field();
        return first;
    }
    prev->link=cur->link;
    free(cur);
    field();
    printf(“\t\tThe node with item %d is deleted”,key);
    field();
    return first;
}
NODE insertnodepos(NODE first,int pos,int element)
{
    NODE temp,prev,cur;
    int count=1;
    temp=allocate(temp);
    temp->info=element;
    temp->link=NULL;
    if(first==NULL && pos==1)
    {
        return temp;
    }
    if(pos==1)
    {
        temp->link=first;
        return temp;
    }
    prev=NULL;
    cur=first;
    while(cur!=NULL && count!=pos)
    {
        prev=cur;
        cur=cur->link;
        count++;
    }
    printf(“\ncount=%d”,count);
    if(count!=pos)
    {
        field();
        printf(“\t\tINVALID POSITION”);
        field();
        free(temp);
        return first;
    }
    prev->link=temp;
    temp->link=cur;
    return first;
}
NODE delposnode(NODE first,int pos)
{
    int count=1;
    NODE cur,prev;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO NODES EXIST”);
        field();
        return NULL;
    }
    cur=allocate(cur);
    if(pos==1)
    {
        cur=first;
        first=first->link;
        free(cur);
        field();
        printf(“\t\tThe first node is freed”);
        field();
        return first;
    }
    prev=allocate(prev);
    prev=NULL;
    cur=first;
    //printf(“hi”);
    while(cur!=NULL && count!=pos)
    {
        prev=cur;
        cur=cur->link;
        count++;
    }
    printf(“\ncount: %d pos:%d”,count,pos);
    if(cur==NULL)
    {
        field();
        printf(“\t\tInvalid Entry”);
        field();
        return first;
    }
    prev->link=cur->link;
    free(cur);
    field();
    printf(“\t\tThe node at position %d is deleted “,pos);
    field();
    return first;
}
int main()
{

   NODE first;
   int choice,item,key;

   first=allocate(first);
   first=NULL;//VERY IMPORTANT INITIALIZATION
   emptytest=true;//INDICATES THAT THE LIST IS EMPTY
   while(1)
   {
      printf(“\n\t********************”);
       printf(“\n\t*1.INSERT FRONT  *******”);
       printf(“\n\t*2.DELETE FORNT  *     *”);
       printf(“\n\t*3.INSERT REAR   *     *”);
       printf(“\n\t*4.DELETE REAR   *     *”);
       printf(“\n\t*5.DISPLAY       *     *”);
       printf(“\n\t*6.COUNT NODES   *******”);
       printf(“\n\t*7.SEARCH ELEMENT*”);
       printf(“\n\t*8.REVERSE LIST  *”);
       printf(“\n\t*9.DELETE INFO   * “);
       printf(“\n\t*10.PUT ITEM POS * “);
       printf(“\n\t*11.DEL POS NODE * “);
       printf(“\n\t*12.EXIT”);
       printf(“\n\t******************”);
       printf(“\nEnter the choice: “);
       scanf(“%d”,&choice);
       switch(choice)
       {
           case 1:
                  printf(“\nEnter the number to be added to the Beginning of the list: “);
                  scanf(“%d”,&item);
                  first=insertfront(first,item);
                  break;
           case 2:
                  first=deletefront(first);
                  break;
           case 3:
                  printf(“\n\tEnter the item to be inserted to the ned of the list: “);
                  scanf(“%d”,&item);
                  first=insertrear(first,item);
                  break;

           case 4:
                  first=deleterear(first);
                  break;
           case 5:
                  display(first);
                  break ;
           case 6:
                  countnode(first);
                  break ;
           case 7:
                   if(emptytest)
                   {
                       field();
                       printf(“\t\tList is empty “);
                       field();
                       break ;
                   }
                   field();
                   printf(“Enter the key to be searched: “);
                   field();
                   scanf(“%d”,&key);

                   searchelement(first,key);
                   break;
           case 8:
                  first=reverse(first);
                  field();

                  break ;
           case 9:printf(“\nEnter the info to be deleted: “);
                  scanf(“%d”,&key);
                  first=deleteinfo(first,key);
                  break ;
           case 10:printf(“\nEnter the position at which you want the node to be inserted: “);
                   scanf(“%d”,&key);
                   printf(“\nEnter the element to be inserted: “);
                   scanf(“%d”,&item);
                   first=insertnodepos(first,key,item);
                  break ;
           case 11:printf(“\nEnter the position where you want the node to be deleted: “);
                   scanf(“%d”,&key);
                   first=delposnode(first,key);
                   break ;
           default:exit(0);
       }
   }

}

illustration of Pure virtual functions

#include<iostream>
using namespace std;
class number
{
    protected:
    int val;
    public:
    void setval(int i)
    {
        val=i;
    }
   virtual void show()=0;

};
class hextype:public number
{
    public:
    void show()
    {
        cout<<“Hextype:”;
        cout<<hex<<val<<“\n”;
    }
};
class dectype:public number
{
    public:
    void show()
    {
        cout<<“Decimal: “;
        cout<<val<<“\n”;

    }
};
class octtype:public number
{
    public:
    void show()
    {
        cout<<“Octal : “;
        cout<<oct<<val<<“\n”;

    }
};
int main()
{
    dectype d;
    hextype h;
    octtype o;
    d.setval(100);
    d.show();
    h.setval(100);
    h.show();
    o.setval(100);
    o.show();
}

Image

Linked List functions

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

bool emptytest;//TO TEST FOR EMPTY LIST
typedef struct node* NODE;
struct node
{
    int info;
    NODE link;
};//dont miss ; at the end of the structure

NODE allocate(NODE temp)//function to allocate memory for created nodes
{
    temp=(NODE)malloc(sizeof(struct node));
    return temp;
    /*no need to return temp as the operation is performed on the pointer (all elements of type NODE
      are pointers ), the change is observed implicitly)*/
}
void field()
{
    int i;
    printf(“\n\t\t”);
    for( i=0;i<10;i++)
     printf(“*”);
    printf(“\n”);
}
NODE insertfront(NODE first,int item)

{
    emptytest=false;
    NODE temp;
    temp=allocate(temp);

    temp->info=item;
    temp->link=first;
    return temp;

}
NODE deletefront(NODE first)
{
    NODE temp;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO Nodes Exist”);
        field();

        return first;
    }

    temp=allocate(temp);
    temp=first;

    first=first->link;
    field();
    printf(“The first node with item %d is deleted”,temp->info);
    field();
    free(temp);
    if(first==NULL)
    {
        emptytest=true;
    }
    return first;

}
NODE insertrear(NODE first,int item)
{
    NODE current,temp;
    current=allocate(current);
    emptytest=false;
    if(first==NULL)
    {

        current->info=item;
        return current;

    }
    temp=allocate(temp);
    current=first;
    while(current->link!=NULL)
    {
        current=current->link;
    }
    current->link=temp;
    temp->info=item;
    temp->link=NULL;
    return first;

}
NODE deleterear(NODE first)
{
    NODE current,prev;
    if(first==NULL)
    {

        field();
        printf(“\n\tNo elements in the List”);
        field();
        return first;
    }
    if(first->link==NULL)
    {
        field();
        printf(“\t\tOnly one node was present and is deleted “);
        printf(“\nThe data in the field was: %d”,first->info);
        field();

        free(first);
        return NULL;
    }
    current=allocate(current);
    prev=allocate(prev);
    current=first;
    prev=NULL;
    while(current->link!=NULL )
    {
        prev=current;
        current=current->link;

    }
    prev->link=NULL;
    field();
    printf(“\tTHE LAST NODE WITH INFORMATION %d is deleted”,current->info);
    free(current);
    if(first==NULL)
    {
        emptytest=true;
    }
    return first;

}
void display(NODE first)
{

    NODE current;
    current=allocate(current);
    current=first;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO NODES EXIST”);
        field();
        return ;
    }
    printf(“\n”);

    while(current->link!=NULL)
    {
        printf(“|%d|->”,current->info);
        current=current->link;
    }
    printf(“|%d|”,current->info);
    printf(“\n\n”);
}
void countnode(NODE first)
{
    NODE current;
    int count=0;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO nodes Exist”);
        field();
        return ;
    }
    current=allocate(current);
    current=first;
    while(current!=NULL)
    {
        current=current->link;
        ++count;
    }
    field();
    printf(“\t\tThe no.of.nodes in the list=%d”,count);
    field();
}
void searchelement(NODE first,int key)
{
    NODE temp;
    int count=0;
    if(first==NULL)
    {
        field();
        printf(“\t\tThe List is Empty “);
        field();
        return ;
    }
    temp=allocate(temp);
    temp=first;
    while(temp)
    {
        count++;
        if(temp->info==key)
        {
            field();
            printf(“\t\tThe key found at location no %d in the list “,count);
            return ;
        }
        temp=temp->link;
    }
    field();
    printf(“Key not found”);
    field();
}

NODE reverse(NODE first)
{
    NODE previous,current;
    char* ch;
    if(first==NULL)
    {
        field();
        printf(“\t\tList is Empty”);
        field();
        return ;
    }
    previous=allocate(previous);
    current=allocate(current);
    previous=NULL;
    while(first!=NULL)
    {
        current=first;
        first=first->link;
        current->link=previous;
        previous=current;
    }
    field();

   return previous;
}
int main()
{

   NODE first;
   int choice,item,key;

   first=allocate(first);
   first=NULL;//VERY IMPORTANT INITIALIZATION
   emptytest=true;//INDICATES THAT THE LIST IS EMPTY
   while(1)
   {
      printf(“\n\t********************”);
       printf(“\n\t*1.INSERT FRONT  *******”);
       printf(“\n\t*2.DELETE FORNT  *     *”);
       printf(“\n\t*3.INSERT REAR   *     *”);
       printf(“\n\t*4.DELETE REAR   *     *”);
       printf(“\n\t*5.DISPLAY       *     *”);
       printf(“\n\t*6.COUNT NODES   *******”);
       printf(“\n\t*7.SEARCH ELEMENT*”);
       printf(“\n\t*8.REVERSE LIST  *”);
       printf(“\n\t******************”);
       printf(“\nEnter the choice: “);
       scanf(“%d”,&choice);
       switch(choice)
       {
           case 1:
                  printf(“\nEnter the number to be added to the Beginning of the list: “);
                  scanf(“%d”,&item);
                  first=insertfront(first,item);
                  break;
           case 2:
                  first=deletefront(first);
                  break;
           case 3:
                  printf(“\n\tEnter the item to be inserted to the ned of the list: “);
                  scanf(“%d”,&item);
                  first=insertrear(first,item);
                  break;

           case 4:
                  first=deleterear(first);
                  break;
           case 5:
                  display(first);
                  break ;
           case 6:
                  countnode(first);
                  break ;
           case 7:
                   if(emptytest)
                   {
                       field();
                       printf(“\t\tList is empty “);
                       field();
                       break ;
                   }
                   field();
                   printf(“Enter the key to be searched: “);
                   field();
                   scanf(“%d”,&key);

                   searchelement(first,key);
                   break;
           case 8:
                  first=reverse(first);
                  field();

                  break ;

           default:exit(0);
       }
   }

}

A simple program illustrating use of virtual functions

#include<iostream>
using namespace std;
class base
{
    public:
    void virtual karthik()//virtual function , which can be re-defined in any of the derived classes
    {
        cout<< “Inside Base , i am in 10th std\n”;
    }
};
class derived1:public base
{
    public:
    void karthik()//redefination of the virtual function
    {
        cout<<“INSIDE DERIVED 1 , I AM IN SECOND PU “;
    }
};
class derived2:public base
{
    public:
    void karthik()//redefination 2
    {
        cout<<“\nINSIDE DERIVED 2 , I AM IN DR.AIT C.S.E”;
    }
};
int main()
{
    base *p,b;
    derived1 d1;
    derived2 d2;
    p=&b;//p is a base class pointer
    p->karthik();//accesing function of base
    p=&d1;
    p->karthik();//accesing function of derived 1
    p=&d2;
    p->karthik();//accesing function of derived 2
    return 0;
}

Program to create a simple file in linux , using linux system call

//here open is not a standard C Call , its a linux system call
//file operations can be performed using standard calls or using linux system calls
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
int main()
{
    int n;
    n=open(“/home/karthic/Desktop/test.txt”,O_CREAT,S_IRUSR);//replace this with a valid path on your computer
    /* 1)Open function call returns a integer called as FileDescriptor , which will be associated with the file whose path is
         is mentioned as the first argument to the open function call
        2)This return value from open  function call , that is the returned file descriptor will be used to perform read ,write operations on the file
        3)The second parameter specifies that we are just creating  a file and the 3rd parameter specifies the permission for the file created
        4)the format of the third parameter is as follows
           *S_IRUSR
           *S_IWGRP
           *S_IXOTH
           As you can see S_I is common amongst all the options , then it must be followed by either ‘R’ or ‘W’ or ‘X’ , which stands for Read , write and
           execute respectively.
           Then it must be followed by either USR,GRP and OTH to apply the permissions either to Users , groups or other users on the system*/
    printf(“\nThe file descriptor associated with the file is :%d “,n);

}

Restoring access of Public and protected members of Base class when derived as Private

Whenever a Base class is derived a private , the public and protected members of the base class becomes private to the derived class and become inaccessible. But its possible to  restore the access mode of such variables by using ::(scope resolution operator ) inside the derived class.
class base {
public:
int j; // public in base
};
// here is access declaration
class derived:private base
{
public:
base::j; // make j public again

.
};
As base is inherited as private by derived, the public member j is made a private
member of derived. However, by including
base::j;
as the access declaration under derived’s public heading, j is restored to  public
Otherwise , its not possible for the derived class to access the variable j.
You can use an access declaration to restore the access mode of public and protected
members. But its not possible to modify the access status a private member function or a variable of the base class.