GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Sep-2004, 01:28
Kay Chan Kay Chan is offline
New Member
 
Join Date: Sep 2004
Posts: 15
Kay Chan is on a distinguished road

Insert problem in Linked list


I have a problem in inserting items in linked list. Hence, there are different way to insert the items to the list. The first one is loading data form a txt file(1). And the second one is input from keyboard. I have done the first part. I have got a question in second part. Do I need to continue the insertion by using the (1) linked list and its position ? If yes, how to return the ptr and list position ?

Can I do sth like this ? If yes, what type I should use. If no, how can I insert other new items in the same list by using keyboard ?

type ReturnPtr( List * l, Position p){

return l, p;
}
  #2  
Old 03-Sep-2004, 17:06
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
Originally Posted by Kay Chan
I have a problem in inserting items in linked list. Hence, there are different way to insert the items to the list. The first one is loading data form a txt file(1). And the second one is input from keyboard. I have done the first part. I have got a question in second part. Do I need to continue the insertion by using the (1) linked list and its position ? If yes, how to return the ptr and list position ?

Can I do sth like this ? If yes, what type I should use. If no, how can I insert other new items in the same list by using keyboard ?

type ReturnPtr( List * l, Position p){

return l, p;
}

Hi,

I wrote similar function a few days back.Parden me if its not what you are actually looking for.I am no expert in C.never the less the function works for me.

I am assuming you already have some data in your linked list cause thats what my functions assume too :-).I have a check before calling the actual function to see if list contains data.I.e.Head Pointer is not NULL.

So here is my function

CPP / C++ / C Code:

/*My node looks like this.Pretty simple huh??*/
struct node
{
	char *data;
	struct node *next;
};

/*Initialize head record before main()*/
struct node *first_rec=NULL;

/*I am passing to arguments while calling my function.
Arg word holds my actual word i want to insert and arg where
hold the record before which I want to insert my word*/

int insert_record(char *word,char *where)
{
	struct node *cur_ptr;    //to hold the current record to work on
	struct node *new_rec;  //create a new node to insert
	struct node *prev;       //hold the record before inserted record to modify its next pointer
	
	int result;
	/*Allocate memory for new record to be inserted*/
	new_rec=(struct node *)malloc(sizeof(struct node));
	/*Allocated the memory for word in new record*/
            new_rec->data = (char *)malloc( (strlen(word) + 1) * sizeof(char));
	
             /*Save the data in the data portion of the node*/
	strcpy(new_rec->data,word);

	/*load the first or head record in current pointer for processing*/
             cur_ptr=first_rec;

	/*Loop till we hit the end of the list (i.e NULL pointer)*/
            while (cur_ptr!=NULL)
	{
		/*Check if the current data matches the where data if yes then you need to insert before this record*/
                          if ((strcmp(cur_ptr->data,where))== 0)
		{
			if (cur_ptr==first_rec)
					{
						/*Here we are Inserting before 1st record which needs updating our head pointer*/
						first_rec=new_rec;
						first_rec->next=cur_ptr;
						result=1;
						return result;
					}
					/*Here we are Inserting before current record which is regular processing*/
					prev->next=new_rec;
					new_rec->next=cur_ptr;
					result=1;
					return result;
					
		}
		prev=cur_ptr; //If no hit to where data in the loop save the current node ptr in node "prev" we will need it in next interation
		cur_ptr=cur_ptr->next; //Increment the cur_ptr to point to next node in the list.
		continue;
	}
	if (cur_ptr==NULL)
	{
		result=2;
		return result;
	}
}

 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
Question about linked list and queue Kay Chan C++ Forum 7 02-Sep-2004 09:27
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 09:32
[include] list1.h -- Linked list class dsmith C Programming Language 2 04-May-2004 09:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:09.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.