Thread: Problem with my program...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Problem with my program...

    my program has not work properly...
    actually my program need to print the sorted numbers in linked list...
    and my program not print a sorted numbers in linked list...
    here is my code..please help me..


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    # include<conio.h>
    # include<malloc.h>
    
    
    struct node
    { 
       int data;
       struct node *next;
    };
    
    
    void in_begin(struct node **,int);
    void display(struct node *);
    void SortAscending(void);
    
    
    int iArray[4]={203,5,10,110};
    
    
    
    void main()
    {
    
       int i;
    
       printf("The original array is : ");
       for(i=0;i<4;i++)
        {
           printf("| %d |",iArray[i]);
        }
    
       printf("\n");
    
    
    SortAscending();
    
    }
    
    
    void SortAscending()
    {
       int i;
       int j;
       int temp;
       int sorted_array;
       struct node *p; 
       p=NULL;
    
       printf("\n");
       printf("Sorted Array");
       for(i = 0; i < 4; i++)
       {
          for(j = i+1; j < 4; j++)
          {
             if(iArray[i] > iArray[j])
             { 
                temp = iArray[i];
                iArray[i] = iArray[j];
                iArray[j] = temp;
              }
          }
    
       printf("| %d |",iArray[i]);
       }
    
       printf("\n\n");
    
       printf("The numbers has been sorted in Linked list like this : \n");
       for (i=0;i<4;i++)
         {
             sorted_array=iArray[i];
             in_begin(&p,sorted_array);
         }
       display(p);
    
    
    
    }
    
    
    void in_begin(struct node **q,int num)
    { 
       struct node *temp;
       if(*q==NULL)
       { 
          temp=(struct node *)malloc(sizeof(struct node));
          temp->data=num;
          temp->next=*q;
         *q=temp; /* pointing to the first node */
       }
       else
       { 
          printf("Nothing");
          getch();
          goto last;
       }
       last:
       getch();
    }
    
    
    void display(struct node *q)
    { 
       if(q==NULL)
       { 
          printf("\n\nEmpty Link List.Can't Display The Data");
          getch();
          goto last;
       }
    
       while(q!=NULL)
       { 
          printf("\n%d",q->data);
          q=q->next;
       }
    
       last:
       getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things that you should take note of:
    • You badly need to indent your code properly.
    • Remove those includes of <conio.h> and <malloc.h> as they are non-standard and the stuff from <stdio.h> and <stdlib.h> will do.
    • Stop using goto: you do not need it here anyway. In at least one case the goto leads to the next statement!
    • Avoid global variables. This will help you think more clearly about what your functions do so that each of the functions that you write will do one thing and do it well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM