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
}
}

Sunday, December 22, 2013

Find list is circular list or not

class single_link_list
{
struct node
{
int element;
node* link;
}*first_node;
public:
single_link_list()
{
first_node = NULL;
}
void change_list_to_circular();
bool is_list_circular();
};

bool Linked_list::is_list_circular()
{
    bool status = false;
    node* slower_iteration = first_node;
    node* faster_iteration = first_node;
    while(faster_iteration  && slower_iteration)
    {
        if(faster_iteration->next_node == slower_iteration )
            {
                   status = true;
                  return status;
            }
       
        else if(faster_iteration == NULL || faster_iteration->next_node == NULL)
            {
              return status;
             }
        else
        {
         faster_iteration= faster_iteration->next_node->next_node;
         slower_iteration = slower_iteration->next_node;
        }
    }
    return status;
}

void single_link_list::change_list_to_circular()
{
//for changing list to circular last node shall point to first node
node* temp_node = first_node;
while(temp_node->link != NULL)
{
temp_node = temp_node->link;
}
temp_node->link = first_node;
}


int main()
{
single_link_list *ptr =  new single_link_list();
int element = 10;
ptr->insert_in_list(element); // for insert refer old post in same blog
ptr->insert_in_list(element + 5 ); // for insert refer old post in same blog
ptr->insert_in_list(element + 10); // for insert refer old post in same blog
ptr->insert_in_list(element + 15); // for insert refer old post in same blog
        bool is_circular = ptr->is_list_circular();
ptr->change_list_to_circular();
printf(" \n After calling  change_list_to_circular(): \n");
is_circular = ptr->is_list_circular();

return 0;
}

Output would be:




Thursday, December 19, 2013

Dynamic Stack implementation

#pragma once

#include "stdafx.h"
#include <iostream>

template<class Stack_type>
class Dynamic_stack
{
    struct node
    {
     Stack_type stack_element;
     node* next;
    }*top;
public:
    Dynamic_stack(void);
    bool push_element(Stack_type);
    bool pop_element();
    bool is_empty();
    int no_of_element();
    void print_element();
   
public:
    ~Dynamic_stack(void);
};

//constructor
template<class Stack_type>
Dynamic_stack<Stack_type>::Dynamic_stack(void)
{
    top = 0;
}

//destructor
template<class Stack_type>
Dynamic_stack<Stack_type>::~Dynamic_stack(void)
{

}
template<class Stack_type>
bool Dynamic_stack<Stack_type>::is_empty()
{
    return top == NULL ? 1:0;
}

template <class Stack_type>
bool Dynamic_stack<Stack_type>::push_element(Stack_type element)
{
    node* new_node = new node();
    new_node->stack_element = element;

    if(is_empty())
    {
        top = new_node;
        new_node->next = NULL;
        return true;
   
    }
    else
    {
        new_node->next = top;
        top = new_node;
        return true;
    }
}
template<class Stack_type>
int Dynamic_stack<Stack_type>::no_of_element()
{
    int count = 0;
    node* temp_node = top;
    while(temp_node)
    {
        count++;
        temp_node = temp_node->next;
    }
    return count;

}
template <class Stack_type>
void Dynamic_stack<Stack_type>:: print_element()
{
    node* temp_node = top;
    if(temp_node == NULL)
    {
        printf("Sorry there is no element to display in the stack");
    }
    else
    {
        while(temp_node)
        {
            printf("%d \n", temp_node->stack_element);
            temp_node = temp_node->next;

        }
    }
   
}

template<class Stack_type>
bool Dynamic_stack<Stack_type>::pop_element()
{
    //Stack is following LIFO(Last in first out) structure, so top element has to popped up
    //top has to updated with next of top.

    if(is_empty())
    {
        printf("Stack is empty\n");
        return false;
    }
    else
    {
        node* temp_node = top;
        printf("Popped Elemenet is: %d \n",temp_node->stack_element);
        temp_node  = temp_node->next;
        top = temp_node;
        return true;
    }

}

int main()
{

Dynamic_stack<int> int_dynamic_stack;
Dynamic_stack<char> char_dynamic_stack;
int element = 10;
char c_element = 'A';
int_dynamic_stack.push_element(element);
int_dynamic_stack.push_element(element+5);
int_dynamic_stack.push_element(element+6);

printf("\nTotal number of element: %d \n",int_dynamic_stack.no_of_element());

printf("After adding element in the stack:\n");
int_dynamic_stack.print_element();

int_dynamic_stack.pop_element();
printf("After Pop element in the stack:\n");
int_dynamic_stack.print_element();

int_dynamic_stack.pop_element();
printf("After 2Pop element in the stack:\n");
int_dynamic_stack.print_element();

int_dynamic_stack.pop_element();
printf("After 3 Pop element in the stack:\n");
int_dynamic_stack.print_element();

}
output:

Friday, December 13, 2013

FAQs

Ques:     Types of storage class in C?
Ans:       In C there are 4 types of storage classes present
              1) Auto
              2) Extern
              3) Static
              4) Register
Auto: Auto variable are local variables define inside and function.
          Any variable can be explain in this way; Scope, Lifetime, Visibility
         ex:  int get ()
                 {
                     int age; // automatic variable
                  }
            Scope: within get method
            Lifetime: Till get method is getting executed
            Visibility : Only to get method

Ques: What is difference between static variable and volatile variable?
Ans:  static Variable:
                                 Static variable is available to the module it is defined and till compilation Unit(                                   Same source file or module).
                                or
                               Static variable is the one that exists for the life time of compilation unit (Source                                   file of module)
         Volatile Variable:
                                Volatile keyword is the hint to compiler that the variable's value might change                                     without the compiler's knowledge, therefore the compiler code optimizer can not                                   make assumptions about the variable's current value and must always re-read                                      the variable content. 
                              volatile is generally used when dealing with external events, like interrupts of                                           hardware related pins
                             Note: Volatile is not a type of storage, it can be stored anywhere in the memory,                                                volatile is just a type, So, by declaring variable as volatile.. it gives guarantee                                            that you will get the latest value, which may be altered by an external event.
                               
      Under construction..............
            

Thursday, December 12, 2013

What is MC/DC(Modified Condition/ Decision Coverage) ?

MC/DC Coverage = Branch Coverage + Condition taken for all possible value + Every condition independently affects the outcome of a decision

Ex:
A or (B & C)

     A                   B                 C                   Test case/Output

1) True           False             True                 True
2) False          False             True                 False
3) False          True              True                 True
4) False          True              False                 false


   
Explanation:

1) test case 1 and 2 are for A where values of variable 1 changed and out come as well changed.
2) test case 2 and 3 are for B where values of variable B changed and out come as well changed.
3) test case 3 and 4 are for C where values of variable C changed and outcome as well changed.


Monday, December 9, 2013

Print the Elements of Singly Linked list

//print the element in the list
void single_linked_list::print_list()
{
    node* temp_node = first_node;
    while(temp_node)
    {
        printf("%d \n" , temp_node->data);
        temp_node =  temp_node->link;
    }
}




Count the Number of Nodes in Singly Linked List

//count the no of elements in the list
int single_linked_list::no_of_elements()
{
    node* temp_node = first_node;
    int no_of_element = 0;
    while(temp_node!= NULL)
    {
      no_of_element++;
      temp_node = temp_node->link;
    }
    return no_of_element;
}

Sorting of Singly Linked List

void single_linked_list::sort()
{
node *temp,*current;
temp = first_node;
current = temp->link;

for(int i = 1;i < no_of_elements();i++)
{
   while(current!=NULL)
   {
    if(temp->data>=current->data)
    {
       current->data=(temp->data)+(current->data);
       temp->data=(current->data)-(temp->data);
       current->data=(current->data)-(temp->data);
       current=current->link;
    }
    else
    {
      current=current->link;
     }
   }
   temp=temp->link;
   current=temp;
   current=current->link;
}
}

Delete Node from Singly Linked List

bool single_linked_list::delete_element(int del_element)
{
   bool status = false;
   node* temp_node = first_node;
   node* deleted_node;
   node* prev_node;
   node* next_node;
   while(temp_node)
   {
       if(temp_node->data == del_element)
       {
           next_node = temp_node->link;
           prev_node->link = next_node;
           deleted_node = temp_node;
           status = true;
       }
    prev_node = temp_node;
    temp_node = temp_node->link;
   }
   if(status)
   {
       printf("Element found and deleted element is : %d", deleted_node->data);
   }
   else
   {
       printf("Element not found !!");
   }
   return status;
}

Insert Node at given position in Singly Linked List

//insert at eneterd position
bool single_linked_list::insert_at_pos(int input_pos , int element)
{
    node* temp_node = first_node;
    node* prev_node;
    bool status = true;
    if(input_pos == 1)
    {
        insert_at_first(element);
    }
    else if(input_pos == no_of_elements() +1 )
    {
        insert_at_end(element);
    }
    else if( input_pos > 1 && input_pos <= no_of_elements())
    {
      node* new_node = new node();
      new_node->data = element;
      for(int i = 0 ; i< input_pos-1 ; i++)
      {
        prev_node = temp_node;
        temp_node = temp_node->link; 
      }
      new_node->link = temp_node;
      prev_node->link = new_node;
    }
    else
    {
        printf(" Invalid position \n");
        status = false;
    }
    return status;
}



Insert Node at the End in Singly Linked List

//insert at end
void single_linked_list::insert_at_end(int element)
{
    if(first_node == NULL)
    {
        //printf("List is empty, end and beginng is same \n");
        node* new_node = new node();
        new_node->data = element;
        new_node->link = NULL;
        first_node =  new_node;
    }
    else
    {
        node* temp_node = first_node;
        node* new_node = new node();
        new_node->data = element;
        new_node->link = NULL;
        while(temp_node->link)
        {
            temp_node = temp_node->link;
        }
        temp_node->link  = new_node;
    }
}