#include <stdlib.h>
#include<iostream>
#include<conio.h> 


typedef struct kume1{
       int data;
       struct kume1 *next;
       };
       
	   
typedef kume1 *kume1ptr;

void insert(kume1ptr *x,int a);
void del(kume1ptr *x);
int main(){
    int t=1;
       kume1ptr first1=NULL;
	   kume1ptr first2=NULL;
	   
	   int x;
	   int i;
	   for( i=10;i>=1;i--)//ikisi içinde 20 şer tane random sayı üretiyoruz
	   {
	   x=rand() % 100 + 1;
	  
	   insert(&first1,x);
	    printf("burdayim");
	   
	   

	   
	   }
        for(i=1;i<=50;i++){//ekrana büyükten küçüğe yazdırdık
		
		
		
		printf("%d\n",first1->data);
		first1=first1->next;
		
		
		
		

		}//ekrana büyükten küçüğe yazdırdık
	   
	   system("pause");
	   
	   }//Main sonu
	   
	   
       

       


void insert(kume1ptr *head,int datam)
{
     int t=1;
     kume1ptr current,yeni;
     yeni=(kume1 *)malloc(sizeof(kume1));
     
     if((*head)!=NULL){
                       current=*head;
                       if(datam>(*head)->data){
                                                   yeni->data=datam;
                                                    yeni->next=*head;
                                                     *head=yeni;
                                              
                                              }
                                              
                       
                       else{
                            
                            while(datam < current->data){
                                                      
                                                      t++;
                                                      printf("%d",t);
                                                      if((current->next)!=NULL){
                                                                              
                                                                              current=current->next;
                                                                              
                                                                              }
                                                                              else{
                                                                                  
                                                                              break;
                                                                              }
                                                      }
                            
                            yeni=current;
                            current->data=datam;
                            current->next=yeni;
                            }
                       
                       }
     
     
 else{yeni->data=datam;
 yeni->next=*head;
 *head=yeni;
 printf("%d",(*head)->data);
}     
     



                       
}

                       

//first1 =( kume1 *) malloc(sizeof(kume1));

hi i want to create random 20 numbers and put it in a linked list when i put a number into the list i put it in the order.so the head will always be the biggest.It works well when the numbers comes in a increasing way,but when a number which is less than the head it doesn't work.It goes to an infinite loop.

What am i doing wrong?

Thanks for any help;

Recommended Answers

All 3 Replies

When you create the current pointer in line 68, it is un initialized and points to some garbage location.
So when you use it in line 75 it goes into an infinite loop.
The solution to your problem is to initialize the current pointer when you create it

i think you write the line numbers wrong.could you please check it.
i initialize the current pointer at line 64

current=*head;

I think there is a logical error in the line 90-94. Let me use and example to explain the logic that should be used

Say you already have this lined list 10--20--30--40--50 and you want to insert 25. So it has to come between 20 and 30. So your while loop is something of this sort

while(current->data <data)     // This loop breaks at 30
{
     temp= current;
     current= current->next
}

When the loop breaks you have a pointer to 25 and 30
So now you have to place your new node between temp and current.You write code of this type
temp->link= new node
new node->link= current

I hope this clears your doubt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.