Thread: Why this code of ordered insertion of linked list is not working in GCC CEntos?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Why this code of ordered insertion of linked list is not working in GCC CEntos?

    Where as the same fine on Dev-C++ (TDM-GCC 4.8.1 64-bit Release), gcc version on centos is (gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16)) Please tell me the reason for this:

    Code:
     #include<stdio.h>#include<stdlib.h>
    struct node
    {
        int i;
        struct node *next;
    };
    void main()
    {
    struct node *head,*temp,*p;
    int d;
    char ch;
    printf("Do you want to enter data? Y/N");
    scanf("%c",&ch);
    fflush(stdin);
    if((ch=='y')||(ch=='Y'))
    {
        printf("Enter your data: ");
        scanf("%d",&d);
        fflush(stdin);
        head=(struct node *)malloc(sizeof(struct node));
        head->i=d;
        head->next=NULL;
    }
    p=head;
    printf("Do you want to enter more data? Y/N");
    scanf("%c",&ch);
    fflush(stdin);
    while((ch=='y')||(ch=='Y'))
    {
        temp=(struct node *)malloc(sizeof(struct node));
        printf("Enter your data: ");
        scanf("%d",&d);
        fflush(stdin);
          temp->i=d;
        temp->next=NULL;
        if(p->i>=temp->i)
        {
        	temp->next=head;
        	head=temp;
    	}
    	else
    	{
            while((p->next!=NULL)&&(p->next->i<temp->i))
            {
                p=p->next;
        }
            temp->next=p->next;
            p->next=temp;
        }
        printf("Do you want to enter more data? Y/N");
    scanf("%c",&ch);
    fflush(stdin);
    p=head;
    }
    while(p!=NULL)
    {
        printf("%d ",p->i);
        p=p->next;
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    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

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    check out its resultant on running the program: Why this code of ordered insertion of linked list is not working in GCC CEntos?-hec-jpg

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Well, not that fflush(stdin) results in undefined behaviour: fflush is only defined for output streams and update streams for which the last operation was output. Therefore, you should replace fflush(stdin) with some other method of discarding what is in the input buffer, e.g., by reading and discarding characters until a newline or EOF is detected.

    Also, note that void main is non-standard: it should be int main.

    Although you have made some effort at indentation, your indentation could still be more consistent, e.g., the entire body of the main function should be indented by one level, and at some parts you indent unnecessarily.
    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

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint: Read the docs scanf - C++ Reference

    Code:
    scanf("%c",&ch);
    Should likely be
    Code:
    scanf(" %c",&ch);
    The space before the %c tells scanf to skip white space.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a Delete Algorithm in an ordered linked list
    By coolguy67 in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2013, 09:27 AM
  2. insertion in an ordered list ?
    By jack_carver in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 11:11 AM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. problme with ordered linked list
    By palku in forum C Programming
    Replies: 5
    Last Post: 09-19-2005, 04:33 PM