Wednesday, 22 February 2012

search an element through for binary search

/*P5.15 Program to search an element through for binary search*/
#include <stdio.h>
#define SIZE 100
int binary_search(int arr[],int item, int low, int high);
main()
{
    int arr[SIZE],i, item, n;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    printf("Enter elements of the array(in sorted order) : \n");
    for(i=0; i<n; i++)
        scanf("%d",&arr[i]);
    printf("Enter the item to be searched : ");
    scanf("%d", &item);
   
    i = binary_search(arr,item,0,n-1);
    if(i == -1)
        printf("Not Present\n");
    else
        printf("Present at index %d\n", i);
}/*End of main()*/

int binary_search(int arr[],int item, int low, int up)
{
    int mid;
    if(up < low)
        return -1;    /*not found*/
    mid = (low+up)/2;
    if(item > arr[mid])
        return binary_search(arr,item,mid+1,up);    /*Search in right portion, tail recursive call */
    else if(item  < arr[mid])
        return binary_search(arr,item,low,mid-1);    /*Search in left portion, tail recursive call */
    else
        return mid;    /*found*/
}/*End of binary_search()*/





find the factorial of a number by tail recursive method

/*P5.14 Program to find the factorial of a number by tail recursive method*/

#include<stdio.h>
long TailRecursiveFact(int n);
long TRfact(int n, int result);
main( )
{
    int num;
    printf("Enter a number : ");
    scanf("%d", &num);
    if(num<0)
        printf("No factorial for negative number\n");
    printf("Factorial of %d is %ld\n", num, TailRecursiveFact(num) );
}

/*Tail recursive*/
long TRfact(int n, int result)
{
    if( n==0)
        return result;
    return TRfact(n-1, n*result);
}/*End of TRFact()*/

/*Helper function for tail recursive function*/
long TailRecursiveFact(int n)
{
    return TRfact(n, 1);
}/*End of TailRecursiveFact()*/


find GCD of two numbers

/*P5.8 Program to find GCD of two numbers*/

#include<stdio.h>
int GCD(int a, int b);
int gcd(int a, int b);

main()
{

    int a, b;
    printf("Enter a and b : \n");
    scanf("%d%d",&a, &b);
    printf("%d\n",GCD(a,b));
    printf("%d\n",gcd(a,b));
}/*End of main()*/

/*Recursive*/
int GCD(int a, int b)  
{
    if(b==0)
        return a;
    return GCD(b, a%b);
}/*End of GCD()*/

/*Iterative*/
int gcd(int a, int b)
{
    int rem;
    while(b != 0)
    {
        rem = a%b;
        a = b;
        b = rem;
    }
    return a;
}/*End of gcd()*/



print the prime factors

/*P5.7 Program to print the prime factors*/

#include<stdio.h>
void PFactors( int num);
void IPFactors( int n);

main( )
{
    int num;
    printf("Enter a number : ");
    scanf("%d", &num);
    PFactors(num);    printf("\n");
    IPFactors(num);    printf("\n");
}/*End of main()*/

void PFactors( int num)
{
    int i = 2;
    if( num == 1 )
        return;
    while( num%i != 0 )
        i++;
    printf("%d ", i);
    PFactors(num/i);
}/*End of PFactors()*/

/*Iterative*/
void IPFactors( int num)
{
    int i;
    for( i = 2; num!=1; i++)
        while( num%i == 0 )
        {
            printf("%d ", i);
            num = num/i;
        }
}/*End of IPFactors()*/

raise a floating point number to a positive integer

/*P5.6 Program to raise a floating point number to a positive integer*/
#include<stdio.h>
float power(float a , int n);
float Ipower(float a , int n);
main( )
{
    float a, p;
    int n;
    printf("Enter a and n : ");
    scanf("%f %d", &a, &n);
    p = power(a, n);
    printf("%f raised to power %d is %f\n", a, n, p);
    p = Ipower(a, n);
    printf("%f raised to power %d is %f\n", a, n, p);
}/*End of main()*/

/*Recursive*/
float power(float a , int n)
{
    if(n == 0)
        return(1);
    else
        return(a * power(a,n-1));
}/*End of power()*/

/*Iterative*/
float Ipower(float a , int n)
{
    int i;
    float result=1;
    for(i=1; i<=n; i++)
        result = result * a;
    return result;
}/*End of Ipower()*/

convert a positive decimal number to Binary, Octal or Hexadecimal

/* P5.5 Program to convert a positive decimal number to Binary, Octal or Hexadecimal */
#include<stdio.h>
void convert(int, int);
main()
{
    int num;
    printf("Enter a positive decimal number : ");
    scanf("%d", &num);
    convert(num, 2);
    printf("\n");
    convert(num, 8);
    printf("\n");
    convert(num, 16);
    printf("\n");
}/*End of main()*/

void convert (int num, int base)
{
    int rem = num%base;
   
    if(num==0)
        return;
    convert(num/base, base);
   
    if(rem < 10)
        printf("%d", rem);   
    else
        printf("%c", rem-10+'A' );
}/*End of convert()*/


display integer as sequence of digits and find sum of its digits

/*P5.4 Program to display integer as sequence of digits and find sum of its digits*/

#include<stdio.h>
void display(long int n);
void Rdisplay(long int n);
int sumdigits( long int n);

main( )
{
    long int num;
    printf("Enter number : ");
    scanf("%ld", &num);
    printf("%d\n",sumdigits(num));
    printf("\n");
    display(num);
    printf("\n");
    Rdisplay(num);
    printf("\n");
}/*End of main()*/

/*Finds the sum of digits of an integer*/
int sumdigits(long int n)
{
    if( n/10 == 0 ) /* if n is a single digit number*/
        return n;
    return n%10 + sumdigits(n/10);       
}/*End of sumdigits()*/

/*Displays the digits of an integer*/
void display(long int n)
{
    if( n/10==0 )
    {
        printf("%d",n);
        return;
    }
    display(n/10);
    printf("%d",n%10);   
}/*End of display()*/

/*Displays the digits of an integer in reverse order*/
void Rdisplay(long int n)
{
    if(n/10==0)
    {
        printf("%d",n);
        return;
    }
    printf("%d",n%10);
    Rdisplay(n/10);
}/*End of Rdisplay()*/   

   

Series : 1 + 2 + 3 + 4 + 5 +.......

/*P5.3 Program to display and find out the sum of series*/
/* Series : 1 + 2 + 3 + 4 + 5 +....... */

#include<stdio.h>
int series(int n);
int rseries(int n);

main( )
{
    int n;
    printf("Enter number of terms : ");
    scanf("%d", &n);
   
    printf("\b\b = %d\n", series(n));    /*  \b to erase last +sign */
    printf("\b\b = %d\n\n\n", rseries(n));
}/*End of main()*/

/*Iterative function*/
int series(int n)
{
    int i, sum=0;
    for(i=1; i<=n; i++)
    {
        printf("%d + ", i);
        sum+=i;   
    }
    return sum;
}/*End of series()*/

/*Recursive function*/
int rseries(int n)
{
    int sum;
    if(n == 0)
        return 0;
    sum = (n + rseries(n-1));
    printf("%d + ",n);
    return sum;
}/*End of rseries()*/

display numbers from 1 to n and their sum

/*P5.2 Program to display numbers from 1 to n and their sum*/

#include<stdio.h>
int summation(int n);
void display1(int n);
void display2(int n);

main( )
{
    int n;
    printf("Enter number of terms : ");
    scanf("%d", &n);
   
    display1(n);
    printf("\n");

    display2(n);
    printf("\n");
   
    printf("sum = %d\n", summation(n));   
}/*End of main()*/
int summation( int n)
{
    if(n==0)
        return 0;
    return ( n + summation(n-1) );
}/*End of summation()*/

/*displays in reverse order*/
void display1(int n)
{
    if( n==0 )
       return;
    printf("%d ",n);   
    display1(n-1);
}/*End of display1()*/

void display2(int n)
{
    if( n==0 )
       return;
    display2(n-1);
    printf("%d ",n);   
}/*End of display2()*/

conversion of infix to postfix and evaluation of postfix. It will evaluate only single digit numbers

/*P4.11 Program for conversion of infix to postfix and evaluation of postfix.
It will evaluate only single digit numbers*/

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

#define BLANK ' '
#define TAB '\t'
#define MAX 50

void push(long int symbol);
long int pop();
void infix_to_postfix();
long int eval_post();
int priority(char symbol);
int isEmpty();
int white_space();

char infix[MAX], postfix[MAX];
long int stack[MAX];
int top;

main()
{
    long int value;
    top=-1;
    printf("Enter infix : ");
    gets(infix);
    infix_to_postfix();
    printf("Postfix : %s\n",postfix);
    value=eval_post();
    printf("Value of expression : %ld\n",value);
}/*End of main()*/

void infix_to_postfix()
{
    unsigned int i,p=0;
    char next;
    char symbol;   
    for(i=0;i<strlen(infix);i++)
    {
        symbol=infix[i];
        if(!white_space(symbol))
        {
            switch(symbol)
            {
            case '(':
                push(symbol);
                break;
            case ')':
                while((next=pop())!='(')
                    postfix[p++] = next;
                break;
            case '+':
            case '-':
            case '*':
            case '/':
            case '%':
            case '^':
                while( !isEmpty( ) &&  priority(stack[top])>= priority(symbol) )
                    postfix[p++]=pop();
                push(symbol);
                break;
            default: /*if an operand comes*/
                 postfix[p++]=symbol;
            }
        }
    }
    while(!isEmpty( ))
        postfix[p++]=pop();
    postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}/*End of infix_to_postfix()*/

/*This function returns the priority of the operator*/
int priority(char symbol)
{
    switch(symbol)
    {
    case '(':
        return 0;
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
    case '%':
        return 2;
    case '^':
        return 3;
    default :
        return 0;
    }
}/*End of priority()*/

void push(long int symbol)
{
    if(top>MAX)
    {
        printf("Stack overflow\n");
        exit(1);
    }
    stack[++top]=symbol;
}/*End of push()*/

long int pop()
{
    if( isEmpty() )
    {
        printf("Stack underflow\n");
        exit(1);
    }
    return (stack[top--]);
}/*End of pop()*/
int isEmpty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}/*End of isEmpty()*/

int white_space(char symbol)
{
    if( symbol == BLANK || symbol == TAB )
        return 1;
    else
        return 0;
}/*End of white_space()*/

long int eval_post()
{
    long int a,b,temp,result;
    unsigned int i;
   
    for(i=0;i<strlen(postfix);i++)
    {
        if(postfix[i]<='9' && postfix[i]>='0')
            push(postfix[i]-'0');
        else
        {
            a=pop();
            b=pop();
            switch(postfix[i])
            {
            case '+':
                temp=b+a; break;
            case '-':
                temp=b-a;break;
            case '*':
                temp=b*a;break;
            case '/':
                temp=b/a;break;
            case '%':
                temp=b%a;break;
            case '^':
                temp=pow(b,a);
            }
            push(temp);
        }
    }
    result=pop();
    return result;
}/*End of eval_post */

reversing a string using stack

/*P4.9 Program of reversing a string using stack */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 20

int top = -1;
char stack[MAX];
char pop();
void push(char);

