LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2012, 11:54 PM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
Link list


Below is a simple example of single link list I have one question , what does this statement means
q=q->link;

Code:
 struct node
{
	int info;
	struct node *link;
}*start;


int create(int data)
{
	struct node *q,*tmp;
	tmp=malloc(sizeof(struct node));
	tmp->info=data;
	tmp->link=NULL;
	
	if(start==NULL) 
		start=tmp;  
	else
	{	
		
		q=start;    
		while(q->link!=NULL)
		{
			q=q->link;
		}
		
			q->link=tmp;		
	}
	return(1);
}
Thanks in advance
 
Old 04-13-2012, 02:03 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
The struct q has two members, q.info is an int and q.link is a pointer to another struct (type of node)
q->link is the struct itself pointed by the member link.
q = q->link means give q a new value, the node pointed by the member link.

this is a pretty simple linked list, all elements has a link to the next one.

the create function will look for the last element in the list and add a new one to it.
 
Old 04-13-2012, 02:23 AM   #3
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Original Poster
Rep: Reputation: 15
Thanks for the reply .I have one more doubt how this statement q=q->link is differ from
q->link=tmp
So let say I have 3 numbers
1
2
3
first for 1 it will go to
Code:
 if(start==NULL) 
		start=tmp;// This means it will store the value of tmp to start i.e. 1
then for 2 it will go to else part

Code:
 else	
	     {	
		q=start;  //Here it will store value start to q   
		while(q->link!=NULL) //Since q is not not null it not go into q=q->link 
		    {
			q=q->link;
		    }
		       q->link=tmp; //Here I came to know we are storing value 2 ,what this statement means		
	     }
 
Old 04-13-2012, 02:52 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
int create(int data)
{
	struct node *q,*tmp;                    # this line defines the variables q and tmp as pointers to struct node
	tmp=malloc(sizeof(struct node));        # allocate memory for a struct (create new item)
	tmp->info=data;                         # fill up the struct, info = data
	tmp->link=NULL;                         #                     link = null (no next item)
	
	if(start==NULL)                         # start = null, list is empty
		start=tmp;                      #       tmp is the first, so store this as start of the list
	else
	{	
		
		q=start;                        # start was not null, try to find the last element of the list
		while(q->link!=NULL)            # while we have a next element
		{
			q=q->link;              # take it
		}
		
			q->link=tmp;		# we found the last (link is null), so we can store the new element (tmp) now
	}
	return(1);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make link list and add and delete list node in shared memory golden_boy615 Programming 1 12-18-2011 06:52 AM
Binary file: Does this look like a link list? josenj Programming 1 01-29-2010 01:48 AM
help with Link List new2lunix Programming 10 01-09-2009 05:14 PM
What is my ignore list, and should there be a simple link to it? billymayday LQ Suggestions & Feedback 5 04-27-2008 02:40 AM
Freeing a Doubly link list !!!! morph_ind Programming 2 04-01-2006 07:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration