1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem on Linked list

Discussion in 'C' started by Shyam92, Jul 19, 2013.

  1. Shyam92

    Shyam92 New Member

    Joined:
    Jul 19, 2013
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hello,
    I have written a program to print the maximum recurrence of a number in a dynamic list which keeps on accepting numbers until a negative number is pressed.The problem that i am facing is this program gives me the output for only if a number is repeated i.e if the inputs are given as 1 2 3 2 -1..the output i get is 2=2..where the first 2 is the data and the second 2 is the count.The problem arises when the input is given as 1 2 3 4 -1..the output should come as 1,2,3,4=1..since there is no recurrence of any number and all the numbers are equal..but the output for the above input is coming as 1=3.
    Some of the examples of inputs and outputs i need is:
    i/p:1 2 3 4 -1
    o/p:1,2,3,4=1
    i/p:1 2 3 2 -1
    o/p:2=2
    i/p:1 2 3 3 2 -1
    o/p:2=2,3
    kindly rectify my code!!
    PROGRAM:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
     int data;
     struct node *next;
    };
    struct node* insert(struct node* p, int n)
        {
            struct node* temp;
            if(p==NULL)
                {
                    p=((struct node*) malloc(sizeof(struct node)));
                    if(p==NULL)
                    {
                        printf("error\n");
                        exit(0) ;
                    }
                    p->data = n;
                    p->next = NULL;
            //printf("%d",p->data);
                }
                else
                p->next=insert(p->next,n);
            //    printf("%d",p->data);
            return (p);
        }
    int max_repeat(struct node *start)
    {
      struct node *ptr1, *ptr2; 
      int count=1,a=0,b;
      ptr1 = start;
      
      while(ptr1 != NULL && ptr1->next != NULL)
      {
       ptr2 = ptr1; 
       while(ptr2->next != NULL)
         {
            if(ptr1->data == ptr2->next->data)
              {
            count++;
          //a=count;
         ptr2->next = ptr2->next->next;
          //ptr3->data=ptr1->data;
               }
            else
          ptr2 = ptr2->next;
         }
        if(count>a)
        {    
            a=count;
            b=ptr1->data;
             
         }
        else if(count==a)
        {
            a=count;
            b=ptr1->data;
             
        }
     ptr1 = ptr1->next;
        
     }
    printf("%d=",b);
    return a;
    }
     
    void printlist(struct node *p)
    {
    while(p!= NULL)
       {
              printf("%d\t",p->data);
              p = p->next;
        }
    }
     
     
     
     
    struct node* insert(struct node*p, int n);
     void printlist(struct node *p);
     int main()
    {
        struct node *start = NULL;
          int x;
        while(1){
          printf("Enter the values\n");
        scanf("%d",&x);
        if(x<0)
        break;
        start=insert(start,x);}
         
         printf("%d",max_repeat(start));
    }
    
     
  2. Shyam92

    Shyam92 New Member

    Joined:
    Jul 19, 2013
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    any help???
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Your code just stuffs all input onto the end of the list without doing any processing. This will make your max_repeat function quite tricky to write. I would add a count variable and increase the count each time you enter a duplicate number instead of adding the duplicate to the end of the list. Here's the algorithm I would use:

    Data:

    Linked list of objects containing NUMBER and COUNT
    Variable: MAXCOUNT=0

    Algorithm:

    1
    Input number. If negative, goto 2
    Scan the list - do we find it? If so, increase the COUNT, and if COUNT>MAXCOUNT set MAXCOUNT=COUNT
    If not, add a new entry and set COUNT to 1
    Goto 1

    2
    FIRST=1
    Scan the list looking for entries with COUNT=MAXCOUNT
    If found
    __if FIRST=1
    ____display NUMBER
    ____FIRST=0
    __else
    ____display ,NUMBER
    End scan
    display =MAXCOUNT
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    So if you enter 1,2,3,4,-1 there will be 4 entries on the list each with COUNT=1, and MAXCOUNT=1 so part 2 will display all four as 1,2,3,4=1.

    If you enter 1,2,3,3,2,-1 there will be 3 entries on the list; 1's COUNT=1, 2,3's COUNT=2, MAXCOUNT=2, so part 2 will display 2,3=2
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you enter 1,3,2,2,3,-1 then the output will be 3,2=2. If that's a problem and you still want 2,3=2, then make the "add a new entry" step insert the new entry in the linked list in order, i.e. the 2 before the 3 instead of just adding it to the end.
     

Share This Page