main()
{
    char str[20];
    unsigned int i;
    printf("Enter the string : " );
    gets(str);
    /*Push characters of the string str on the stack */
    for(i=0;i<strlen(str);i++)
        push(str[i]);
    /*Pop characters from the stack and store in string str */
    for(i=0;i<strlen(str);i++)
        str[i]=pop();
    printf("Reversed string is : ");
    puts(str);
}/*End of main()*/

void push(char item)
{
    if(top == (MAX-1))
    {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] =item;
}/*End of push()*/

char pop()
{
    if(top == -1)
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    return stack[top--];
}/*End of pop()*/

priority queue using linked list

/*P4.8 Program of priority queue using linked list*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int priority;
    int info;
    struct node *link;
}*front=NULL;

void insert(int item, int item_priority);
int del();
void display();
int isEmpty();

main()
{
    int choice,item,item_priority;
    while(1)
    {
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Display\n");
        printf("4.Quit\n");
        printf("Enter your choice : ");
        scanf("%d", &choice);

        switch(choice)
        {
         case 1:
            printf("Input the item to be added in the queue : ");
            scanf("%d",&item);
            printf("Enter its priority : ");
            scanf("%d",&item_priority);
            insert(item, item_priority);
            break;
         case 2:
            printf("Deleted item is %d\n",del());           
            break;
         case 3:
            display();
            break;
         case 4:
            exit(1);
         default :
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

void insert(int item,int item_priority)
{
    struct node *tmp,*p;
   
    tmp=(struct node *)malloc(sizeof(struct node));
    if(tmp==NULL)
    {
        printf("Memory not available\n");
        return;
    }
    tmp->info=item;
    tmp->priority=item_priority;
    /*Queue is empty or item to be added has priority more than first element*/
    if( isEmpty() || item_priority < front->priority )
    {
        tmp->link=front;
        front=tmp;
    }
    else
    {
        p = front;
        while( p->link!=NULL && p->link->priority<=item_priority )
            p=p->link;
        tmp->link=p->link;
        p->link=tmp;
    }
}/*End of insert()*/

int del()
{
    struct node *tmp;
    int item;
    if( isEmpty() )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    else
    {
        tmp=front;
        item=tmp->info;
        front=front->link;
        free(tmp);
    }
    return item;
}/*End of del()*/

int isEmpty()
{
    if( front == NULL )
        return 1;
    else
        return 0;

}/*End of isEmpty()*/


void display()
{
    struct node *ptr;
    ptr=front;
    if( isEmpty() )
        printf("Queue is empty\n");
    else
    {    printf("Queue is :\n");
        printf("Priority       Item\n");
        while(ptr!=NULL)
        {
            printf("%5d        %5d\n",ptr->priority,ptr->info);
            ptr=ptr->link;
        }
    }
}/*End of display() */




circular queue

/*P4.6 Program of circular queue*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 10

int cqueue_arr[MAX];
int front=-1;
int rear=-1;

void display( );
void insert(item);
int del();
int peek();
int isEmpty();
int isFull();

main()
{
    int choice,item;
    while(1)
    {
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Peek\n");
        printf("4.Display\n");
        printf("5.Quit\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);

        switch(choice)
        {
        case 1 :
            printf("Input the element for insertion : ");
            scanf("%d",&item);
            insert(item);
            break;
        case 2 :
            printf("Element deleted is : %d\n",del());
            break;
        case 3:
            printf("Element at the front is  : %d\n",peek());
            break;
        case 4:
            display();
            break;
        case 5:
            exit(1);
        default:
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while */
}/*End of main()*/

void insert(int item)
{
    if( isFull() )
    {
        printf("Queue Overflow\n");
        return;
    }
    if(front == -1 ) 
        front=0;
   
    if(rear==MAX-1)/*rear is at last position of queue*/
        rear=0;
    else
        rear=rear+1;
    cqueue_arr[rear]=item ;
}/*End of insert()*/

int del()
{
    int item;
    if( isEmpty() )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    item=cqueue_arr[front];
    if(front==rear) /* queue has only one element */
    {
        front=-1;
        rear=-1;
    }
    else if(front==MAX-1)
        front=0;
    else
        front=front+1;
    return item;
}/*End of del() */

int isEmpty()
{
    if(front==-1)
        return 1;
    else
        return 0;
}/*End of isEmpty()*/

int isFull()
{
    if((front==0 && rear==MAX-1) || (front==rear+1))
        return 1;
    else
        return 0;
}/*End of isFull()*/

int peek()
{
    if( isEmpty() )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    return cqueue_arr[front];
}/*End of peek()*/

void display()
{
    int i;
    if(isEmpty())
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue elements :\n");
    i=front;
    if( front<=rear )
    {
        while(i<=rear)
            printf("%d ",cqueue_arr[i++]);
    }
    else
    {
        while(i<=MAX-1)
            printf("%d ",cqueue_arr[i++]);
        i=0;
        while(i<=rear)
            printf("%d ",cqueue_arr[i++]);
    }
    printf("\n");
}/*End of display() */

queue using circular linked list

/*P4.5 Program of queue using circular linked list*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
}*rear=NULL;

void insert(int item);
int del();
void display();
int isEmpty();
int peek();

main()
{
    int choice,item;
    while(1)
    {
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Peek\n");
        printf("4.Display\n");
        printf("5.Quit\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);

        switch(choice)
        {
         case 1:
            printf("Enter the element for insertion : ");
            scanf("%d",&item);
            insert(item);
            break;
         case 2:
            printf("Deleted element is %d\n",del());   
            break;
         case 3:
            printf("Item at the front of queue is %d\n",peek());
            break;
         case 4:
            display();
            break;
         case 5:
            exit(1);
         default:
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

void insert(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=item;
    if(tmp==NULL)
    {
        printf("Memory not available\n");
        return;
    }
   
    if( isEmpty() ) /*If queue is empty */
    {
        rear=tmp;
        tmp->link=rear;
    }
    else
    {
        tmp->link=rear->link;
        rear->link=tmp;
        rear=tmp;
    }
}/*End of insert()*/

