Friday, December 14, 2018

Program to reverse a single linked list



//Program to reverse a single linked list
//Assumption: Each item in the list, is added as head in the list
// Improv:- modify such that the element is pushed into the last of list
#include <stdio.h>
#include <stdlib.h>

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

void reverse(struct node **head)
{
  struct node *travnode = NULL;
  struct node *prevnode = NULL;
  struct node *nextnode = NULL;
  travnode = *head;
  while(travnode !=NULL)
   
  {
    //Storing the next node is important
    nextnode = travnode->next;
    travnode->next = prevnode;
    prevnode = travnode;
    travnode = nextnode;
  }
 
  *head = prevnode; 
 
   printf("%s\n", "Exiting reverse");
}

static void pushlist(struct node **head, int data)
{
    struct node *currentnode = NULL;
    currentnode = malloc(sizeof(struct node *));
    currentnode->data = data;
    currentnode->next = *head;
    *head = currentnode;
}

void print(struct node *headnode)
{
   
  struct node* travnode = headnode;
  while(travnode!=NULL) {
    printf("%d\t", travnode->data);
    travnode = travnode->next;
   
  }
 
}

int main()
{
 
  int a= 0, b=0, c=0, d=0, e=0;
  struct node *head = NULL;
  printf(" Enter 5 nos :\n");
  scanf( "%d %d %d %d %d", &a, &b, &c, &d, &e);
  pushlist(&head, a);
  pushlist(&head, b);
  pushlist(&head,c);
  pushlist(&head,d);
  pushlist(&head,e);
  print(head);
  reverse (&head);
  print(head);
       
  return 0;
 
}

Sample Output:
Enter 5 nos :
99 88 77 66 55
55  66  77  88  99  Exiting reverse
99  88  77  66  55

Thursday, September 20, 2018

Method to find XOR of two numbers without using XOR operator

I need to find an optimized solution for this interview question. When I mention optimized, it should be truly optimized without over complicating the code.
For the time being, I have written down a not-so optimized code. This will require further testing.


#include <stdio.h>
unsigned int find_xor(unsigned int a,unsigned int b);
int main()
{
    int a, b,c;
    scanf("%d %d", &a,&b);
    c = find_xor((unsigned int)a,(unsigned int)b);
    printf("The xor is %x",c);
    //get(c);
    return 0;
}

unsigned int find_xor(unsigned int a,unsigned int b)
{
 unsigned int c = 0, i=0,d = 0;
 unsigned int a1,b1;
  printf("The a %x b %x \n",a,b);
 while ( (a || b) && (i<32))
 {
     a1 = a &1;
     b1 = b &1;
     printf("The a1 %x b1 %x \n",a1,b1);
     if((a1 != b1)) {
      c = (c<<1)|(1);
      printf("The c %x  \n",c);
     }

     i++;
     a = a>>1;
     b= b>>1;
  }
  //the bits are reverse
   printf("The xor is %x",c);
     while (c) {
         d = (d<<1) |(c & 0x1);
         c = c>>1;
        
     }
 c = d;
 return c;
   
}

Tuesday, June 27, 2017

Learning Python - Online Resources

I have been planning to learn Python for some time and needs some motivation to do so. So here are few links which I intend to use in future.

1. https://www.codecademy.com/learn/python
2.  http://www.python-course.eu/

I am planning to expand this post as I go along. But here are starting point for me currently. 

Saturday, July 2, 2016

Finding NVMe Passthru handle - UEFI Edk2.5

One of the challenge when I started working on NVMe devices in UEFI driver was the lack of sample code which can leverage the NvmExpressDxe code in the Edk2.5 code.

At this point, let me add that there is a quite a bit of different in the NvmExpressDxe code in Edk2 and Edk2.5.

1. The major difference is the GUID itself. Then all the data structures for sending command, completion etc are different. This is an important point - If you are using the wrong GUID, you will not be able to communicate with your NVMe driver using pass thru protocol.

2. The location of the NvmExpressPassthru.h is different in Edk2.5. You can find it in Edk_2.5\MdePkg\Include\Protocol. This file was previously included in the NvmExpressDxe folder in MdeModulePkg.

So here is a bit of reference code that can be used. Refer Edk2.5 code for better clarity.

#define EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL_GUID \
  { \
    0x52c78312, 0x8edc, 0x4233, { 0x98, 0xf2, 0x1a, 0x1a, 0xa5, 0xe3, 0x88, 0xa5 } \
  }

 EFI_GUID gNvmExpressPassThruProtocolGuid = EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL_GUID;
EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *ptrNvmePassThru = NULL;
We can use the LocateHandleBuffer function to find the handles which has exposed the Nvm Pass thru protocol.

