Thread: Linked List

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    30

    Smile Linked List

    Hey, I am having problem running this simple link list program on mingw compiler from Codelite.I am not getting any output. Can any body help...
    Here is the <code>:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct list_el
    {
    	int val;
    	struct list_el *next;
    };
    
    typedef struct list_el item;
    
    int main(int argc, char **argv)
    {
    
    	item *curr, *head;
    	int i;
    	
    	 head =NULL;
    	 
    	 for(i=1; i<=5; i++)
    	 {
    		 
    		 curr = (item*)malloc(sizeof(item));
    		 curr->val = i;
    		 
    		 curr->next = head;
    		 curr = head;
    	 }
    	  curr = head;
    	  
    	  
    	 while(curr)
    	  {
    		  
    		  printf("\n %d",curr->val);
    		  curr = curr->next;
    	  }
    	
    	return 0;
    }

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct list_el
    {
    	int val;
    	struct list_el *next;
    };
    
    typedef struct list_el item;
    
    int main(int argc, char **argv)
    {
    	item *curr, *head;
    	int i;
    	head = (item*)malloc(sizeof(item));
    	curr = head;
    	for(i=1;i <= 5;i++)
    	{
    		curr->next = (item*)malloc(sizeof(item));
    		curr->val = i;
    		curr = curr->next;
    	}
    	curr = head;
    	while(i--)
    	{
    		printf("\n %d",curr->val);
    		curr = curr->next;
    	}
    	return 0;
    }
    It'd be harder to explain why what you have is wrong. Compare yours to this and ask on anything you don't understand.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    74
    in the for, the last statement, it should be head = curr;

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    Thanks for the complete code. It worked for me!!
    So does it mean that linked list elements can be accessed from the beginning only and not from the end??

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can access any specific node onward. You can't go backwards, unless you have a double linked list. Or, the only other way to access every node in the list is to make your list circular, so that the last node points to the first node. Then, given any node, you can eventually get to all the others.


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

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    I am trying to construct an linked list of 5 members in the following snippet but the snipet just prints two elements. Can anybody point out where am I going wrong:
    Code:
    int main(int argc, char **argv)
    {
    	struct node *head, *curr, *temp;
    	int i;
    	head = (struct node*)malloc(sizeof(struct node));
    	
    	curr = head;
    	
    	for(i=1;i<=5;i++)
    	{
    	
    	curr -> next = (struct node*)malloc(sizeof(struct node));
    	curr -> x= i;
    	temp=curr;
    	curr = curr->next;
    	}
    	temp->next =0;
    	
    	
    	
    	//Display members
    	curr = head;
    
    	
    	if(curr->next!=0)
    	{
    		printf("\n %d", curr->x);
    		curr = curr->next;
    	}
    	printf("\n %d", curr->x); // to print the last element in the list
    			 
    		return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    if(curr->next!=0) should be a while.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    you are not doing any looping when you are printing the list.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the current node's next member is not NULL, then print its x value. That is what your logic says. Now you can fix it on your own.
    But there are more problems. First off, 0 is not a null pointer. NULL is. I am assuming you're getting a warning from that.
    Your loop is dubious. After 5 iterations, curr points to where? And tmp points to where? You're leaking memory.
    Finally, you are not freeing what you malloced.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    > First off, 0 is not a null pointer. NULL is. I am assuming you're getting a warning from that.
    LOL probably because he think 'macros are evil!'
    Question 5.2
    Question 5.5
    Last edited by Bayint Naung; 07-24-2010 at 08:46 PM.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Are you trying to insert at head or tail of the list??

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    Thank you for the help. I replaced 0 with NULL and if with while and it now prints all the 5 members in the linked list.
    After completing the 5 iterations, the temp points to NULL but where how should curr points so as to avoid memory leaks ?

    Also have I correctly freed the memory in the following snippet?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int x;
    	node *next;
    };
    int main(int argc, char **argv)
    {
    	struct node *head, *curr, *temp;
    	int i;
    	head = (struct node*)malloc(sizeof(struct node));
    	
    	curr = head;
    	
    	for(i=1;i<=5;i++)
    	{
    	
    	curr -> next = (struct node*)malloc(sizeof(struct node));
    	curr -> x= i;
    	temp=curr;
    	curr = curr->next;
    	}
    	temp->next =NULL;
    	
    	
    	
    	//Display members
    	curr = head;
    
    	
    	while(curr->next!=NULL)
    	{
    		printf("\n %d", curr->x);
    		curr = curr->next;
    	}
    	printf("\n %d", curr->x); // to print the last element in the list
    	
    	free(curr->next);
    			 
    		return 0;
    }

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    this might be what you want in the very begining

    this might be what you want in the very begining
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct list_el
    {
    	int val;
    	struct list_el *next;
    };
    
    typedef struct list_el item;
    
    int main(int argc, char **argv)
    {
    
    	item *curr, *head;
    	int i;
    	
    	 head =NULL;
    	 
    	 for(i=1; i<=5; i++)
    	 {
    		 
    		 curr = (item*)malloc(sizeof(item));
    		 curr->val = i;
    		 
    		 curr->next = head;
    		 head = curr;
    		
    	 }
    	  head = curr;
    	  
    	  
    	 while(curr)
    	  {
    		  
    		  printf("\n %d",curr->val);
    		  curr = curr->next;
    	  }
    	
    	return 0;
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bayint Naung View Post
    > First off, 0 is not a null pointer. NULL is. I am assuming you're getting a warning from that.
    LOL probably because he think 'macros are evil!'
    Question 5.2
    Question 5.5
    OMG!
    That I think macros are evil has nothing to do with this.
    0 is an integer. An integer is not a pointer. In C, there is a very elegant solution. That is often to cast the integer to void*, because void* will implicitly convert to any other pointer type, but not to an integer.

    Quote Originally Posted by OCcounty View Post
    Thank you for the help. I replaced 0 with NULL and if with while and it now prints all the 5 members in the linked list.
    After completing the 5 iterations, the temp points to NULL but where how should curr points so as to avoid memory leaks ?

    Also have I correctly freed the memory in the following snippet?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int x;
    	node *next;
    };
    int main(int argc, char **argv)
    {
    	struct node *head, *curr, *temp;
    	int i;
    	head = (struct node*)malloc(sizeof(struct node));
    	
    	curr = head;
    	
    	for(i=1;i<=5;i++)
    	{
    	
    	curr -> next = (struct node*)malloc(sizeof(struct node));
    	curr -> x= i;
    	temp=curr;
    	curr = curr->next;
    	}
    	temp->next =NULL;
    	
    	
    	
    	//Display members
    	curr = head;
    
    	
    	while(curr->next!=NULL)
    	{
    		printf("\n %d", curr->x);
    		curr = curr->next;
    	}
    	printf("\n %d", curr->x); // to print the last element in the list
    	
    	free(curr->next);
    			 
    		return 0;
    }
    OK, brainstorming time.
    Consider the loop. When i = 5, what does cur->next point to?
    After that, what does temp point to?
    After that, what does cur point to?
    Finally, after the loop, what does temp point to?
    Can you spot it?
    Also, you're not freeing memory correctly. For every malloc, there must be a free, in reverse order. Since you have 5 mallocs and 1 free, one can assume that you're not freeing memory correctly.

    Quote Originally Posted by tAo0 View Post
    this might be what you want in the very begining
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct list_el
    {
    	int val;
    	struct list_el *next;
    };
    
    typedef struct list_el item;
    
    int main(int argc, char **argv)
    {
    
    	item *curr, *head;
    	int i;
    	
    	 head =NULL;
    	 
    	 for(i=1; i<=5; i++)
    	 {
    		 
    		 curr = (item*)malloc(sizeof(item));
    		 curr->val = i;
    		 
    		 curr->next = head;
    		 head = curr;
    		
    	 }
    	  head = curr;
    	  
    	  
    	 while(curr)
    	  {
    		  
    		  printf("\n %d",curr->val);
    		  curr = curr->next;
    	  }
    	
    	return 0;
    }
    This is even more wrong. Brainstorming time.
    On the first iteration in the loop, what does head point to?
    After that, where will cur->next point to?
    After that, where does head point?
    On the next iteration, where does head point?
    And where will cur->next point?
    Finally, head should be the first node in the list, not the last. And you are also leaking memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    wondering

    The code I posted before was a fixed version from #1 post.
    At least, the result is printing
    5
    4
    3
    2
    1
    in the command line window.
    The way of inserting node was not great.
    The order of the linked list would be reversed from the inputting order.
    But..I mean it still a kind of way to create a linked list. It might be called "insertation at head "or sth .etc.

    by the way, I do not understand leaking memory problem. I know I have not free the list I just created(I don't need this time). what else?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread