Problem with my program...

This is a discussion on Problem with my program... within the C Programming forums, part of the General Programming Boards category; my program has not work properly... actually my program need to print the sorted numbers in linked list... and my ...

  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
    22,176
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:38 PM
  4. Replies: 20
    Last Post: 06-13-2005, 12:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21