Sunday 14 August 2016

  • SHOWING THE SECOND LAST ELEMENT OF A LINKED LISTS

    Language=C


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

    struct linked_lists
    {
        int data;
        struct linked_lists *next;
    };

    typedef struct linked_lists node;

    void create( node *list );

    void show_before_last( node *list );

    int main()
    {
        node *head;

        head = ( node* ) malloc( sizeof( node ) );

        create( head );
        show_before_last( head );

        return 0;
    }
    void create( node *list )
    {
        char c;

        scanf( "%d%c",&list->data,&c );

        if(c == '\n')

            list->next = NULL;

        else
        {
            list->next = ( node* ) malloc ( sizeof( node ) );

            create( list->next );
        }
        return;
    }
    void show_before_last( node *list )
    {
        while( list->next->next )

            list = list->next;

        printf( "%d\n",list->data );

        return;
    }

No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...