del()
{
    int item;
    struct node *tmp;
    if( isEmpty() )
    {
        printf("Queue underflow\n");
        exit(1);
    }
    if(rear->link==rear)  /*If only one element*/
    {
        tmp=rear;
        rear=NULL;
    }
    else
    {
        tmp=rear->link;
        rear->link=rear->link->link;
    }
    item=tmp->info;
    free(tmp);
    return item;
}/*End of del()*/

int peek()
{
    if( isEmpty() )
    {
        printf("Queue underflow\n");
        exit(1);
    }
    return rear->link->info;
}/* End of peek() */

int isEmpty()
{
    if( rear == NULL )
        return 1;
    else
        return 0;
}/*End of isEmpty()*/


void display()
{
    struct node *p;
    if(isEmpty())
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue is :\n");
    p=rear->link;
    do
    {
        printf("%d ",p->info);
        p=p->link;
    }while(p!=rear->link);
    printf("\n");
}/*End of display()*/

queue using linked list

/*P4.4 Program of queue using linked list*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
}*front=NULL,*rear=NULL;

void insert(int item);
int del();
int peek();
int isEmpty();
void display();

main()
{
    int choice,item;
    while(1)
    {      
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Display the element at the front\n");
        printf("4.Display all elements of the queue\n");
        printf("5.Quit\n");
        printf("Enter your choice : ");
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            printf("Input the element for adding in queue : ");
            scanf("%d",&item);
            insert(item);
            break;
        case 2:
            printf("Deleted element is  %d\n",del());
            break;
        case 3:
            printf("Element at the front of the queue is %d\n", peek() );
            break;
        case 4:
            display();
            break;
        case 5:
            exit(1);
        default :
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

void insert(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    if(tmp==NULL)
    {
        printf("Memory not available\n");
        return;
    }
    tmp->info = item;
    tmp->link=NULL;
    if(front==NULL)         /*If Queue is empty*/
        front=tmp;
    else
        rear->link=tmp;
    rear=tmp;
}/*End of insert()*/

int del()
{
    struct node *tmp;
    int item;
    if( isEmpty( ) )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    tmp=front;
    item=tmp->info;
    front=front->link;
    free(tmp);
    return item;
}/*End of del()*/

int peek()
{
    if( isEmpty( ) )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    return front->info;
}/*End of peek()*/

int isEmpty()
{
    if(front==NULL)
        return 1;
    else
        return 0;

}/*End of isEmpty()*/

void display()
{
    struct node *ptr;
    ptr=front;
    if(isEmpty())
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue elements :\n\n");
    while(ptr!=NULL)
    {
        printf("%d ",ptr->info);
        ptr=ptr->link;
    }
    printf("\n\n");
}/*End of display()*/

queue using array

/*P4.3 Program of queue using array*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 10

int queue_arr[MAX];
int rear=-1;
int front=-1;

void insert(int item);
int del();
int peek();
void display();
int isFull();
int isEmpty();

main()
{
    int choice,item;
    while(1)
    {
        printf("1.Insert\n");
        printf("2.Delete\n");
        printf("3.Display element at the front\n");
        printf("4.Display all elements of the queue\n");
        printf("5.Quit\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:
            printf("Input the element for adding in queue : ");
            scanf("%d",&item);
            insert(item);
            break;
        case 2:
            item=del();
            printf("Deleted element is  %d\n",item);
            break;
        case 3:
            printf("Element at the front is %d\n",peek());
            break;
        case 4:
            display();
            break;
        case 5:
            exit(1);
        default:
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

void insert(int item)
{
    if( isFull() )
    {
        printf("Queue Overflow\n");
        return;
    }
    if( front == -1 ) 
        front=0;
    rear=rear+1;
    queue_arr[rear]=item ;
}/*End of insert()*/

int del()
{
    int item;
    if( isEmpty() )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    item=queue_arr[front];
    front=front+1;
    return item;
}/*End of del()*/

int peek()
{
    if( isEmpty() )
    {
        printf("Queue Underflow\n");
        exit(1);
    }
    return queue_arr[front];
}/*End of peek()*/

int isEmpty()
{
    if( front==-1 || front==rear+1 )
        return 1;
    else
        return 0;
}/*End of isEmpty()*/

int isFull()
{
    if( rear==MAX-1 )
        return 1;
    else
        return 0;
}/*End of isFull()*/

void display()
{
    int i;
    if ( isEmpty() )
    {
        printf("Queue is empty\n");
        return;
    }
    printf("Queue is :\n\n");
    for(i=front;i<=rear;i++)
        printf("%d  ",queue_arr[i]);
    printf("\n\n");
}/*End of display() */

stack using linked list

/*P4.2 Program of stack using linked list*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
}*top=NULL;

void push(int item);
int pop();
int peek();
int isEmpty();
void display();

main()
{
    int choice,item;
    while(1)
    {         
        printf("1.Push\n");
        printf("2.Pop\n");
        printf("3.Display item at the top\n");
        printf("4.Display all items of the stack\n");
        printf("5.Quit\n");
        printf("Enter your choice : ") ;
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            printf("Enter the item to be pushed : ");
            scanf("%d",&item);
            push(item);
            break;
        case 2:
            item=pop();
            printf("Popped item is : %d\n",item);
            break;
        case 3:
            printf("Item at the top is %d\n",peek());
            break;
        case 4:
            display();
            break;
        case 5:
            exit(1);
        default :
            printf("Wrong choice\n");
        }/*End of switch */
    }/*End of while */
}/*End of main() */

void push(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    if(tmp==NULL)
    {
        printf("Stack Overflow\n");
        return;
    }
    tmp->info=item;
    tmp->link=top;
    top=tmp;
}/*End of push()*/

