Thread: Linked List Question

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    16

    Smile Linked List Question

    In the following code, it required for users to enter scores between 0 -50 which should print valid link list and if values beyond the range are entered, should be flagged as invalid and print invalid list.
    Example:
    Linked-List of valid scores 0->30->27->43-> END OF VALID LIST
    Linked-List of invalid scores 0->55->65->66->END OF ERROR LIST

    Here is My Code:
    Help Me To Implant this
    Code:
    #include <Stdio.h>#include <stdlib.h>
    #include <malloc.h>
    
    
    void main()
    {
    	struct node
    	{
    		int num;
    		struct node *p;
    	};
    	typedef struct node NODE;
    	
    	NODE *head, *first, *temp = 0;
    	int count = 0;
    	int choice = 1;
    	first = 0;
    	
    	while(choice)
    	{
    		head = (NODE *)malloc(sizeof(NODE));
    		printf("Enter the data item\n");
    		scanf("%d", &head-> num);
    		if(first !=0)
    		{
    			temp -> p = head;
    			temp = head;
    		}
    		else
    		{
    			first = temp = head;
    		}
    		fflush(stdin);
    		printf("Do You want to continue (Type 0 or 1)?\n");
    		scanf("%d", &choice);
    	}
    	temp->p=0;
    	// reset temp to the begining 
    	temp = first;
    	printf("\n status of the linked list is\n");
    	while(temp !=0)
    	{
    		printf("%d=>", temp->num);
    		count++;
    		temp = temp -> p;
    	}
    	printf("NULL\n");
    	printf("No.Of Nodes in the list = %d \n", count);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include <Stdio.h>
    It is usual to write filenames in lowercase, even when your local file system seems to ignore case.

    > #include <malloc.h>
    malloc is in stdlib.h, which you already include (unless you're using some really old fossil compiler).

    > void main()
    SourceForge.net: Void main - cpwiki

    > struct node *p;
    The usual convention is to call the link 'next';

    > NODE *head, *first, *temp = 0;
    head and first pretty much mean the same thing.
    temp (within the context of the first loop) should be called tail.

    > head = (NODE *)malloc(sizeof(NODE));
    FAQ > Casting malloc - Cprogramming.com

    > fflush(stdin);
    SourceForge.net: Fflush - cpwiki

    Now, as for your question, what is hard about
    if ( temp->num < 0 || temp->num > 50 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    //Linked List data structure  declaration
    struct node
    {
    	int num;
    	struct node *next;
    };
    
    
    // Function Prototype declaration
    void create(struct node **);
    void display (struct node *);
    
    
    
    
    //Main Program
    int main()
    {
    	struct node *p = NULL;
    	
    	create(&p);
    	display(p);
    	
    	return 0;
    }
    // Function to Insert 
    void create(struct node **head)
    {
    	int c,ch;
    	struct node *temp, *rear;
    	
    	do
    	{
    		printf("Enter Number:");
    		scanf("%d", &c);
    		temp = (struct node *)malloc(sizeof(struct node));
    		temp -> num = c;
    		temp -> next = NULL;
    		
    			if (*head == NULL)
    			{
    				*head = temp;
    			}
    			else 
    			{
    				rear->next = temp;
    			}
    		
    		rear = temp;
    		printf("Do you wish to continue [1/0]:");
    		scanf("%d", &ch);
    	}while(ch !=0);
    	printf("\n");	
    	}
    	
    	
    
    
    void display(struct node *p)
    {
    
    
    if (p->num <0 || p -> num > 50)
    	{
    		printf("Linked-List of Valid Scores %d =>", p ->num);
    		printf("end of VALID list \n");
    	}
    	else{
    		printf("Linked-List of invalid Scores %d =>",p->num);
    		printf("end of ERROR-list \n");
    	}
    }

    Can you help me to solve this! display function is not working

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I don't know how your display function is supposed to work so I can't tell you why it's not working, it looks like it works fine to me.

    One hint though. Your code should add the first node to the list fine, but nodes after that won't be added. Step through your code line by line and you should be able to see why.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Quote Originally Posted by DeadPlanet View Post
    I don't know how your display function is supposed to work so I can't tell you why it's not working, it looks like it works fine to me.

    One hint though. Your code should add the first node to the list fine, but nodes after that won't be added. Step through your code line by line and you should be able to see why.
    In the following code, it required for users to enter scores between 0 -50 which should print valid link list and if values beyond the range are entered, should be flagged as invalid and print invalid list.
    I WANT OUTPUT TO BE LIKE THIS:
    Linked-List of valid scores 0->30->27->43-> END OF VALID LIST
    Linked-List of invalid scores 0->55->65->66->END OF ERROR LIST

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You had a working display() function in your original post.

    All you needed to add to it was a test to record whether (or not) you've seen an out of range value.

    When you're done printing, you print the appropriate message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Quote Originally Posted by Salem View Post
    You had a working display() function in your original post.

    All you needed to add to it was a test to record whether (or not) you've seen an out of range value.

    When you're done printing, you print the appropriate message.
    In my first post coding linked list print as the order it is entered! i want to print like this. 0 to 50 scores as valid and rest as invalid :|

    Linked-List of valid scores 0->30->27->43-> END OF VALID LIST
    Linked-List of invalid scores 0->55->65->66->END OF ERROR LIST

    Code:
    
    
    Code:
    oid display(struct node *p) { if (p->num <0 || p -> num > 50) { printf("Linked-List of Valid Scores %d =>", p ->num); printf("end of VALID list \n"); } else{ printf("Linked-List of invalid Scores %d =>",p->num); printf("end of ERROR-list \n"); } }

    this part is not working i am new to programming language couldn't figure out

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    //Linked List data structure  declaration
    struct node
    {
    	int num;
    	struct node *next;
    };
    
    
    // Function Prototype declaration
    void create(struct node **);
    void display_valid(struct node *, const char * );
    void display_invalid (struct node *, const char * );
    
    
    //Main Program
    int main()
    {
    	struct node *p = NULL;
    	
    	create(&p);
    	display_valid(p, "Linked-List of valid Scores => %d");
    	display_invalid(p, "Linked-List of invalid Scores => %d");
    
    
    	return 0;
    }
    // Function to Insert 
    void create(struct node **head)
    {
    	int c,ch;
    	struct node *temp, *rear;
    	
    	do
    	{
    		printf("Enter Number:");
    		scanf("%d", &c);
    		temp = (struct node *)malloc(sizeof(struct node));
    		temp -> num = c;
    		temp -> next = NULL;
    		
    			if (*head == NULL)
    			{
    				*head = temp;
    			}
    			else 
    			{
    				rear->next = temp;
    			}
    		
    			rear = temp;
    			printf("Do you wish to continue [1/0]:");
    			scanf("%d", &ch);
    			
    			}while(ch !=0);
    			
    		printf("\n");	
    	}
    
    
    // Function to Display
    void display_valid(struct node *p, const char * str)
    {
    	if(p -> num >= 0 && p -> num <= 50)
    	{
    		printf(str, p->num);
    		
    		if ( p-> next)
    		{
    			display_valid(p->next, "%d");
    		}
    		else
    		{
    			if (p->next)
    			{
    				display_valid(p->next, str);
    			}
    			else
    			{
    				fputs("\n", stdout);
    			}
    		}
    
    
    }
    
    
    void display_invalid (struct node *p, const char * str)
    {
    	if(p -> num<0 || p -> num > 50)
    	{
    		printf(str, p -> num);
    		
    		if (p -> next)
    		{
    			display_invalid(p->next, "%d");
    		}
    		else
    		{
    			fputs("\n",stdout);
    		}
    	}
    	else
    	{
    		if (p -> next)
    		{
    			display_invalid(p->next, str);
    		}
    		else
    		{
    			fputs("\n", stdout);
    		}
    	}
    }
    can someone help me to fix this program?
    In the following code, it required for users to enter scores between 0 -50 which should print valid link list and if values beyond the range are entered, should be flagged as invalid and print invalid list.
    I WANT OUTPUT TO BE LIKE THIS:
    Linked-List of valid scores 0->30->27->43-> END OF VALID LIST
    Linked-List of invalid scores 0->55->65->66->END OF ERROR LIST

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is what you had originally.
    Code:
    void display ( node *p ) {
      while ( p != NULL ) {
        printf("%d->", p->data);
        p = p->next;
      }
    }
    Now try this
    Code:
    void display ( node *p ) {
      while ( p != NULL ) {
        if ( p->data < 0 || p->data > 50 ) fprintf(stderr,"OUT OF RANGE\n");
        printf("%d->", p->data);
        p = p->next;
      }
    }
    Now, instead of printing something every time, set a flag and print it once at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Another fix, both head and rear should be in main and passed to the create function ... (struct node **head, struct node **rear) ...

  11. #11
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Quote Originally Posted by Salem View Post
    This is what you had originally.
    Code:
    void display ( node *p ) {
      while ( p != NULL ) {
        printf("%d->", p->data);
        p = p->next;
      }
    }
    Now try this
    Code:
    void display ( node *p ) {
      while ( p != NULL ) {
        if ( p->data < 0 || p->data > 50 ) fprintf(stderr,"OUT OF RANGE\n");
        printf("%d->", p->data);
        p = p->next;
      }
    }
    Now, instead of printing something every time, set a flag and print it once at the end.
    i didnt get what you mean by "
    Now, instead of printing something every time, set a flag and print it once at the end."


    i hate linked list -_-

    http://i.imgur.com/dtodncW.png

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm curious as to how you managed to create a functional linked list all by yourself, and yet make such a mess over something as trivial as setting a flag and testing it.

    A flag is simply a boolean variable, it has two states.

    Now figure out a boolean expression that records whether (or not) you've ever seen an out of range value whilst printing your array.

    If you don't exclaim "OMG, why didn't I see that before" when you finally get it, then programming isn't for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Sep 2013
    Posts
    16
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct node 
      int no;
      struct node *next;
    };
    
    
    struct node *Valid_List; 
    struct node *Invalid_List;
    
    
    void create(struct node **);
    
    
    int main()
    {
      struct node *p = NULL;
      printf("'q' or 'Q' to end the mark entry!\n");
      create(&p);
    
    
    if ((p->no < 0) || (p->no > 50))
             {
                Valid_List = p;    
                p = Valid_List -> next;
                printf("%d->", Valid_List-> no);
            }
             else
             {
                Invalid_List = p;
                p = Invalid_List -> next;
                printf("%d->", Invalid_List-> no);
            }
            
      return 0;
    }
    
    
    void create(struct node **head)
    {
      int c;
      char ch;
      struct node *temp, *rear;
    
    
      do
      {
    
    
        printf("Enter Score:");
        scanf("%d", &c);
        temp = (struct node *)malloc(sizeof(struct node));
        temp -> no = c;
        temp -> next = NULL;
    
    
        if (*head == NULL)
        {
      *head = temp;
        }
        else
        {
      rear->next = temp;
        }
    
    
        rear = temp;
        scanf("%c", &ch);
    
    
        } while(ch !='q' && ch !='Q');
    
    
      printf("\n");    
    }

    i want to print as two linked list! so later i can calculate the average lowest highest of valid list range of 0 to 50.
    after user input i want to print the valid and invalid linked list by if else condition. i have tried to do it check it in main function.
    In header i have declared two pointer for pointing valid and invalid list. Struct node *Invalid_List , Struct node *Valid_List.
    I am just a beginner! :/
    How to fix this? Helps will be really appreciated
    Last edited by Aliano; 10-31-2013 at 04:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c linked list question.
    By bos1234 in forum C Programming
    Replies: 2
    Last Post: 03-21-2011, 10:48 AM
  2. linked list question
    By sumdude in forum C Programming
    Replies: 5
    Last Post: 08-10-2009, 08:38 PM
  3. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  4. linked list question..
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 10-26-2008, 12:40 PM
  5. I have a question about linked list
    By Tzahi in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 03:15 PM