Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem on Linked list (http://www.go4expert.com/forums/linked-list-t29737/)

Shyam92 19Jul2013 12:14

Problem on Linked list
 
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));
}


Shyam92 19Jul2013 12:41

Re: Problem on Linked list
 
any help???

xpi0t0s 20Jul2013 13:48

Re: Problem on Linked list
 
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

xpi0t0s 20Jul2013 13:54

Re: Problem on Linked list
 
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

xpi0t0s 20Jul2013 13:56

Re: Problem on Linked list
 
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.


All times are GMT +5.5. The time now is 17:22.