Thread: adding new nodes to linked list

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    adding new nodes to linked list

    I have this code to sort a linked list which asks the user to add nodes to the lists till the user presses y or Y. But what is happening currently is that after insertion of one node the program moves ahead without waiting for the input from the user. Is it that i am doing something wrong when asking input from the user.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int data;
    	struct node* link;
    } *start;
    
    void addatend(struct node**, int);
    void display(struct node*);
    int count(struct node*);
    void selection_sort(int);
    void bubble_sort(int);
    void getdata();
    
    int main(void)
    {
    	int n;
    
    	getdata();
    	display(start);
    	n = count(start);
    
    	selection_sort(n);
    	display(start);
    	n = count(start);
    
    	getdata();
    	display(start);
    	n = count(start);
    
    	bubble_sort(n);
    	display(start);
    	n = count(start);
    
    	return 0;
    }
    
    
    void getdata()
    {
    	struct node *newnode;
    	int val;
    	char ch;
    	newnode = NULL;
    
    	do
    	{
    		printf("\nEnter the value");
    		scanf("%d", &val);
    
    		addatend(&newnode, val);
    
    		printf("\nAny more nodes");
    		ch = getchar();
    	}while (ch == 'y' || ch == 'Y');
    	
    	start = newnode;
    	
    }
    
    
    void addatend(struct node **q, int data)
    {	
    
    	struct node *temp;
    	temp = *q;
    
    	if(*q == NULL)
    	{
    		// adding the first node
    		*q = malloc(sizeof(struct node));
    		temp = *q;
    	}
    
    	else
    	{
    		// go to last node
    		while(temp ->link != NULL)
    			temp = temp->link;
    
    		temp->link = malloc(sizeof(struct node));
    		temp = temp->link;
    	}
    
    	temp ->data = data;
    	temp ->link = NULL;
    }
    
    void display (struct node *q){
    
    	int i = 0;
    	struct node *temp;
    
    	temp = q;
    	while (temp != NULL){
    		printf("\nThe data at loc %d is %d", i , temp ->data);
    		temp = temp->link;
    		i++;
    	}
    }
    
    int count (struct node *q){
    
    	struct node *temp;
    	int count = 0;
    
    	temp = q;
    	while (temp != NULL){
    		++count;
    		temp = temp -> link;
    	}
    
    	printf("\nCount %d", count);
    	return count;
    }
    
    void selection_sort(int n)
    {
    	int i,j,temp;
    	struct node *p, *q;
    
    	p = start;
    	for(i = 0;i<n-1;i++)
    	{
    		for(j=i+1;j<n;j++)
    		{
    			if(p->data > q->data)
    			{
    				temp = p->data;
    				p->data = q->data;
    				q->data = temp;
    			}
    			q = q->link;
    		}
    		p= p->link;
    	}
    }
    
    void bubble_sort(int n)
    {
    	int i,j,k, temp;
    	struct node *p,*q;
    
    	for(i = 0;i<n-1;i++)
    	{
    		p = start;
    		q = p->link;
    
    		for(j=1;j<n;j++)
    		{
    			if(p->data > q->data)
    			{
    				temp = p->data;
    				p->data = q->data;
    				q->data = temp;
    			}
    			p= p->link;
    			q= q->link;
    		}
    	}
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The FAQ should answer your question: Cprogramming.com FAQ > Flush the input buffer

    Although I'd probably use fgets() instead.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay i tried but it does not work.

    [insert]
    Code:
    	puts("Flushing input");
    		while((ch = getchar()) != '\n' && ch != EOF);
    	
    		printf("\nAny more nodes");
    		ch = fgets(buf, 1 , stdin);
    	}while (ch == 'y' || ch == 'Y');
    Control passes away from the getdata function to display and count in the main even when i have not finished entering the elements.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using fgets to read one character? That's weird.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    fgets is supposed to read one line. But the thing is that i just want from the user one character y or Y and when i was using
    ch = getchar();

    the post above said it should not be used and use fgets instead :-(

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay i figured out an alternative way of doing this

    Using getche() or getch() instead of getchar().

    Also noted that getchar() is not an interactive function for taking the input from the user as it buffers the input until enter is pressed.

    @zacs7

    Although I'd probably use fgets() instead.
    @zacs7

    Can you please explain of using fgets for taking the input from the user which in this case is a single character? I really did not get your point of using fgets when i am to read a single character.
    Last edited by roaan; 07-14-2009 at 09:09 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fgets doesn't return the first character read. Read your book or "man fgets". (And of course, the tutorial advised you to do fgets in place of all the stuff you had before it -- the scanf and the flushing of input -- not after it.)

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    I have this code to sort a linked list which asks the user to add nodes to the lists till the user presses y or Y. But what is happening currently is that after insertion of one node the program moves ahead without waiting for the input from the user. Is it that i am doing something wrong when asking input from the user.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int data;
    	struct node* link;
    } *start;
    
    
    void getdata()
    {
    	struct node *newnode;
    	int val;
    	char ch;
    	newnode = NULL;
    
    	do
    	{
    		printf("\nEnter the value");
    		scanf("%d", &val);
    
    		addatend(&newnode, val);
    
    		printf("\nAny more nodes");
    		ch = getchar();
    	}while (ch == 'y' || ch == 'Y');
    	
    	start = newnode;
    	
    }
    Instead of getchar() in the colored line just use scanf() with a space after the first double quotes. That will eat up the \n stored in the buffer. This way you dont have to flush the buffer either.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You will get a segfault in your selection_sort. You should initialize q to something, perhaps like what you have done within your bubble_sort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM