Posts

Showing posts from September, 2014

C Program to insert/delete/traverse/display XOR Linked List

Here is the C Program to insert/delete/traverse/display XOR Linked List.XOR linked list is said to be memory efficient version of doubly linked list.

_________________________________________________________________________________

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *ptr;
};
struct Node* XOR(struct Node *a,struct Node *b){
return (struct node*) ((unsigned int) (a) ^ (unsigned int) (b));
}
void insertIntoXORLL(struct Node **head,int pos,int data){
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
if(*head==NULL){//Insert first Node
newNode->data=data;
newNode->ptr=NULL;
*head=newNode;
}else{
struct Node *current=*head;
struct Node *previous=NULL,*next=NULL;
struct Node *nextNode=NULL;
if(pos<=1){
newNode->ptr=XOR(previous,*head);
nextNode=XOR((*head)->ptr,previous);
(*head)->ptr=XOR(newNode,nextNode);
*head=newNode;
}else if(pos>1){
int count=1;
while(current!=NULL &&…

C Program to sort an array using insertion sort

I have seen couple of students who use swap in insertion sort to sort an array.Insertion sort is an example of exchange sort ,we don't swap two elements to sort an array ,instead of this we shift elements if ith element is greater than i+1th element to sort an array.It works on simple concept:-
Store the ith element in temporary variable and shift element until you find the element smaller than this element towards 0 index position.Here is the C Program to find insertion sort:-

_________________________________________________________________________________
#include <stdio.h>
#include <string.h>

main()
{
    int arrSize,i;
   printf("Enter Size of array:");
   scanf("%d",&arrSize);
   int array[arrSize];
   for(i=0;i<arrSize;i++){
       scanf("%d",&array[i]);
   }
   int temp,j;
   for(i=1;i<arrSize;i++){
       temp=array[i];
       for(j=i;j>0;j--){
           if(temp<array[j-1]){
               array[j]=array[j-1]…