Friday, March 18, 2011

Concatenate two linked lists

C Program to concatenate one linked list at end of another and than to erase all nodes present in the linked list

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

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

void append ( struct node **, int ) ;
void concat ( struct node **, struct node ** ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
struct node * erase ( struct node * ) ;

void main( )
{
    struct node *first, *second ;

    first = second = NULL ;  /* empty linked lists */

    append ( &first, 1 ) ;
    append ( &first, 2 ) ;
    append ( &first, 3 ) ;
    append ( &first, 4 ) ;

    clrscr( ) ;
    printf ( "\nFirst List : " ) ;
    display ( first ) ;
    printf ( "\nNo. of elements in the first Linked List = %d",
            count ( first ) ) ;

    append ( &second, 5 ) ;
    append ( &second, 6 ) ;
    append ( &second, 7 ) ;
    append ( &second, 8 ) ;

    printf ( "\n\nSecond List : " ) ;
    display ( second ) ;
    printf ( "\nNo. of elements in the second Linked List = %d",
            count ( second ) ) ;

    /* the result obtained after concatenation is in the first list */
    concat ( &first, &second ) ;

    printf ( "\n\nConcatenated List : " ) ;
    display ( first ) ;

    printf ( "\n\nNo. of elements in Linked List before erasing = %d",
            count ( first ) ) ;

    first = erase ( first ) ;
    printf ( "\nNo. of elements in Linked List after erasing = %d",
            count ( first ) ) ;
}

/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
    struct node *temp ;
    temp = *q ;

    if ( *q == NULL )  /* if the list is empty, create first node */
    {
        *q = malloc ( sizeof ( struct node ) ) ;
        temp = *q ;
    }
    else
    {
        /* go to last node */
        while ( temp -> link != NULL )
            temp = temp -> link ;

        /* add node at the end */
        temp -> link = malloc ( sizeof ( struct node ) ) ;
        temp = temp -> link ;
    }

    /* assign data to the last node */
    temp -> data = num ;
    temp -> link = NULL ;
}

/* concatenates two linked lists */
void concat ( struct node **p, struct node **q )
{
    struct node *temp ;

    /* if the first linked list is empty */
    if ( *p == NULL )
        *p = *q ;
    else
    {
        /* if both linked lists are non-empty */
        if ( *q != NULL )
        {
            temp = *p ;  /* points to the starting of the first list */

            /* traverse the entire first linked list */
            while ( temp -> link != NULL )
                temp = temp -> link ;

            temp -> link = *q ;  /* concatenate the second list after the
                                 first */
        }
    }
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
    printf ( "\n" ) ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        printf ( "%d ", q -> data ) ;
        q = q -> link ;
    }
}

/* counts the number of nodes present in the linked list */
int count ( struct node *q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        q = q -> link ;
        c++ ;
    }

    return c ;
}

/* erases all the nodes from a linked list */
struct node * erase ( struct node *q )
{
    struct node *temp ;

    /* traverse till the end erasing each node */
    while ( q != NULL )
    {
        temp = q ;
        q = q -> link ;
        free ( temp ) ;  /* free the memory occupied by the node */
    }

    return NULL ;
}

Implement a circular queue as a linked list.

C Program to implement a circular queue as a linked list.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node * link ;
} ;

void addcirq ( struct node **, struct node **, int ) ;
int delcirq ( struct node **, struct node ** ) ;
void cirq_display ( struct node * ) ;

void main( )
{
    struct node *front, *rear ;

    front = rear = NULL ;

    addcirq ( &front, &rear, 10 ) ;
    addcirq ( &front, &rear, 17 ) ;
    addcirq ( &front, &rear, 18 ) ;
    addcirq ( &front, &rear, 5 ) ;
    addcirq ( &front, &rear, 30 ) ;
    addcirq ( &front, &rear, 15 ) ;

    clrscr( ) ;

    printf ( "Before deletion:\n" ) ;
    cirq_display ( front ) ;

    delcirq ( &front, &rear ) ;
    delcirq ( &front, &rear ) ;
    delcirq ( &front, &rear ) ;

    printf ( "\n\nAfter deletion:\n" ) ;
    cirq_display ( front ) ;
}

/* adds a new element at the end of queue */
void addcirq ( struct node **f, struct node **r, int item )
{
    struct node *q ;

    /* create new node */
    q = malloc ( sizeof ( struct node ) ) ;
    q -> data = item ;

    /* if the queue is empty */
    if ( *f == NULL )
        *f = q ;
    else
        ( *r ) -> link = q ;

    *r = q ;
    ( *r ) -> link = *f ;
}

/* removes an element from front of queue */
int delcirq ( struct node **f, struct node **r )
{
    struct node *q ;
    int item ;

    /* if queue is empty */
    if ( *f == NULL )
        printf ( "queue is empty" ) ;
    else
    {
        if ( *f == *r )
        {
            item = ( *f ) -> data ;
            free ( *f ) ;
            *f = NULL ;
            *r = NULL ;
        }
        else
        {
            /* delete the node */
            q = *f ;
            item = q -> data ;
            *f = ( *f ) -> link ;
            ( *r ) -> link = *f ;
            free ( q ) ;
        }
        return ( item ) ;
    }
    return NULL ;
}

/* displays whole of the queue */
void cirq_display ( struct node *f )
{
    struct node *q = f, *p = NULL ;

    /* traverse the entire linked list */
    while ( q != p )
    {
        printf ( "%d\t", q -> data ) ;

        q = q -> link ;
        p = f ;
    }
}

Sort a linked list by readjusting the links.

C Program to sort a linked list by readjusting the links.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node *link ;
} *start, *visit ;

void getdata( ) ;
void append ( struct node **q, int num ) ;
void displaylist( ) ;
void selection_sort( ) ;
void bubble_sort( ) ;

void main( )
{
    getdata( ) ;
    clrscr( ) ;
    printf ( "\nLinked List Before Sorting:\n" ) ;
    displaylist( ) ;

    selection_sort( ) ;
    printf ( "\nLinked List After Selection Sorting:\n" ) ;
    displaylist( ) ;
    getch( ) ;

    getdata( ) ;
    clrscr( ) ;
    printf ( "\nLinked List Before Sorting:\n" ) ;
    displaylist( ) ;

    bubble_sort( ) ;
    printf ( "\nLinked List After Bubble Sorting:\n" ) ;
    displaylist( ) ;
    getch( ) ;
}

void getdata( )
{
    int val, n ;
    char ch ;
    struct node *newnode;

    clrscr( ) ;

    newnode = NULL ;
    do
    {
        printf ( "\nEnter a value: " ) ;
        scanf ( "%d", &val ) ;

        append ( &newnode, val ) ;

        printf ( "\nAny More Nodes (Y/N): " ) ;
        ch = getche( ) ;
    } while ( ch == 'y' || ch == 'Y' ) ;

    start =  newnode ;
}

/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
    struct node *temp ;
    temp = *q ;

    if ( *q == NULL ) /* if the list is empty, create first node */
    {
        *q = malloc ( sizeof ( struct node ) ) ;
        temp = *q ;
    }
    else
    {
        /* go to last node */
        while ( temp -> link != NULL )
            temp = temp -> link ;

        /* add node at the end */
        temp -> link = malloc ( sizeof ( struct node ) ) ;
        temp = temp -> link ;
    }

    /* assign data to the last node */
    temp -> data = num ;
    temp -> link = NULL ;
}

/* displays the contents of the linked list */
void displaylist( )
{
    visit = start ;

    /* traverse the entire linked list */
    while ( visit != NULL )
    {
        printf ( "%d ", visit -> data ) ;
        visit = visit -> link ;
    }
}