UINTN       NHandles = 0;
    EFI_HANDLE      *Handles = NULL;
        Status = gBS->LocateHandleBuffer (
                ByProtocol,
                &gNvmExpressPassThruProtocolGuid,
                NULL,
                &NHandles,
                &Handles
                );

    if (Status == EFI_NOT_FOUND || NHandles == 0) {
    //
    // If there are no Protocols handles, then return EFI_NOT_FOUND
    //
                return EFI_NOT_FOUND;
    }

    for(Index = 0; Index < NHandles; Index++) {

        Status = gBS->HandleProtocol(
                        Handles[Index],
                        &gNvmExpressPassThruProtocolGuid,
                        (VOID **) &ptrNvmePassThru                      
                        );
        if (EFI_ERROR (Status)) {              continue;
        }
//Use  ptrNvmePassThru for sending identify nvme controller to get the details of the nvme controller
}

This ptrNvmePassThru is the passthru handle required for sending any command to the NVMe device using NVMe passthru protocol.

Note: The above code is very much similar for getting handle who has exposed SATA Pass through protocol or SCSI pass through protocol.

Saturday, February 21, 2015

Custom Blogger to WordPress - Change DNS Records on eNom

update DNS Records. Moving from Custom Blogger blog to self hosted WordPress site

Update DNS Records Self Hosting on Bluehost


When you move you Custom Blogger blog to self hosted WordPress, you need to update your DNS Records with Enom so that the url points to the brand new self-hosted site. If you are moving your Custom Blogger blog to self hosted WordPress, then your DNS Records will need to be updated as shown above.

When you are using the Custom Blogger blog, the DNS Settings would be Default and there will be several details related to Google. You have to click on Edit and then Custom and enter the server names of Bluehost.

Logging into Enom Account


To update DNS Records, you need to login into your Domain Management console of eNom.

https://access.enom.com

Finding the eNom Login Information


To get the login details, you will have to get the eNom login details, login into Google apps

http://www.google.com/a/yourdomain.com

If you have forgotten the login user name, try bloggeradmin@yourdomain.com. This will

allow you to reset your password using your email id and give you access.

Once you login, click on Domains in the Admin Console. This will take you to the Domains Page.Click on Advanced DNS settings to get the eNom Login details to update DNS records.

You can click on Sign in to DNS console to go straight to the eNom Domain Management Page

Update DNS Records for CloudFlare


CloudFlare is a security and anti-spam tool used to protect my WordPress site. It came

with high recommendation and I hope CloudFlare will take care of my security as well as spam concerns about my self hosted WordPress site.

During the setup of CloudFlare on your website, you are asked to update your DNS Records

with CloudFlare details.

Login into eNom account with above login details. The url for the required login is

https://access.enom.com

Once you have logged in, click on Edit for DNS Information and add the CloudFlare DNS Server details. Go back to CloudFlare website and let them know that you have updated the DNS records.

Wait till CloudFlare takes over your website.

Friday, November 21, 2014

Storage Networks Explained - Book Recommendation


I think a good technical book about Storage Networks and other Storage Domain related information is hard to come by especially cheap ones. So here is a recommendation for a great book.

I liked this book very much and felt very bad that I could not get hold of this book when I started my career. I would have learned and understood much more about technical stuffs related to storage and network domain.

This book is priced at Rs 379 INR and will come cheaper if you are ordering it online. I believe I got this from flipkart.com.

If you are into Storage or related Domain, please take care to read this book. It has lots and lots of great information which is difficult to find anywhere else. eg - RAID, SAN, iSCSI etc.

Friday, November 7, 2014

C Interview Questions - Tackling the C Written Exam


Everyone who has done a course in computers be it a diploma or an Engineering course dreams of landing a dream job in a company of their job with a dream pay. To be frank, this dream remains to be a dream for many even after slogging for hours in front of computer for years. But there are a few who seems to get it all together.

Landing your dream job as a software developer requires systematic preparation especially if you are aiming for a job in a tier-1 or tier-2 company who gives emphasis on your coding skills or atleast knowledge of your basics. This is where most people fail. I know a lot of people who studies Yashavant Kanetkar books and expects to be placed well. As such there is nothing wrong in reading these books, but if your exposure to the subject is limited to these books, you are going to be in the group of mediocre programmers; forever.

In my few years of experience as a software developer, I have given a few interviews over the years.At this point, I am holding my third job after joining the company which placed me through the campus. Believe me, there are few basic things that you need to know how much ever experienced you are.

So I am giving you the shortest though not the easiest method to tackle the C interview round or the C written exam.

Read The C Programming Language book, preferably from the first page to the last page including the appendix.

Ok. Now for some confessions. I have not read the book fully, but has gone through most of the chapters during the first few years of my career. I did certainly read it during my college days and this book is still the best book about programming.

In the age of C#, .NET etc this book may look outdated, but this book is not going to be outdated for a long time. Because all other programming languages have evolved from C.

There is an answer book corresponding to the programming exercises in the end of the different chapters. It will be great if you can do those exercises too.