Posts

Hottest Programming Languages Today

Assembly C C# C++ D Processing Erlang Haskell Java Javascript PHP Python R Ruby Rust Swift

Good Tools for Research : Helpful Links

ShareLaTeX, and WriteLaTeX:  To draft a research paper in any format you wish with ease and share among collaborators while working. Link for ShareLaTeX Link for WriteLaTeX DropBox:  Keeping documents/papers etc at one place and sharing among the team members Link PDF Annotator: Easy to edit pdf at anytime Link Google Scholar:  Easy to find research papers relevant to my required keywords Link Plotly:  For creating / drawing graphs Link Mendeley: Reference Manager Link Animoto:  Video Maker Link EndNote: Make your library of whatever you search Link SmartDraw: Create diagrams and charts Link

Implementation of Stack

Implementatio in C: Using Array #include<stdio.h> #include<stdlib.h> struct stack {     int top;     unsigned capacity;     int* array; }; struct stack* create(unsigned capacity) {     struct stack* stack = (struct stack*)malloc(sizeof(struct stack));     stack->top = -1;     stack->capacity = capacity;     stack->array = (int*)malloc(capacity * sizeof(int));     return(stack); } int isFull(struct stack* stack) {     return(stack->top == stack->capacity - 1); } int isEmpty(struct stack* stack) {     return(stack->top == -1); } void push(struct stack* stack, int data) {     if(isFull(stack))         printf("Stack overflow");     stack->array[++stack->top] = data;     printf("\nPushed data on stack : %d\n",data); } int pop(struct stack* stack) {     if(isEmpty(...

Function to find Identical LinkedList

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } int identical(struct node* a,struct node* b) {     while(1)     {     if(a == NULL && b == NULL)         return 1;     if(a == NULL && b != NULL)         return 0;     if(a != NULL && b == NULL)         return 0;     if(a->data != b->data)         return 0;     a = a->next;     b = b->next;     } } int main() {     struct node* a = NULL;     struct node* b = NULL;     p...

Delete Alternate Nodes in LL

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void print(struct node* n) {     struct node* temp = n;     if(temp == NULL)     {     printf("Empty LinkedList");     }     while(temp!=NULL)     {     printf(" %d",temp->data);     temp=temp->next;     } } void alternateDelete(struct node* head) {     struct node* prev= head;     struct node* node=head->next ;     if(head == NULL)         return;     while(prev != NULL && node != NULL)     {     prev->...

Move Last Element to first of LL

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void print(struct node* n) {     struct node* temp = n;     if(temp == NULL)     {     printf("Empty LinkedList");     }     while(temp!=NULL)     {     printf(" %d",temp->data);     temp=temp->next;     } } void relocate(struct node** head) {     struct node* current= *head;     struct node* temp= NULL;     if(*head == NULL || (*head)->next == NULL)         return;     while(current->next != NULL)     {     tem...

Pairwise Swap elements of LL

Implementation in C: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void print(struct node* n) {     struct node* temp = n;     if(temp == NULL)     {     printf("Empty LinkedList");     }     while(temp!=NULL)     {     printf(" %d",temp->data);     temp=temp->next;     } } void pairswap(struct node* head) {     struct node* current = head;     while(current!=NULL && current->next != NULL)     {     swap(&current->data,&current->next->data);     current = current->next->next; ...

Remove duplicates from sorted LL

void removeDuplicates(struct node* head) {     struct node* current = head;     struct node* next;     if(current == NULL)         return;     while(current->next!= NULL)     {         if(current->data == current->next->data)         {         next=current->next->next;         free(current->next);         current->next=next;         }         else         {         current = current->next;         }     } }   Time Complexity:  O(n)

Print reverse of LL by recursive function

void printReverse(struct node* head) { if(head == NULL) return; else printReverse(head->list); printf("%d",head->data); } Time Complexity:  O(n)

Find intersection point of two LL

Logic: 1) Find the total number of elements in both the LL. 2) Find the difference between number of elements of the two linked list. 3) Move the pointer of that LL which contains more number of elements by the difference. 4) Then move the pointers of both the list and check if nodes present in first LL is same as the node present in second LL. Time Complexity:  O(m+n) if first LL contains m elements and second LL contains n elements. Space Complexity:  O(1)

Insert element in LL in sorted order

Logic: 1) If LL is empty, then insert the node as head and return it. 2) If value of node to be inserted in LL is less than the value of the head node, then insert the node at start and make it head. 3) Otherwise, traverse through the list and find the appropriate place for the node and check if the value of node to be inserted is less than the value of next node, then insert the node before the next node. Implementation in C: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; struct node* newNode(int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data=newData;     newNode->next=NULL;     return newNode; } void print(struct node* n) {     while(n!=NULL)     {         printf("%d    ",n->data);         n=n->next;   ...

Print Middle element of LL

Method 1: Traverse linked list using two pointers. Move first pointer by one and second pointer by two, when second pointer reaches to the end of the list, then first pointer will reach to the middle of the list. Implementation in C: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; int main() {     struct node* head = NULL;     push(&head,1);     push(&head,2);     push(&head,3);     push(&head,4);     push(&head,5);     push(&head,6);     push(&head,7);     printMiddle(head);     return 0; } void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void printMiddle(struct node...

Function to get Nth Node in LL

From beginning of the list: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; int main() {     struct node* head = NULL;     push(&head,8);     push(&head,7);     push(&head,3);     push(&head,4);     push(&head,1);     printf("LinkedList:\n");     print(head);     int value = GetNth(head,0);     printf("\nValue of Nth node is: %d",value);     return 0; } void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } int GetNth(struct node* head, int index) {     struct node* current = head;     int count =0;     while(current != NULL)    ...

Deletion in LL

#include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; }; int main() {     struct node *head = NULL;     push(&head,7);     push(&head,8);     push(&head,4);     push(&head,3);     printf("\n Linked List is:\n");     print(head);     delete(&head,8);     printf("\n LinkedList after Deletion of 8: \n");     print(head);     return 0; } void push(struct node** ref, int newData) {     struct node *newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void delete(struct node** headref,int key) {     struct node* temp = *headref;struct node* prev;     if(temp != NULL && temp->data == key)     {   ...

Insertion in LL

#include<stdio.h> #include<stdlib.h> struct node { int data ; struct node * next ; }; int main () { struct node * head = NULL ; append ( & head , 8 ); append ( & head , 7 ); push ( & head , 3 ); push ( & head , 4 ); append ( & head , 5 ); insertAfter ( head -> next -> next , 1 ); printf ( "Linked List is: \n " ); print ( head ); return 0 ; } void push ( struct node ** ref , int newData ) { struct node * newNode = ( struct node * ) malloc ( sizeof ( struct node )); newNode -> data = newData ; newNode -> next = ( * ref ); ( * ref ) = newNode ; } void insertAfter ( struct node * prev , int newData ) { if ( prev == NULL ) { printf ( "the given previous node can't be null" ); return 0 ; } struct node * newNode = ( struct node * ) malloc ( sizeof ( struct node )); ...

LinkedList in C

#include<stdio.h> #include<stdlib.h> struct node {  int data;  struct node *next; }; int main() {  struct node *head = NULL;  struct node *second = NULL;  struct node *third = NULL;  head = (struct node*)malloc(sizeof(struct node));  second = (struct node*)malloc(sizeof(struct node));  third = (struct node*)malloc(sizeof(struct node));  head->data = 1;  head->next = second;  second -> data = 2;  second -> next = third;  third -> data =3;  third -> next = NULL;  print(head);  return 0; } void print(struct node *n) {  while(n!= NULL)  {  printf("%d",n->data);  n = n ->next;  } }

gets() vs fgets()

We can take string as input with the help of gets() function but this function suffers with the problem of buffer overflow as it does not care about the maximum limit of array of characters (do not perform aray bound testing) and it takes input as long as it sees a newline character. Apart from it, fgets reads string input till the maximum limit of array is reached.

Structure

It is not possible to directly check equality of two structures but can compare two structures by element by element basis. Reason:   There is not a good way for a compiler to implement structure comparison (i.e. to support the  ==  operator for structures) which is consistent with C's low-level flavor. A simple byte-by-byte comparison could founder on random bits present in unused ``holes'' in the structure (such padding is used to keep the alignment of later fields correct . A field-by-field comparison might require unacceptable amounts of repetitive code for large structures. Any compiler-generated comparison could not be expected to compare pointer fields appropriately in all cases: for example, it's often appropriate to compare  char *  fields with  strcmp  rather than  ==.   Struct Hack:  This technique is mainly used to create variable length array as a member in structure. We generally keep that element as last element of...

Program to check if strings are rotation of each other or not

Implementation in C: #include<stdio.h> #include<string.h> #include<stdlib.h> int isRotation(char *s1, char *s2) { int len = strlen(s1); if(len == strlen(s2) && len >0) { char *temp; void *ptr; temp = (char*)malloc(sizeof(char)*(len*2 +1)); temp[0] = '\0'; strcat(temp,s1); strcat(temp,s1); ptr = strstr(temp, s2); free(temp); if(ptr!= NULL) return 1; else return 0; } return 0; } void main() { char *s1 = "waterbottle"; char *s2 = "erbottlewat"; if( isRotation(s1, s2)) { printf("Yes, s2 is the rotation of s1."); } else { printf("No, s2 is not the rotation of s1."); } } Time Complexity:  Complexity of this function depends on the implementation of strstr function.

Virtual Function

In Object Oriented Programming, a virtual function or method is a function whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is important part of polymorphism. Virtual function allows a program to call methods that don't necessarily even exist at the moment code is compiled. In C++, virtual methods are declared by prepending the function with the virtual keyword in the base class. This modifier is inherited by all implementations of that method in derived classes, meaning that they can continue to over-ride each other and be late-bound. For example, a base class  Animal  could have a virtual function  eat . Subclass  Fish  would implement  eat() differently than subclass  Wolf , but one can invoke  eat()  on any class instance referred to as Animal, and get the  eat()  behavior of the specific subclass. A  pure virtual function  or  pure virtual method ...