Friday, January 31, 2014

String operations


Two string comparison:

Header file:

class String_operations
{
 public:
     String_operations()
     {}
      bool string_compare(char* p_str1, char* p_str2 );
};
Cpp File:

bool String_operations::string_compare(char *p_str1, char *p_str2)
{
    bool status = true;
    while(*p_str1 != '\0' || *p_str2 != '\0' )
    {
        if(*p_str1 == *p_str2)
        {
         p_str1++;
         p_str2++;
         status = true;
        }
        else
        {
            status = false;
            break;
        }
    }
    return status;
}

Main method:

int main()
{
    String_operations* p_str_oper = new String_operations();
    bool status = p_str_oper->string_compare("He", "Hello");
    if(status)
    {
        printf("Both strings are equal");
    }
    else
        printf("Both strings are not equal");
    return 0;
}









int main()
{
    String_operations* p_str_oper = new String_operations();
    bool status = p_str_oper->string_compare("Hello", "Hello");
    if(status)
    {
        printf("Both strings are equal");
    }
    else
        printf("Both strings are not equal");
    return 0;
}





























Monday, December 30, 2013

Reverse Singly Linked list

//Description:
//out put will be first element if we want to track execution after first execution of while loop
// out put will be reverse of first two element if we are doing it for 2nd time
//ex: Original list: 10, 15, 20,5
// after first while execution : list will give out put as 10
//second exeecutin 15 , 10 , same execution will follow for rest
// third execution: 20 , 15, 10 etc



void single_link_list::reverse_linked_list()
{
node* current_node = first_node;
node* next_node;
node* prev_node = NULL ;
int count = 0;
if(first_node->link == NULL)
{
printf("there is only one element in the list Hence can not reverse");
}
else
{
      while(current_node != NULL)
              {
next_node = current_node->link; // this is to store the address of broken node link
current_node->link = prev_node; // this is to revese the link
prev_node = current_node; // prev node to current to make sure link is established after reversing
current_node = next_node; // iterating to all nodes
}
         first_node = prev_node; // just changing the head of the list
}
}