void selection_sort( )
{
    struct node *p, *q, *r, *s, *temp ;

    p = r = start ;
    while ( p -> link != NULL )
    {
        s = q = p -> link ;
        while ( q != NULL )
        {
            if ( p -> data > q -> data )
            {
                if ( p -> link == q ) /* Adjacent Nodes */
                {
                    if ( p == start )
                    {
                        p -> link = q -> link ;
                        q -> link = p ;

                        temp = p ;
                        p = q ;
                        q = temp ;

                        start = p ;
                        r = p ;
                        s = q ;
                        q = q -> link ;
                    }
                    else
                    {
                        p -> link = q -> link ;
                        q -> link = p ;
                        r -> link = q ;

                        temp = p ;
                        p = q ;
                        q = temp ;

                        s = q ;
                        q = q -> link ;
                    }
                }
                else
                {
                    if ( p == start )
                    {
                        temp = q -> link ;
                        q -> link = p -> link ;
                        p -> link = temp ;

                        s -> link = p ;

                        temp = p ;
                        p = q ;
                        q = temp ;

                        s = q ;
                        q = q -> link ;
                        start = p ;
                    }
                    else
                    {
                        temp = q -> link ;
                        q -> link = p -> link ;
                        p -> link = temp ;

                        r -> link = q ;
                        s -> link = p ;

                        temp = p ;
                        p = q ;
                        q = temp ;

                        s = q ;
                        q = q -> link ;
                    }
                }
            }
            else
            {
                s = q ;
                q = q -> link ;
            }
        }
        r = p ;
        p = p -> link ;
    }
}

void bubble_sort( )
{
    struct node *p, *q, *r, *s, *temp ;
    s = NULL ;

    /* r precedes p and s points to the node up to which comparisons are to be made */
    while ( s != start -> link )
    {
        r = p = start ;
        q = p -> link ;

        while ( p != s )
        {
            if ( p -> data > q -> data )
            {
                if ( p == start )
                {
                    temp = q -> link ;
                    q -> link = p ;
                    p -> link = temp ;

                    start = q ;
                    r = q ;
                }
                else
                {
                    temp = q -> link ;
                    q -> link = p ;
                    p -> link = temp ;

                    r -> link = q ;
                    r = q ;
                }
            }
            else
            {
                r = p ;
                p = p -> link ;
            }
            q = p -> link ;
            if ( q == s )
                s = p ;
        }
    }
}

Sort a linked list by swapping data.

C Program to sort a linked list by swapping data.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node *link ;
} *newnode, *start, *visit ;

void getdata( ) ;
void append ( struct node **, int ) ;
void displaylist( ) ;
int count ( struct node * ) ;
void selection_sort ( int ) ;
void bubble_sort ( int ) ;

void main( )
{
    int n ;

    getdata( ) ;

    clrscr( ) ;
    printf ( "Linked List Before Sorting: " ) ;
    displaylist( ) ;

    n = count ( start ) ;

    selection_sort ( n ) ;
    printf ( "\nLinked List After Selection Sorting: " ) ;
    displaylist( ) ;
    getch( ) ;

    getdata( ) ;
    clrscr( ) ;
    printf ( "Linked List Before Sorting: " ) ;
    displaylist( ) ;

    n = count ( start ) ;

    bubble_sort ( n ) ;
    printf ( "\nLinked List After Bubble Sorting: " ) ;
    displaylist( ) ;
    getch( ) ;
}

void getdata( )
{
    int val, n ;
    char ch ;
    struct node *new ;

    clrscr( ) ;

    new = NULL ;
    do
    {
        printf ( "\nEnter a value: " ) ;
        scanf ( "%d", &val ) ;

        append ( &new, val ) ;

        printf ( "\nAny More Nodes (Y/N): " ) ;
        ch = getche( ) ;
    } while ( ch == 'y' || ch == 'Y' ) ;

    start = new ;
}

/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
    struct node *temp ;
    temp = *q ;

    if ( *q == NULL ) /* if the list is empty, create first node */
    {
        *q = malloc ( sizeof ( struct node ) ) ;
        temp = *q ;
    }
    else
    {
        /* go to last node */
        while ( temp -> link != NULL )
            temp = temp -> link ;

        /* add node at the end */
        temp -> link = malloc ( sizeof ( struct node ) ) ;
        temp = temp -> link ;
    }

    /* assign data to the last node */
    temp -> data = num ;
    temp -> link = NULL ;
}

