Saturday, March 12, 2011

Linked list using recursion

//Untested
Q: How to print the linked list in reverse order

Data * TempData ;
TempData = HeadData;

Data * ReverseOrder(Data * TempData)
{

if(TempData)
{
ReverseOrder(TempData->next);
printf("%d",TempData->data);
}
else
{
printf("NULL \t");

}


}

Thursday, March 10, 2011

Pointer = Call By Value

# include <stdio.h>
int fnCall(char *);
int main()
{
char *PTR="HELLO";
fnCall(PTR);
printf("%s",PTR);
return 0;
}

int fnCall(char *PTR)
{
PTR +=3;
return 0;
}



OUTPUT:

HELLO
Press any key to continue . . .

Wednesday, March 9, 2011

Fibonacci series in Visual C++

# include <stdio.h>
int fnFibonacci(int);
int main()
{
int nNumber;
printf("Enter the number of items limit\n");
fflush(stdin);
scanf("%d",&nNumber);

fnFibonacci(nNumber); // call function

return 0;// for successful execution
}

int fnFibonacci(int nNo) // called function
{
int a=0;
int b=1;
int nLoop,c; // Third variable
if (nNo <= 2)
{
printf("%d\t%d",a,b);
}
else
{
printf("%d\t%d",a,b);
for(nLoop = 3; nLoop<= nNo;nLoop++)
{
c = a + b;
a = b;
b = c;
printf("\t%d",c);
}
}
return 0;// for successful execution
}

OUTPUT

Enter the number of items limit
10
0 1 1 2 3 5 8 13 21 34Press

Tuesday, March 8, 2011

Multiple a number by 7 without using * and + operator.

NewNum = Num << 3; // mulitplied by 2 ^ 3 = 8

NewNum = NewNum - Num; // 8 – 1 = 7

Covert a string to upper case

void ToUpper(char * S)
{
while (*S!=0)
{
*S=(*S >= 'a' && *S <= 'z')?(*S-'a'+'A'):*S;
S++;
}
}

Insert a node a sorted linked list

void sortedInsert(Node * head, Node* newNode)
{
Node *current = head;

// traverse the list until you find item bigger the // new node value
//
while (current!= NULL && current->data < newNode->data)
{
current = current->next);
}
//
// insert the new node before the big item
//
newNode->next = current->next;
current = newNode;
}

Reverse a string

void ReverseString (char *String)
{
char *Begin = String;
char *End = String + strlen(String) - 1;
char TempChar = '\0';

while (Begin < End)
{
TempChar = *Begin;
*Begin = *End;
*End = TempChar;
Begin++;
End--;
}
}

Sort a linked list

//sorting in descending order
struct node
{
int value;
node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)
{
node* first,second,temp;
first= Head;
while(first!=null)
{
second=first->NEXT;
while(second!=null)
{
if(first->value < second->value)
{
temp = new node();
temp->value=first->value;
first->value=second->value;
second->value=temp->value;
delete temp;
}
second=second->NEXT;
}

first=first->NEXT;
}
}

Delete a node in double linked list

void deleteNode(node *n)
{
node *npre = n->prev;
node *nnext = n->next;
npre->next = n->next;
nnext->prev = n->prev;
delete n;
}

Reverse a singly linked list

1. Reverse a singly linked list


//
// iterative version
//
Node* ReverseList( Node ** List )
{

Node *temp1 = *List;
Node * temp2 = NULL;
Node * temp3 = NULL;

while ( temp1 )
{
*List = temp1; //set the head to last node
temp2= temp1->pNext; // save the next ptr in temp2
temp1->pNext = temp3; // change next to privous
temp3 = temp1;
temp1 = temp2;
}

return *List;
}