int pop()
{
    struct node *tmp;
    int item;
    if( isEmpty() )
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    tmp=top;
    item=tmp->info;
    top=top->link;
    free(tmp);
    return item;
}/*End of pop()*/

int peek()
{
    if( isEmpty() )
    {
        printf("Stack Underflow\n");
        exit(1);
    }
    return top->info;
}/*End of peek() */

int isEmpty()
{
    if(top == NULL)
        return 1;
    else
        return 0;
}/*isEmpty()*/


void display()
{      
    struct node *ptr;
    ptr=top;
    if(isEmpty())
    {   
        printf("Stack is empty\n");
        return;
    }
    printf("Stack elements :\n\n");
    while(ptr!=NULL)
    {
        printf(" %d\n",ptr->info);
        ptr=ptr->link;
    }
    printf("\n");
}/*End of display()*/

Tuesday, 21 February 2012

polynomial addition and multiplication using linked list

/* P3.10 Program of polynomial addition and multiplication using linked list */
#include<stdio.h>
#include<stdlib.h>

struct node
{
    float coef;
    int expo;
    struct node *link;
};

struct node *create(struct node *);
struct node *insert_s(struct node *,float,int);
struct node *insert(struct node *,float,int);
void display(struct node *ptr);
void poly_add(struct node *,struct node *);
void poly_mult(struct node *,struct node *);
main( )
{
    struct node *start1=NULL,*start2=NULL;
   
    printf("Enter polynomial 1 :\n");
    start1=create(start1);

    printf("Enter polynomial 2 :\n");
    start2=create(start2);

    printf("Polynomial 1 is :  ");
    display(start1);
    printf("Polynomial 2 is :  ");
    display(start2);
       
    poly_add(start1, start2);
    poly_mult(start1, start2);
}/*End of main()*/

struct node *create(struct node *start)
{
    int i,n,ex;
    float co;
    printf("Enter the number of terms : ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("Enter coeficient for term %d : ",i);
        scanf("%f",&co);
        printf("Enter exponent for term %d : ",i);
        scanf("%d",&ex);
        start=insert_s(start,co,ex);
    }
    return start;
}/*End of create()*/
struct node *insert_s(struct node *start,float co,int ex)
{
    struct node *ptr,*tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->coef=co;
    tmp->expo=ex;
    /*list empty or exp greater than first one */
    if(start==NULL || ex > start->expo)
    {
        tmp->link=start;
        start=tmp;
    }
    else   
    {
        ptr=start;
        while(ptr->link!=NULL && ptr->link->expo >= ex)
            ptr=ptr->link;
        tmp->link=ptr->link;
        ptr->link=tmp;
    }
    return start;
}/*End of insert()*/
   
struct node *insert(struct node *start,float co,int ex)
{
    struct node *ptr,*tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->coef=co;
    tmp->expo=ex;
    /*If list is empty*/
    if(start==NULL)
    {
        tmp->link=start;
        start=tmp;
    }
    else    /*Insert at the end of the list*/
    {
        ptr=start;
        while(ptr->link!=NULL)
            ptr=ptr->link;
        tmp->link=ptr->link;
        ptr->link=tmp;
    }
    return start;
}/*End of insert()*/

void display(struct node *ptr)
{
    if(ptr==NULL)
    {
        printf("Zero polynomial\n");
        return;
    }
    while(ptr!=NULL)
    {
        printf("(%.1fx^%d)", ptr->coef,ptr->expo);
        ptr=ptr->link;
        if(ptr!=NULL)
            printf(" + ");
        else
            printf("\n");
    }
}/*End of display()*/
void poly_add(struct node *p1,struct node *p2)
{
    struct node *start3;
    start3=NULL;
   
    while(p1!=NULL && p2!=NULL)
    {
        if(p1->expo > p2->expo)
        {
            start3=insert(start3,p1->coef,p1->expo);
            p1=p1->link;
        }
        else if(p2->expo > p1->expo)
        {
            start3=insert(start3,p2->coef,p2->expo);
            p2=p2->link;
        }
        else if(p1->expo==p2->expo)
        {
            start3=insert(start3,p1->coef+p2->coef,p1->expo);
            p1=p1->link;
            p2=p2->link;
        }
    }
    /*if poly2 has finished and elements left in poly1*/
    while(p1!=NULL)
    {
        start3=insert(start3,p1->coef,p1->expo);
        p1=p1->link;
    }
    /*if poly1 has finished and elements left in poly2*/
    while(p2!=NULL)
    {
        start3=insert(start3,p2->coef,p2->expo);
        p2=p2->link;
    }
    printf("Added polynomial is : ");
    display(start3);
}/*End of poly_add() */

void poly_mult(struct node *p1, struct node *p2)
{
    struct node *start3;
    struct node *p2_beg = p2;
    start3=NULL;
    if(p1==NULL || p2==NULL)
    {
        printf("Multiplied polynomial is zero polynomial\n");
        return;
    }
    while(p1!=NULL)
    {
        p2=p2_beg;
        while(p2!=NULL)
        {
            start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo);
            p2=p2->link;   
        }
        p1=p1->link;
    }   
    printf("Multiplied polynomial is : ");
    display(start3);
}/*End of poly_mult()*/       

concatenate two circular linked lists

/* P3.9 Program to concatenate two circular linked lists*/
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
};

struct node *create_list(struct node *last);
void display(struct node *last);
struct node *addtoempty(struct node *last,int data );
struct node *addatend(struct node *last,int data);
struct node *concat(struct node *last1,struct node *last2);

main( )
{
    struct node *last1=NULL,*last2=NULL;
    last1=create_list(last1);
    last2=create_list(last2);
    printf("First list is :  ");
    display(last1);
    printf("Second list is :  ");
    display(last2);
    last1=concat(last1, last2);
    printf("Concatenated list is  : ");
    display(last1);
}/*End of main( )*/

struct node *concat( struct node *last1,struct node *last2)
{
    struct node *ptr;
    if(last1==NULL)
    {
        last1=last2;
        return last1;
    }
    if(last2==NULL )  
        return last1;
    ptr=last1->link;
    last1->link=last2->link;
    last2->link=ptr;
    last1=last2;
    return last1;
}
struct node *create_list(struct node *last)
{
    int i,n;
    int data;
    printf("Enter the number of nodes : ");
    scanf("%d",&n);
    last=NULL;
    if(n==0)
        return last;
    printf("Enter the element to be inserted : ");
    scanf("%d",&data);
    last=addtoempty(last,data);
       
    for(i=2;i<=n;i++)
    {
        printf("Enter the element to be inserted : ");
        scanf("%d",&data);
        last=addatend(last,data);   
    }
    return last;
}

void display(struct node *last)
{
    struct node *p;
    if(last==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=last->link;  /*p points to first node*/
    do
    {
        printf("%d ", p->info);
        p=p->link;
    }while(p!=last->link);
    printf("\n");
}/*End of display( )*/

struct node *addtoempty(struct node *last,int data)
{
    struct node *tmp;
    tmp = (struct node *)malloc(sizeof(struct node));
    tmp->info = data;
    last = tmp;
    last->link = last;
    return last;
}/*End of addtoempty( )*/

struct node *addatend(struct node *last,int data)
{
    struct node *tmp;
    tmp = (struct node *)malloc(sizeof(struct node));
    tmp->info = data;
    tmp->link = last->link;
    last->link = tmp;
    last = tmp;
    return last;
}/*End of addatend( )*/

concatenate two single linked lists

/* P3.8 Program to concatenate two single linked lists*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
};
struct node *create_list(struct node *);
struct node *concat( struct node *start1,struct node *start2);
struct node *addatbeg(struct node *start, int data);
struct node *addatend(struct node *start,int data);
void display(struct node *start);

main()
{
    struct node *start1=NULL,*start2=NULL;
    start1=create_list(start1);
    start2=create_list(start2);
    printf("First list is  : ");
    display(start1);
    printf("Second list is  : ");
    display(start2);
    start1=concat(start1, start2);
    printf("Concatenated list is  : ");
    display(start1);
}/*End of main()*/

struct node *concat( struct node *start1,struct node *start2)
{
    struct node *ptr;
    if(start1==NULL)
    {
        start1=start2;
        return start1;
    }
    if(start2==NULL)  
        return start1;
    ptr=start1;
    while(ptr->link!=NULL)
        ptr=ptr->link;
    ptr->link=start2;   
    return start1;
}
struct node *create_list(struct node *start)
{
    int i,n,data;
    printf("Enter the number of nodes : ");
    scanf("%d",&n);
    start=NULL;
    if(n==0)
        return start;

    printf("Enter the element to be inserted : ");
    scanf("%d",&data);
    start=addatbeg(start,data);

    for(i=2;i<=n;i++)
    {
        printf("Enter the element to be inserted : ");
        scanf("%d",&data);
        start=addatend(start,data);   
    }
    return start;
}/*End of create_list()*/

void display(struct node *start)
{
    struct node *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=start;
    while(p!=NULL)
    {
        printf("%d ", p->info);
        p=p->link;
    }
    printf("\n");
}/*End of display() */

struct node *addatbeg(struct node *start,int data)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=start;
    start=tmp;
    return start;
}/*End of addatbeg()*/

struct node *addatend(struct node *start, int data)
{
    struct node *p,*tmp;
    tmp= (struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    p=start;
    while(p->link!=NULL)
        p=p->link;
    p->link=tmp;
    tmp->link=NULL;
    return start;
}/*End of addatend()*/

merging two sorted single linked lists

/* P3.7 Program of merging two sorted single linked lists*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
};

struct node *create(struct node *start);
struct node *insert_s(struct node *start,int data);
struct node *insert(struct node *start,int data);
void display(struct node *start );
void merge(struct node *p1,struct node *p2);
main()
{
    struct node *start1=NULL,*start2=NULL;   
    start1=create(start1);
    start2=create(start2);
   
    printf("List1 : ");   
    display(start1);
    printf("List2 : ");
    display(start2);
    merge(start1, start2);
}/*End of main()*/

void merge(struct node *p1,struct node *p2)
{
    struct node *start3;
    start3=NULL;
   
    while(p1!=NULL && p2!=NULL)
    {
        if(p1->info < p2->info)
        {
            start3=insert(start3,p1->info);
            p1=p1->link;
        }
        else if(p2->info < p1->info)
        {
            start3=insert(start3,p2->info);
            p2=p2->link;
        }
        else if(p1->info==p2->info)
        {
            start3=insert(start3,p1->info);
            p1=p1->link;
            p2=p2->link;
        }
    }
    /*If second list has finished and elements left in first list*/
    while(p1!=NULL)
    {
        start3=insert(start3,p1->info);
        p1=p1->link;
    }
    /*If first list has finished and elements left in second list*/
    while(p2!=NULL)
    {
        start3=insert(start3,p2->info);
        p2=p2->link;
    }
    printf("Merged list is : ");
    display(start3);
}

struct node *create(struct node *start )
{
    int i,n,data;
    printf("Enter the number of nodes : ");
    scanf("%d",&n);
    start=NULL;
    for(i=1;i<=n;i++)
    {
        printf("Enter the element to be inserted : ");
        scanf("%d",&data);
        start=insert_s(start, data);
    }
    return start;
}/*End of create_slist()*/

struct node *insert_s(struct node *start,int data)
{
    struct node *p,*tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    /*list empty or data to be added in beginning */
    if(start==NULL || data<start->info)
    {
        tmp->link=start;
        start=tmp;
        return start;
    }
    else
    {
        p=start;
        while(p->link!=NULL && p->link->info < data)
            p=p->link;
        tmp->link=p->link;
        p->link=tmp;
    }
    return start;
}/*End of insert_s()*/

struct node *insert(struct node *start,int data)
{
    struct node *p,*tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    /*If list is empty*/
    if(start==NULL)
    {
        tmp->link=start;
        start=tmp;
        return start;
    }
    else    /*Insert at the end of the list*/
    {
        p=start;
        while(p->link!=NULL)
            p=p->link;
        tmp->link=p->link;
        p->link=tmp;
    }
    return start;
}/*End of insert()*/

void display(struct node *start)
{
    struct node *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=start;
    while(p!=NULL)
    {
        printf("%d ",p->info);
        p=p->link;
    }
    printf("\n");
}/*End of display()*/

sorting a single linked list

/* P3.6 Program of sorting a single linked list*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
};

struct node *create_list(struct node *start );
void display(struct node *start);
struct node *addatbeg(struct node *start,int data);
struct node *addatend(struct node *start,int data);
void selection(struct node *start);
void bubble(struct node *start);
struct node *selection_l(struct node *start);
struct node *bubble_l(struct node *start);

main()
{
    int choice;
    struct node *start=NULL;   
    while(1)
    {
        printf("1.Create List\n");
        printf("2.Display\n");
        printf("3.Bubble Sort\n");
        printf("4.Selection Sort\n");
        printf("5.Bubble Sort by changing links\n");
        printf("6.Selection Sort by changing links\n");
        printf("7.Quit\n\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
         case 1:
            start=create_list(start);
            break;
         case 2:
            display(start);
            break;
         case 3:
            bubble(start);
            break;
         case 4:
            selection(start);
            break;
         case 5:
            start=bubble_l(start);
            break;
         case 6:
            start=selection_l(start);
            break;
         case 7:
             exit(1);
         default:
            printf("Wrong choice\n\n");
        }/*End of switch */
    }/*End of while */
}/*End of main()*/

struct node *create_list(struct node *start)
{
    int i,n,data;
    printf("Enter the number of nodes : ");
    scanf("%d",&n);
    start=NULL;
    if(n==0)
        return start;

    printf("Enter the element to be inserted : ");
    scanf("%d",&data);
    start=addatbeg(start,data);

    for(i=2;i<=n;i++)
    {
        printf("Enter the element to be inserted : ");
        scanf("%d",&data);
        start=addatend(start,data);   
    }
    return start;
}/*End of create_list()*/

void display(struct node *start)
{
    struct node *p;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=start;
    printf("List is : ");
    while(p!=NULL)
    {
        printf("%d ",p->info);
        p=p->link;
    }
    printf("\n");
}/*End of display() */

struct node *addatbeg(struct node *start,int data)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=start;
    start=tmp;
    return start;
}/*End of addatbeg()*/

struct node *addatend(struct node *start,int data)
{
    struct node *p,*tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    p=start;
    while(p->link!=NULL)
        p=p->link;
    p->link=tmp;
    tmp->link=NULL;
    return start;
}/*End of addatend()*/

void selection(struct node *start )
{
    struct node *p, *q;
    int tmp;
    p=start;
    for(p=start; p->link!=NULL; p=p->link )
    {
        for(q=p->link; q!=NULL; q=q->link)
        {
            if(p->info > q->info )
            {
                tmp=p->info;
                p->info=q->info;
                q->info=tmp;
            }
        }
    }
}/*End of selection( )*/

void bubble(struct node *start )
{
    struct node *end,*p,*q;
    int tmp;
    for(end=NULL; end!=start->link; end=q)
    {
          for(p=start; p->link!=end; p=p->link)
        {
            q=p->link;
            if(p->info > q->info)
            {
                tmp=p->info;
                p->info=q->info;
                q->info=tmp;
            }
        }
    }
}/*End of bubble( )*/       
struct node *selection_l(struct node *start)
{
    struct node *p,*q,*r,*s,*tmp;
   
    for(r=p=start; p->link!=NULL; r=p,p=p->link)
    {
        for(s=q=p->link; q!=NULL; s=q,q=q->link)
        {
            if(p->info > q->info)
            {
                tmp=p->link;
                p->link=q->link;
                q->link=tmp;
                if(p!=start)
                    r->link=q;
                s->link=p;
                if(p==start)
                    start=q;
                tmp=p;
                p=q;
                q=tmp;
            }
        }
    }
    return start;
}/*End of selection_l( )*/

struct node *bubble_l(struct node *start)
{
    struct node *end,*r,*p,*q,*tmp;
       
    for(end=NULL; end!=start->link; end=q)
    {
          for(r=p=start; p->link!=end; r=p,p=p->link)
        {
            q=p->link;
            if(p->info > q->info )
            {
                p->link=q->link;
                q->link=p;
                if(p!=start)
                    r->link=q;
                else
                    start=q;
                tmp=p;
                p=q;
                q=tmp;
            }
        }
       
    }
    return start;
}/*End of bubble_l( )*/       

orted linked list

/* P3.5 Program of sorted linked list*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
};
struct node *insert_s(struct node *start,int data);
void search(struct node *start,int data);
void display(struct node *start);
main()
{
    int choice,data;
    struct node *start=NULL;
    while(1)
    {
        printf("1.Insert\n");
        printf("2.Display\n");
        printf("3.Search\n");
        printf("4.Exit\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
         case 1:
            printf("Enter the element to be inserted : ");
            scanf("%d",&data);
            start=insert_s(start,data);
            break;
         case 2:
            display(start);
            break;
         case 3:
            printf("Enter the element to be searched : ");
            scanf("%d",&data);
            search(start,data);
            break;
         case 4:
            exit(1);
         default:
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
} /*end of main */

