Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

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

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.