Thursday, November 30, 2017

Program to return maximum link value if all link values are unique, other wise returns maximum repeated link value

/*   Program Long Coding : IMGIT Online Paper Dt :29-11-2017
program return max link value if all link values are unique other wise returns maximum repeated link value
10 20 30 40 50  it has to return 50
10 20 20 20 30 20it has to return 20
 */
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>

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

struct link*first=NULL;
struct link*last=NULL;

addlink(int dat)
{
struct link*newlink;

newlink=(struct link*)malloc(sizeof(struct link));
newlink->data=dat;
newlink->next=first;
first=newlink;

/* init last link */
last=newlink;

}

displaylinks()
{
struct link*current;
current=first;
while(current!=NULL)
{
printf("\n%d",current->data);
current=current->next;
}
}
int getmax()
{
    struct link*current;
    int max;
    current=first;
    max=current->data;
while(current!=NULL)
{
    if(current->data>max)
            max=current->data;
                current=current->next;
}

return max;
}

int findrptlink()
{
    struct link*current,*curr;
    int ctr=0,max=0,rptval;
current=first;
while(current!=NULL)
{
    ctr=0;
    curr=first;
        while(curr!=NULL)
        {
    //printf("\t%d",curr->data);
    if(current->data==curr->data)
            ctr++;
        curr=curr->next;
        }

    if(max<ctr)
        {
            max=ctr;
            rptval=current->data;
            //printf("\n%d..%d",ctr,current->data);
        }
        current=current->next;
}

if(max==1)
printf("\n%d",getmax());
else
    printf("\n%d",rptval);

}
/* program return max link value if all link values are unique other wise returns maximum repeated link value */


int main()
{
//clrscr();
puts("Linked list program");
addlink(10);
addlink(10);
addlink(130);
addlink(30);
addlink(30);
addlink(10);

    //displaylinks();

//displaylinks();
findrptlink();



}

















No comments:

Post a Comment