/* displays the contents of the linked list */
void displaylist( )
{
    visit = start ;

    /* traverse the entire linked list */
    while ( visit != NULL )
    {
        printf ( "%d ", visit -> data ) ;
        visit = visit -> link ;
    }
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        q = q -> link ;
        c++ ;
    }

    return c ;
}

void selection_sort ( int n )
{
    int i, j, k, temp ;
    struct node *p, *q ;

    p = start ;
    for ( i = 0 ; i < n - 1 ; i++ )
    {
        q = p -> link ;

        for ( j = i + 1 ; j < n ; j++ )
        {
            if ( p -> data > q -> data )
            {
                temp = p -> data ;
                p -> data = q -> data ;
                q -> data = temp ;
            }
            q = q -> link ;
        }
        p = p -> link ;
    }
}

void bubble_sort ( int n )
{
    int i, j, k, temp ;
    struct node *p, *q ;

    k = n ;
    for ( i = 0 ; i < n - 1 ; i++, k-- )
    {
        p = start ;
        q = p -> link ;

        for ( j = 1 ; j < k ; j++ )
        {
            if ( p -> data > q -> data )
            {
                temp = p -> data ;
                p -> data = q -> data ;
                q -> data = temp ;
            }
            p = p -> link ;
            q = q -> link ;
        }
    }
}


Merge two linked list, restricting common elements to occur only once

C Program to merge two linked list, restricting common elements to occur only once

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node *link ;
} ;

void add ( struct node **, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void merge ( struct node *, struct node *, struct node ** ) ;

void main( )
{
    struct node *first, *second, *third ;
    first = second = third = NULL ;  /* empty linked lists */

    add ( &first, 9 ) ;
    add ( &first, 12 ) ;
    add ( &first, 14 ) ;
    add ( &first, 17 ) ;
    add ( &first, 35 ) ;
    add ( &first, 61 ) ;
    add ( &first, 79 ) ;

    clrscr( ) ;
    printf ( "First linked list : " ) ;
    display ( first ) ;
    printf ( "\nNo. of elements in Linked List : %d" , count ( first ) ) ;

    add ( &second, 12 ) ;
    add ( &second, 17 ) ;
    add ( &second, 24 ) ;
    add ( &second, 36 ) ;
    add ( &second, 59 ) ;
    add ( &second, 64 ) ;
    add ( &second, 87 ) ;

    printf ( "\n\nSecond linked list : " ) ;
    display ( second ) ;
    printf ( "\nNo. of elements in Linked List : %d" , count ( second ) ) ;

    merge ( first, second, &third ) ;

    printf ( "\n\nThe merged list : " ) ;
    display ( third ) ;
    printf ( "\nNo. of elements in Linked List : %d", count ( third ) ) ;
}

/* adds node to an ascending order linked list */
void add ( struct node **q, int num )
{
    struct node *r, *temp = *q ;

    r = malloc ( sizeof ( struct node ) ) ;
    r -> data = num ;

    /* if list is empty or if new node is to be inserted before the first node */
    if ( *q == NULL || ( *q ) -> data > num )
    {
        *q = r ;
        ( *q ) -> link = temp ;
    }
    else
    {
        /* traverse the entire linked list to search the position to insert the new node */
        while ( temp != NULL )
        {
            if ( temp -> data < num && ( temp -> link -> data > num ||
                temp -> link == NULL ))
            {
                r -> link = temp -> link ;
                temp -> link = r ;
                return ;
            }
            temp = temp -> link ;  /*go to next node */
        }

        r -> link = NULL ;
        temp -> link = r ;
    }
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
    printf ( "\n" ) ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        printf ( "%d ", q -> data ) ;
        q = q -> link ;
    }
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        q = q -> link ;
        c++ ;
    }

    return c ;
}

