#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Linked List Question


    I am writing a programming that will input the score of mid semester students.
    - print a valid score range of 0 to 50 and print invalid scores, flagged as errors and delete from main linked list.

    I have done up to input score in to a single linked list! now i want to know is how to print valid score range of 0 to 50 and print invalid range, flagged as error and delete from main linked list (Moving out of range Numbers into invalid range List)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Please show your program and test cases---the input and expected output, and your specific question, and all the other pertinent details such as that you're actually using COBOL not C, or whatever these details happen to be.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
      int num;
      struct node *next;
    };
    
    void create(struct node **);
    void display (struct node *p);
    
    int main()
    {
      struct node *p = NULL;
    
      create(&p);
    
      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 -> num = 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");    
    }
    this what i have done so far. i want to know is how to print valid score range of 0 to 50 and print invalid range, flagged as error and delete from main linked list

    thanks :)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Your program successfully creates a queue.
    You need to know that the way your input is structured the scanf("%c", &ch) reads newlines. Thus, entering 1 through 4 then quitting looks like this:

    Enter Score:1
    Enter Score:2
    Enter Score:3
    Enter Score:4q

    I'd instead test the return value from scanf("%d", &the_int) and break if it isn't 1. If that were unsatisfactory I'd try another approach. Other contributors at this forum wouldn't have to guess though.



    REMOVING NODE b.
    Pictures help.

    Suppose the queue is chained as
    head -> a -> b -> c -> NULL

    after removing node b the queue looks like
    head -> a -> c -> NULL

    Is (a->next = a->next->next) all you need to do?
    No, because in the singly linked list you'd lose track of b

    You'll need to maintain an extra variable that stores b so you can deallocate its memory.

    And is that enough to know? No. Because as you scan the queue looking for the data you want to remove once you find the node to remove you'll have lost track of parent unless you use an extra variable to store the predecessor in the queue.

    Is that enough? No. What about the end cases. Suppose you need to remove the head of the queue. That means you'll have to change the head of the queue back in the main program, which means you'll need to either have a fixed head of queue that doesn't store information (a good approach since you might have multiple variables that store the same queue and it's possibly difficult to synchronize more than one of them) or the delete_node function will take a **struct node parameter to cause the change or return a struct node* as the potentially new head of queue.

    Suppose the item to remove comes right before the NULL end of the queue? I think that case is not troublesome.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    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
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    (For everyone else), some history
    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

IMN logo majestic logo threadwatch logo seochat tools logo