C Programming Tutorial

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

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

void append ( struct node **, int ) ;
int compare ( struct node *, struct node * ) ;

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

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

    append ( &second, 1 ) ;
    append ( &second, 2 ) ;
    append ( &second, 3 ) ;

    clrscr( ) ;
    if ( compare ( first, second ) )
        printf ( \"Both linked lists are EQUAL\" ) ;
    else
        printf ( \"Linked lists are DIFFERENT\" ) ;
}

/* 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 ;
}

/* compares 2 linked lists and returns 1 if linked lists are equal and 0 if
    unequal */
int compare ( struct node *q, struct node *r )
{
    static int flag ;

    if ( ( q == NULL ) && ( r == NULL ) )
        flag = 1  ;
    else
    {
        if ( q == NULL || r == NULL )
            flag = 0  ;

        if ( q -> data != r -> data )
            flag = 0 ;
        else
            compare ( q -> link, r -> link ) ;
    }
    return ( flag ) ;
}

    Related Post:
  1. Program to find first occurrence of a character in a string and also position in string using strchr function from string.h

  2. Program to display entered character in capital letter

  3. PICARDS METHOD

  4. Program to check entered name in the master list

  5. Program that accepts a string in an array and prints the reverse of the string i.e. if the string is abcd then it will print dcba.

  6. PROGRAM FOR SECANT METHOD OF PARTICULAR EQUATION IS log(x)-cos(x)

  7. Program for production and sales analysis

  8. Program that displays use of register variable

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

  10. Program to search for a string into another string

  11. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBORD SUM =12-32+52-72+.....

  12. Program to display the conventional memory of the system

  13. Program that accepts characters in lower case, stores them in an array and prints them in the upper-case and vice - versa

  14. Program that takes characters from user in an array and prints them with their ASCII values.(Do not use atoi function)

  15. Program to get substring of entered text or word by taking starting index and number of characters from a user

  16. Program of 2-thread for Linux Using the POSIX threads library. Once the thread spawning is done, the first thread should wait for user input

  17. Program gives maximum and minimum value of datatype

  18. PROGRAM TO COMPUTE n! USING SELF-SCHEDULING

  19. Program to simulate a dictionary using linked list

  20. Program to take input of 5 candidates vote and display it using an array

 
 
Didn't find what you were looking for? Find more on Program to compare two linked lists using recursion