/* merges the two linked lists, restricting the common elements to occur only once in the final list */
void merge ( struct node *p, struct node *q, struct node **s )
{
    struct node *z ;

    z = NULL ;

    /* if both lists are empty */
    if ( p == NULL && q == NULL )
        return ;

    /* traverse both linked lists till the end. If end of any one list is reached loop is terminated */
    while ( p != NULL && q != NULL )
    {
        /* if node being added in the first node */
        if ( *s == NULL )
        {
            *s = malloc ( sizeof ( struct node ) ) ;
            z = *s ;
        }
        else
        {
            z -> link = malloc ( sizeof ( struct node ) ) ;
            z = z -> link ;
        }

        if ( p -> data < q -> data )
        {
            z -> data = p -> data ;
            p = p -> link ;
        }
        else
        {
            if ( q -> data < p -> data )
            {
                z -> data = q -> data ;
                q = q -> link ;
            }
            else
            {
                if ( p -> data == q -> data )
                {
                    z -> data = q -> data ;
                    p = p -> link ;
                    q = q -> link ;
                }
            }
        }
    }

    /* if end of first list has not been reached */
    while ( p != NULL )
    {
        z -> link = malloc ( sizeof ( struct node ) ) ;
        z = z -> link ;
        z -> data = p -> data ;
        p = p -> link ;
    }

    /* if end of second list has been reached */
    while ( q != NULL )
    {
        z -> link = malloc ( sizeof ( struct node ) ) ;
        z = z -> link ;
        z -> data = q -> data ;
        q = q -> link ;
    }
    z -> link = NULL ;
}

Reverse a linked list

C Program to reverse a linked list

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node *link ;
} ;

void addatbeg ( struct node **, int ) ;
void reverse ( struct node ** ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;

void main( )
{
    struct node *p ;
    p = NULL ;  /* empty linked list */

    addatbeg ( &p, 7 ) ;
    addatbeg ( &p, 43 ) ;
    addatbeg ( &p, 17 ) ;
    addatbeg ( &p, 3 ) ;
    addatbeg ( &p, 23 ) ;
    addatbeg ( &p, 5 ) ;

    clrscr( ) ;
    display ( p ) ;
    printf ( "\nNo. of elements in the linked list = %d", count ( p ) ) ;

    reverse ( &p ) ;
    display ( p ) ;
    printf ( "\nNo. of elements in the linked list = %d", count ( p ) ) ;
}

/* adds a new node at the beginning of the linked list */
void addatbeg ( struct node **q, int num )
{
    struct node *temp ;

    /* add new node */
    temp = malloc ( sizeof ( struct node ) ) ;
    temp -> data = num ;
    temp -> link = *q ;
    *q = temp ;
}

void reverse ( struct node **x )
{
    struct node *q, *r, *s ;

    q = *x ;
    r = NULL ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        s = r ;
        r = q ;
        q = q -> link ;
        r -> link = s ;
    }

    *x = r ;
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
    printf ( "\n" ) ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        printf ( "%d ", q -> data ) ;
        q = q -> link ;
    }
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        q = q -> link ;
        c++ ;
    }
    return c ;
}

Add a new node to the ascending order linked list.

  • C Program to add a new node to the ascending order linked list.
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node *link ;
} ;

void add ( struct node **, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void delete ( struct node **, int ) ;

void main( )
{
    struct node *p ;
    p = NULL ;  /* empty linked list */

    add ( &p, 5 ) ;
    add ( &p, 1 ) ;
    add ( &p, 6 ) ;
    add ( &p, 4 ) ;
    add ( &p, 7 ) ;

    clrscr( ) ;
    display ( p ) ;
    printf ( "\nNo. of elements in Linked List = %d", count ( p ) ) ;
}

/* adds node to an ascending order linked list */
void add ( struct node **q, int num )
{
    struct node *r, *temp = *q ;

    r = malloc ( sizeof ( struct node ) ) ;
    r -> data = num ;

    /* if list is empty or if new node is to be inserted before the first node */
    if ( *q == NULL || ( *q ) -> data > num )
    {
        *q = r ;
        ( *q ) -> link = temp ;
    }
    else
    {
        /* traverse the entire linked list to search the position to insert the
            new node */
        while ( temp != NULL )
        {
            if ( temp -> data <= num && ( temp -> link -> data > num ||
                                        temp -> link == NULL ))
            {
                r -> link = temp -> link ;
                temp -> link = r ;
                return ;
            }
            temp = temp -> link ;  /* go to the next node */
        }
    }
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
    printf ( "\n" ) ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        printf ( "%d ", q -> data ) ;
        q = q -> link ;
    }
}

/* counts the number of nodes present in the linked list */
int count ( struct node *q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        q = q -> link ;
        c++ ;
    }
    return c ;
}


Maintain a linked list

  • C Program to maintain a linked list
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
    int data ;
    struct node * link ;
} ;

void append ( struct node **, int ) ;
void addatbeg ( struct node **, int ) ;
void addafter ( struct node *, int, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void delete ( struct node **, int ) ;

void main( )
{
    struct node *p ;
    p = NULL ;  /* empty linked list */

    printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
    append ( &p, 14 ) ;
    append ( &p, 30 ) ;
    append ( &p, 25 ) ;
    append ( &p, 42 ) ;
    append ( &p, 17 ) ;

    display ( p ) ;

    addatbeg ( &p, 999 ) ;
    addatbeg ( &p, 888 ) ;
    addatbeg ( &p, 777 ) ;

    display ( p ) ;

    addafter ( p, 7, 0 ) ;
    addafter ( p, 2, 1 ) ;
    addafter ( p, 5, 99 ) ;

    display ( p ) ;
    printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;

    delete ( &p, 99 ) ;
    delete ( &p, 1 ) ;
    delete ( &p, 10 ) ;

    display ( p ) ;
    printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
}

/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
    struct node *temp, *r ;

    if ( *q == NULL ) /* if the list is empty, create first node */
    {
        temp = malloc ( sizeof ( struct node ) ) ;
        temp -> data = num ;
        temp -> link = NULL ;
        *q = temp ;
    }
    else
    {
        temp = *q ;

        /* go to last node */
        while ( temp -> link != NULL )
            temp = temp -> link ;

        /* add node at the end */
        r = malloc ( sizeof ( struct node ) ) ;
        r -> data = num ;
        r -> link = NULL ;
        temp -> link = r ;
    }
}

/* adds a new node at the beginning of the linked list */
void addatbeg ( struct node **q, int num )
{
    struct node *temp ;

    /* add new node */
    temp = malloc ( sizeof ( struct node ) ) ;

    temp -> data = num ;
    temp -> link = *q ;
    *q = temp ;
}

/* adds a new node after the specified number of nodes */
void addafter ( struct node *q, int loc, int num )
{
    struct node *temp, *r ;
    int i ;

    temp = q ;
    /* skip to desired portion */
    for ( i = 0 ; i < loc ; i++ )
    {
        temp = temp -> link ;

        /* if end of linked list is encountered */
        if ( temp == NULL )
        {
            printf ( "\nThere are less than %d elements in list", loc ) ;
            return ;
        }
    }

    /* insert new node */
    r = malloc ( sizeof ( struct node ) ) ;
    r -> data = num ;
    r -> link = temp -> link ;
    temp -> link = r ;
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
    printf ( "\n" ) ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        printf ( "%d ", q -> data ) ;
        q = q -> link ;
    }
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
    int c = 0 ;

    /* traverse the entire linked list */
    while ( q != NULL )
    {
        q = q -> link ;
        c++ ;
    }

    return c ;
}

/* deletes the specified node from the linked list */
void delete ( struct node **q, int num )
{
    struct node *old, *temp ;

    temp = *q ;

    while ( temp != NULL )
    {
        if ( temp -> data == num )
        {
            /* if node to be deleted is the first node in the linked list */
            if ( temp == *q )
                *q = temp -> link ;

            /* deletes the intermediate nodes in the linked list */
            else
                old -> link = temp -> link ;

            /* free the memory occupied by the node */
            free ( temp ) ;
            return ;
        }

        /* traverse the linked list till the last node is reached */
        else
        {
            old = temp ;  /* old points to the previous node */
            temp = temp -> link ;  /* go to the next node */
         }
    }

    printf ( "\nElement %d not found", num ) ;
}

