linked list noob - function create new list

This is a discussion on linked list noob - function create new list within the C Programming forums, part of the General Programming Boards category; Well i have list with names and year of birth. Now I want to pass that list into function and ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    7

    linked list noob - function create new list

    Well i have list with names and year of birth. Now I want to pass that list into function and year (for example 1982) so function should return newly formed list containing only names with specified year. This function must create new list inside function. So this is problem for me. How to create new "start" head inside function? how to fill it with data and display them?I tried but no luck. Note, it must be single linked list.

    Code:
    struct student
    {
           char name[20];
           int year;
          struct student *next;
    };
    
    struct student *head = NULL;
    
    struct student *selection(struct student *head,int year )
    {
    
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    There are a few ways of doing it. One way is to leave the first node empty, because it simplifies the addition and deletion.

    One problem you may have with your current design is that you're accepting an element called head in the function selection() and the actual head of the linked list is also called head. This means you can't use the actual head of the linked list inside the function. If you don't need to access it, then you're fine.

    From the website tutorials:

    http://www.cprogramming.com/tutorial/c/lesson15.html

    Start slow. The concept of adding a new node to a linked list is just as simple as inserting it into the next available space. So until it finds a NULL element in the list, it keeps looping until it reaches the end. The exception to this is if the first element is NULL, in which case you have an empty linked list. Add a new element at the head.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    7
    This came to my mind but I can't work it out.
    Code:
    struct student
    {
           char name[20];
           int year;
           struct student *next;
    };
    struct student *selection(struct student *head,int year)
    {
    	struct student *temp;
    	struct student *head1=NULL;
    	
    
    	temp = (struct student *)malloc(sizeof(*temp));
    	
    	temp = head;
    
    	while (temp != NULL)
    	{
    		if (temp->year == year)
    		{
    			
    			temp->next=head1;
    			head1=temp;
    			temp=temp->next;
    		}
    	}
    
    	
    	temp=head1;
    
    	if (head1 == NULL)
    	{
    		printf("List is empty");
    		return;
    	}
    	else
    	{
    	
    	while (temp != NULL)
    	{
    		printf("\n");
    		printf("%s %d",temp->name,temp->year);
    		printf("\n");
    		temp=temp->next;
    	}
    	}
    	return;
    
    }
    Last edited by dukysta; 07-06-2007 at 08:01 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    temp = (struct student *)malloc(sizeof(*temp));
    new = (struct student *)malloc(sizeof(*new));
    Why are you making two new elements just to add one? There's no reason to do that. After you do this, then you're just playing around with what pointer points where without actually doing anything.

    In addition....

    Code:
    struct student *next;
    struct student *next1;
    This is a singly-linked list you said, so you don't need two pointers, much less two pointers to point forward.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    7
    Yes, I don't need them but I was trying to create another copy so it is separately pointing to new head. That's why *next1. More memory it takes but an idea. And as I said I am linked list noob. Now code is in original state.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You don't need to make any copies of nodes. For one, unless you free() the temporary node, you're causing a memory leak.

    I still have no idea what next1 is for, and again, it's not needed.

    Check this out: http://en.wikipedia.org/wiki/Linked_List

    It has some code written in C, in addition to an extremely lengthy amount of information on linked lists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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