struct node *insert_s(struct node *start,int data)
{
    struct node *p,*tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    /*list empty or new node to be added before first node*/
    if(start==NULL || data<start->info)
    {
        tmp->link=start;
        start=tmp;
        return start;
    }
    else
    {
        p=start;
        while(p->link!=NULL && p->link->info < data)
            p=p->link;
        tmp->link=p->link;
        p->link=tmp;
    }
    return start;
}/*End of insert()*/
void search(struct node *start,int data)
{
    struct node *p;
    int pos;
   
    if(start==NULL || data < start->info)
    {
        printf("%d not found in list\n",data);
        return;
    }
    p=start;
    pos=1;
    while(p!=NULL && p->info<=data)
    {
        if(p->info==data)
        {
            printf("%d found at position %d\n",data,pos);
            return;
        }
        p=p->link;
        pos++;
    }
    printf("%d not found in list\n",data);
}/*End of search()*/

void display(struct node *start)
{
    struct node *q;
    if(start==NULL)
    {
        printf("List is empty\n");
        return;
    }
    q=start;
    printf("List is :\n");
    while(q!=NULL)
    {
        printf("%d ",q->info);
        q=q->link;
    }
    printf("\n");
}/*End of display() */

single linked list with header node

/*P3.4 Program of single linked list with header node*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int info;
    struct node *link;
};

struct node *create_list(struct node *head);
void display(struct node *head);
struct node *addatend(struct node *head,int data);
struct node *addbefore(struct node *head,int data,int item );
struct node *addatpos(struct node *head,int data,int pos);
struct node *del(struct node *head,int data);
struct node *reverse(struct node *head);

main()
{
    int choice,data,item,pos;
    struct node *head;   
    head=(struct node *)malloc(sizeof(struct node));
    head->info=0;   
    head->link=NULL;
   
    head=create_list(head);
   
    while(1)
    {
       
        printf("1.Display\n");
        printf("2.Add at end\n");
        printf("3.Add before node\n");
        printf("4.Add at position\n");
        printf("5.Delete\n");
        printf("6.Reverse\n");
        printf("7.Quit\n\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
         case 1:
            display(head);
            break;
         case 2:
            printf("Enter the element to be inserted : ");
            scanf("%d",&data);
            head=addatend(head,data);
            break;
         case 3:
            printf("Enter the element to be inserted : ");
            scanf("%d",&data);
            printf("Enter the element before which to insert : ");
            scanf("%d",&item);
            head=addbefore(head,data,item);
            break;
         case 4:
            printf("Enter the element to be inserted : ");
            scanf("%d",&data);
            printf("Enter the position at which to insert : ");
            scanf("%d",&pos);
            head=addatpos(head,data,pos);
            break;
         case 5:
            printf("Enter the element to be deleted : ");
            scanf("%d",&data);
            head=del(head,data);   
            break;
         case 6:
            head=reverse(head);
            break;
         case 7:
             exit(1);
         default:
            printf("Wrong choice\n\n");
        }/*End of switch */
    }/*End of while */
}/*End of main()*/

struct node *create_list(struct node *head)
{
    int i,n,data;
    printf("Enter the number of nodes : ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        printf("Enter the element to be inserted : ");
        scanf("%d",&data);
        head=addatend(head,data);   
    }
    return head;
}/*End of create_list()*/

void display(struct node *head)
{
    struct node *p;
    if(head->link==NULL)
    {
        printf("List is empty\n");
        return;
    }
    p=head->link;
    printf("List is :\n");
    while(p!=NULL)
    {
        printf("%d ",p->info);
        p=p->link;
    }
    printf("\n");
}/*End of display() */

struct node *addatend(struct node *head,int data)
{
    struct node *p,*tmp;
    tmp= (struct node *)malloc(sizeof(struct node));
    tmp->info=data;
    p=head;
    while(p->link!=NULL)
        p=p->link;
    p->link=tmp;
    tmp->link=NULL;
    return head;
}/*End of addatend()*/

struct node *addbefore(struct node *head,int data,int item)
{
    struct node *tmp,*p;
    p=head;
    while(p->link!=NULL)
    {
        if(p->link->info==item)
        {
            tmp=(struct node *)malloc(sizeof(struct node));
            tmp->info=data;
            tmp->link=p->link;
            p->link=tmp;
            return head;
        }
        p=p->link;
    }
    printf("%d not present in the list\n",item);
    return head;
}/*End of addbefore()*/       

struct node *addatpos(struct node *head,int data,int pos)
{
    struct node *tmp,*p;
    int i;
    tmp=(struct node *)malloc(sizeof(struct node) );
    tmp->info=data;
    p=head;
    for(i=1;i<=pos-1;i++)
    {
        p=p->link;
        if(p==NULL)
        {
            printf("There are less than %d elements\n",pos);
            return head;
        }
    }
    tmp->link=p->link;
    p->link=tmp;
    return head;
}/*End of addatpos()*/

struct node *del(struct node *head, int data)
{
    struct node *tmp,*p;
    p=head;
    while(p->link!=NULL)
    {
        if(p->link->info==data)  
        {
            tmp=p->link;
            p->link=tmp->link;
            free(tmp);
            return head;
        }
        p=p->link;
    }
    printf("Element %d not found\n",data);
    return head;
}/*End of del()*/

struct node *reverse(struct node *head)
{
    struct node *prev, *ptr, *next;
    prev=NULL;
    ptr=head->link;
    while(ptr!=NULL)
    {
        next=ptr->link;
        ptr->link=prev;
        prev=ptr;
        ptr=next;
    }
    head->link=prev;
    return head;
}/*End of reverse()*/