Tuesday, March 8, 2011

Monday, March 7, 2011

C - Program to multiply two polynomials.

Code:

#include <stdio.h>
#include <conio.h>

#define MAX 10

struct term
{
    int coeff ;
    int exp ;
} ;

struct poly
{
    struct term t [10] ;
    int noofterms ;
} ;

void initpoly ( struct poly *) ;
void polyappend ( struct poly *, int, int ) ;
struct poly polyadd ( struct poly, struct poly ) ;
struct poly polymul ( struct poly, struct poly ) ;
void display ( struct poly ) ;

void main( )
{
    struct poly p1, p2, p3 ;

    clrscr( ) ;

    initpoly ( &p1 ) ;
    initpoly ( &p2 ) ;
    initpoly ( &p3 ) ;

    polyappend ( &p1, 1, 4 ) ;
    polyappend ( &p1, 2, 3 ) ;
    polyappend ( &p1, 2, 2 ) ;
    polyappend ( &p1, 2, 1 ) ;

    polyappend ( &p2, 2, 3 ) ;
    polyappend ( &p2, 3, 2 ) ;
    polyappend ( &p2, 4, 1 ) ;

    p3 = polymul ( p1, p2 ) ;

    printf ( "\nFirst polynomial:\n" ) ;
    display ( p1 ) ;

    printf ( "\n\nSecond polynomial:\n" ) ;
    display ( p2 ) ;

    printf ( "\n\nResultant polynomial:\n" ) ;
    display ( p3 ) ;

    getch( ) ;
}

/* initializes elements of struct poly */
void initpoly ( struct poly *p )
{
    int i ;
    p -> noofterms = 0 ;
    for ( i = 0 ; i < MAX ; i++ )
    {
        p -> t[i].coeff = 0 ;
        p -> t[i].exp = 0 ;
    }
}

/* adds the term of polynomial to the array t */
void polyappend ( struct poly *p, int c, int e )
{
    p -> t[p -> noofterms].coeff = c ;
    p -> t[p -> noofterms].exp =  e ;
    ( p -> noofterms ) ++ ;
}

/* displays the polynomial equation */
void display ( struct poly p )
{
    int flag = 0, i ;
    for ( i = 0 ; i < p.noofterms ; i++ )
    {
        if ( p.t[i].exp != 0 )
            printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;
        else
        {
            printf ( "%d", p.t[i].coeff ) ;
            flag = 1 ;
        }
    }
    if ( !flag )
        printf ( "\b\b  " ) ;

}
/* adds two polynomials p1 and p2 */
struct poly polyadd ( struct poly p1, struct poly p2 )
{
    int i, j, c ;
    struct poly p3 ;
    initpoly ( &p3 ) ;

    if ( p1.noofterms > p2.noofterms )
        c = p1.noofterms ;
    else
        c = p2.noofterms ;

    for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
    {
        if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
            break ;
        if ( p1.t[i].exp >= p2.t[j].exp )
        {
            if ( p1.t[i].exp == p2.t[j].exp )
            {
                p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
                p3.t[p3.noofterms].exp = p1.t[i].exp ;
                i++ ;
                j++ ;
            }
            else
            {
                p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
                p3.t[p3.noofterms].exp = p1.t[i].exp ;
                i++ ;
            }
        }
        else
        {
            p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
            p3.t[p3.noofterms].exp = p2.t[j].exp ;
            j++ ;
        }
    }
    return p3 ;
}

/* multiplies two polynomials p1 and p2 */
struct poly polymul ( struct poly p1, struct poly p2 )
{
    int coeff, exp ;
    struct poly temp, p3 ;

    initpoly ( &temp ) ;
    initpoly ( &p3 ) ;

