/*

this PROGRAM FINDS RANDOM N NUMBER IN THE INTERVAL OF [1,N]

by Serdar Ulutas

sulutas@eng.marmara.edu.tr

*/

#include "stdio.h"

#include "stdlib.h"

#include "conio.h"

    struct Node{

    int num;

    struct Node * nextPtr;

};

typedef struct Node Node;

typedef Node * node;

node headPtr=NULL,currentPtr=NULL,newPtr=NULL,lastPtr=NULL;

int limit;

void MakeNull(node genPtr)

    {

    genPtr->num=0;

    genPtr->nextPtr=NULL;

}

int ControlR(int num)

    {

    int bool=1;

    currentPtr=headPtr;

    while((currentPtr!=NULL)&&(bool!=0))

        {

        if(currentPtr->num==num) bool=0;

        currentPtr=currentPtr->nextPtr;

    }

    if(bool==0) return 0;

    else return 1;

}

void RandomR()

    {

    int i,num;

    for(i=0;i

        {

        num=rand()%limit+1;

        if(headPtr==NULL)

            {

            headPtr=(node)malloc(sizeof(Node)); MakeNull(headPtr);

            headPtr->num=num; lastPtr=headPtr;

        }

            else{

            if(ControlR(num)) // search the array for controlling

                {

                 newPtr=(node)malloc(sizeof(Node)); MakeNull(newPtr);

                 newPtr->num=num; lastPtr->nextPtr=newPtr; lastPtr=newPtr;

            }

            else i--;

        } // finding the number for the index of the previous one

    }

}

void DisplayR(void) // displays the array

    {

    currentPtr=headPtr;

    while(currentPtr!=NULL)

        {

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

        currentPtr=currentPtr->nextPtr;

    }

}

void main()

    {

    int key=1;

    while(key!=27)

        {

        textmode(2);

        srand(time(NULL));

        clrscr();

        printf("Enter the limit : ");

        scanf("%d",&limit);

        RandomR(); // find random n numbers

        DisplayR(); // display the array

        // after this stage you can use selectio

        //     n sort method to sort array

        printf("\n\n\nPress ESC to exit");

        key=getch();

        headPtr=NULL;

    }

}