Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>

typedef struct node_type{
    int data;
    struct node_type *link;
}node;

typedef node *list;

void add_to_list(int,int);
void show();

list bucket[10];

main()
{   
    int number;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y'){
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
        }
    show();
}


void  add_to_list(int index,int cut){
     list q,r,temp;
     int i;
     if (bucket[index] == NULL){
              q = bucket[index] =(list)malloc(sizeof (node));
              q->data=cut;
              q->link=NULL;
}
     else{  q=bucket[index];
            while(q->link!=NULL) q=q->link;
            if(q->link==NULL){
            temp=(list)malloc(sizeof(node));
            temp->data=cut;
            temp->link=NULL;
            q->link=temp;
}
}
}
/*To show every element in the table*/

void show(){

         int i;
         list pemp;
         for(i=0;i<10;i++){
             pemp= (list)malloc(sizeof(node));
             pemp=bucket[i];
             while(pemp!=NULL){
                      printf("%d ",pemp->data);
                      pemp=pemp->link;}
}
}


What I have tried:

Could anyone please suggest me what can I do here? The problem is when I gave same input for twice or, more than twice, the program crashed. But when I got two distinct value or even more value the program worked well......
Posted
Updated 22-Mar-16 9:07am
v7
Comments
jeron1 8-Mar-16 14:07pm    
What exactly do you mean by "But it didn't work"?
Member 12378355we 8-Mar-16 14:16pm    
When I compiled the code on code blocks, it crashed!!!
jeron1 8-Mar-16 14:19pm    
By 'crashed' do you mean, it did not compile?
Member 12378355we 8-Mar-16 14:22pm    
Yes.
jeron1 8-Mar-16 14:25pm    
You should post the compilation error messages then.

"When I compiled the code on code blocks, it crashed!!!"

C++
q=bucket[index];
if(q==NULL){
    q->data=cut;
    q->link=NULL;


Yup. That will crash. Try this.

C++
if (bucket[index] == NULL)
{
    q = bucket[index] = (struct node *)malloc(sizeof node);
    q->data=cut;
    q->link=NULL;


This should get you started.
 
Share this answer
 
Comments
Member 12378355we 12-Mar-16 16:36pm    
You are welcome! But when I input two same numbers then the program crashed again..... Help me please. I updated my code...
[no name] 14-Mar-16 12:44pm    
In your else statement, you need to assign q to something before using it.
Member 12378355we 15-Mar-16 5:53am    
Yes. And it's done. You real helpful........:)
Do yourself a favour and sort out your indentation!
If nothing else, it would make it obvious to you what the problem is!
main()
{   int number,index;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y'){
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
    }
    show();
}
} // <<<--- What is this closing?
Formatted:
main(){
    int number,index;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y'){
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
    }
    show();
}
}  // <<<--- It's obvious that is spurious.

Or better, don't use 1TB
main()
    {
    int number,index;
    list tet;
    char ch;
    printf("Enter y if u want to input:\n");
    tet=bucket[0];
    scanf("%c",&ch);
    while(ch=='y')
        {
        printf("Enter numbers\n");
        scanf("%d\n",&number);
        add_to_list(number/10,number);
        scanf("%c",&ch);
        }
    show();
    }
}  // <<<---- Still very obvious.

Your other code is even better at hiding things...:laugh:
 
Share this answer
 
Comments
Patrice T 8-Mar-16 15:33pm    
+5 conter vote
OriginalGriff 8-Mar-16 16:06pm    
Likewise! :laugh:
I think there is one } too many at the end of main.

You should use an editor for programmer like UltraEdit or notepad++ or any other.
these editors are said "programmer editor" because they have feature that allow language highlight and automatic indentation or re-indentation.
 
Share this answer
 
Comments
OriginalGriff 8-Mar-16 16:07pm    
You can tell VS to support "straight" C rather than C++ as well.
Patrice T 8-Mar-16 16:13pm    
Thank you.
I don't use VS :)
OriginalGriff 8-Mar-16 16:34pm    
It's damn good, as IDEs go.
And being able to change the code while debugging it? Luxury! :laugh:
Patrice T 8-Mar-16 17:04pm    
Unfortunately, my language of choice is not supported by VS
since 25 year A make and maintain apps in xBase family languages.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900