    if ( p1.noofterms != 0 && p2.noofterms != 0 )
    {
        int i ;
        for ( i = 0 ; i < p1.noofterms ; i++ )
        {
            int j ;

            struct poly p ;
            initpoly ( &p ) ;

            for ( j = 0 ; j < p2.noofterms ; j++ )
            {
                coeff = p1.t[i].coeff * p2.t[j].coeff ;
                exp = p1.t[i].exp + p2.t[j].exp ;
                polyappend ( &p, coeff, exp ) ;
            }

            if ( i != 0 )
            {
                p3 = polyadd ( temp, p ) ;
                temp = p3  ;
            }
            else
                temp = p ;
        }
    }
    return p3 ;
}

C - Program to add two polynomials.

Code:

#include <stdio.h>
#include <conio.h>

#define MAX 10

struct term
{
    int coeff ;
    int exp ;
} ;

struct poly
{
    struct term t [10] ;
    int noofterms ;
} ;


void initpoly ( struct poly * ) ;
void polyappend ( struct poly *, int c, int e ) ;
struct poly polyadd ( struct poly, struct poly ) ;
void display ( struct poly ) ;

void main( )
{
    struct poly p1, p2, p3 ;

    clrscr( ) ;

    initpoly ( &p1 ) ;
    initpoly ( &p2 ) ;
    initpoly ( &p3 ) ;

    polyappend ( &p1, 1, 7 ) ;
    polyappend ( &p1, 2, 6 ) ;
    polyappend ( &p1, 3, 5 ) ;
    polyappend ( &p1, 4, 4 ) ;
    polyappend ( &p1, 5, 2 ) ;

    polyappend ( &p2, 1, 4 ) ;
    polyappend ( &p2, 1, 3 ) ;
    polyappend ( &p2, 1, 2 ) ;
    polyappend ( &p2, 1, 1 ) ;
    polyappend ( &p2, 2, 0 ) ;

    p3 = polyadd ( p1, p2 ) ;

    printf ( "\nFirst polynomial:\n" ) ;
    display ( p1 ) ;

    printf ( "\n\nSecond polynomial:\n" ) ;
    display ( p2 ) ;

    printf ( "\n\nResultant polynomial:\n" ) ;
    display ( p3 ) ;

    getch( ) ;
}

/* initializes elements of struct poly */
void initpoly ( struct poly *p )
{
    int i ;
    p -> noofterms = 0 ;
    for ( i = 0 ; i < MAX ; i++ )
    {
        p -> t[i].coeff = 0 ;
        p -> t[i].exp = 0 ;
    }
}

/* adds the term of polynomial to the array t */
void polyappend ( struct poly *p, int c, int e )
{
    p -> t[p -> noofterms].coeff = c ;
    p -> t[p -> noofterms].exp =  e ;
    ( p -> noofterms ) ++ ;
}

/* displays the polynomial equation */
void display ( struct poly p )
{
    int flag = 0, i ;
    for ( i = 0 ; i < p.noofterms ; i++ )
    {
        if ( p.t[i].exp != 0 )
            printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;
        else
        {
            printf ( "%d", p.t[i].coeff ) ;
            flag = 1 ;
        }
    }
    if ( !flag )
        printf ( "\b\b  " ) ;

}

/* adds two polynomials p1 and p2 */
struct poly polyadd ( struct poly p1, struct poly p2 )
{
    int i, j, c ;
    struct poly p3 ;
    initpoly ( &p3 ) ;

    if ( p1.noofterms > p2.noofterms )
        c = p1.noofterms ;
    else
        c = p2.noofterms ;

    for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
    {
        if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
            break ;
        if ( p1.t[i].exp >= p2.t[j].exp )
        {
            if ( p1.t[i].exp == p2.t[j].exp )
            {
                p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
                p3.t[p3.noofterms].exp = p1.t[i].exp ;
                i++ ;
                j++ ;
            }
            else
            {
                p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
                p3.t[p3.noofterms].exp = p1.t[i].exp ;
                i++ ;
            }
        }
        else
        {
            p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
            p3.t[p3.noofterms].exp = p2.t[j].exp ;
            j++ ;
        }
    }
    return p3 ;
}