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.

Overloaded Constructor

#include<iostream>
#include<cstdio>
using namespace std;
class date
{
int day,month,year;
public:
date(char *iamastar);
date(int d,int m,int y);
void display();
};
date::date(char *iamastar)
{
sscanf(iamastar,”%d%*c%*c%*c%*c%d%*c%*c%*c%*c%d”,&day,&month,&year);

}
date::date(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
void date::display()
{
cout<<day<<“/”<<month<<“/”<<year<<“\n”;
if(day==15&&month==8&&year==1947)
cout<<“*********”<<“HAPPY INDEPENDENCE DAY <<<<<<<<<<<<<<“<<“\n”<<“JAI HIND”;
}
int main()
{
date rao1(20,9,1991),rao2(“15hack8hack1947”);

rao1.display();
rao2.display();
return 0;
}