C Program to reverse the inserted number using Linked List

#include <stdio.h>

#include <stdlib.h>


struct digit {

    int num;

    struct digit *next;

};


struct digit * createDigit(int);

struct digit * append(struct digit * end, struct digit * newDigptr);

struct digit * readNumber();

struct digit * reverseNumber(struct digit * start);

struct digit * insertAtFront(struct digit * start, struct digit * newptr); 


void printNumber(struct digit *);

void freeNumber(struct digit *start);


int main(void) 

{

    struct digit *start,*backward,*sorted;

    printf("Enter a number:\n");

    start=readNumber();

    printf("You entered:\n");

    printNumber(start);

    backward=reverseNumber(start);

    printf("\nReverse order of the given num:\n");

    printNumber(backward);

    freeNumber(start);

    freeNumber(backward);

    return 0;

}

struct digit *readNumber()

{

    int d;

    char c;

    struct digit * start,*end,*newptr;

    start=NULL;

    scanf("%c",&c);

    while(c!='\n')

    {

        d=c-48;

        newptr=createDigit(d);

    if(start==NULL)

    {

        start=end=newptr;

        

    }

    else 

    {

        end=append(end,newptr);

    }

    scanf("%c",&c);

    }

    return start;

}


struct digit * createDigit(int num)

{

    struct digit *start=(struct digit *)malloc(sizeof(struct digit*));

    start->num=num;

    start->next=NULL;

    return start;

}


struct digit *append(struct digit *end, struct digit * newptr)

{

    end->next=newptr;

    return(end->next);

}


void printNumber(struct digit *start)

{

    struct digit * ptr=start;

    while(ptr!=NULL)

    {

        printf("%d",ptr->num);

        ptr=ptr->next;

    }

}


struct digit * reverseNumber(struct digit *start)

{

    struct digit *ptr,*backptr,*newptr;

    backptr=NULL;

    ptr=start;

    if(backptr==NULL)

    {

    newptr=createDigit(ptr->num);

    backptr=newptr;

    ptr=ptr->next;

    }

    while(ptr!=NULL)

    {

    newptr=createDigit(ptr->num);

    backptr=insertAtFront(backptr,newptr);

    ptr=ptr->next;

    }

    return backptr;

}


struct digit *insertAtFront(struct digit * backptr, struct digit *newptr)

{

    newptr->next=backptr;

    return(newptr);

}


void freeNumber( struct digit * start)

{

    struct digit * ptr=start;

    while(ptr!=NULL)

    {

        start=ptr;

        ptr=ptr->next;

        free(start);

        

    }

}


       OUTPUT:



***Comment if any doubt*****


Comments

Popular posts from this blog

[ C++ program] Assume that a bank maintains two kinds of accounts for customers, | | one called as savings account and the other as current account. | | The savings account provides compound interest and withdrawal facilities but no cheque book facility. | | The current account provides cheque book facility but no interest. | | Current account holders should also maintain a minimum balance | | and if the balance falls below this level, a service charge is imposed. | | Create a class account that stores customer name, account number and type of account. | | From this derive the classes curacct and savacct to make them more specific to their requirements.

Write a program in C++ to create a base class shape and derive two classes named Circle and Square from it. Create and initialize objects from these classes using appropriate constructors.

Perform all operation in a singly linked list using C language: