what does this function ask

Job *j remove( int fromWhere)
Removes the fromWhere 'th element and return a pointer to it. Note that if fromWhere > length-1 , then nothing is removed and NULL should be returned.


so we delete for example we delete 3rd element and return the pointer to the deleted item?? how we just deleted it so how can we point to it
this is my code so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Job* LinkedJobList::remove( int fromWhere)
{ Job* RemoveJob= new Job;
 JobNode* CurrentNode= new JobNode;
  JobNode* PrevNode= list;
  
  int i=0;
 if (fromWhere> count) 
		{cout<<"error"; 
			return NULL;
						}
 while ( i<fromWhere)
		{PrevNode=list;
			CurrentNode=list->next;
					i++;
					list=list->next;}
 RemoveJob= &(CurrentNode->next->Item);
 if ( CurrentNode != NULL )
   {
      if (PrevNode != NULL)
      {
         PrevNode->next = CurrentNode->next;
      }
      else
      {
         list = CurrentNode->next;
      }
      
delete CurrentNode;}
  

 count--;                         // Decrement the count in the IntSet
  
 return RemoveJob;}
Last edited on
I didn't check the entire code, but I can tell that you are confusing deletion with removal. You are asked to code a removal function, as in "remove the fromWhere'th element from the linked list and return me a pointer to the removed element". You are not asked to DELETE the element from memory. Your task is to create a function that takes out (but doesn't destroy or delete) an element by indicating the position.

I also don't understand why a remove function needs to create a new Job object and a new JobNode object. Nothing is being added. Why do you need these new objects? What are their purpose???? Lines 2 and 3.
Last edited on
thank you so much for the reply. so I should find ith element and return the pointer to it? Im so confused:(
That's right, but you also must remove it from the linked list. Don't delete the object, just take it out of the list and return a pointer to it.

NOTE: If the Job object is embedded in a JobNode object, then I guess you need to delete the JobNode object and then return a pointer to the original Job object. If the JobNode object doesn't point to a Job object and instead it has a Job object inside it, you must copy-construct the contained Job object and then return this copy. But I suppose this is not the case. I suppose JobNode also contains a pointer to a Job object, so no copy-construction is necessary.
thanks a million for your explanations. one more question, once I do remove for example the 2nd element my list starts from 3rd. for example my list is 12,10,14,15,17 and i wanna delete 10 then my new list becomes 14,15,17 not 12,14,15,17..what should i do?
It is a linked list. A linked list must work like a rope: If a piece in the middle of the rope is cut out, the new ends of the rope pieces must be tied together so the rope is built again, but without the piece that needed removal.

So what are you doing right now? You are probably just discarding the first rope piece and just leaving the second piece.

A linked list is super fast in tying the new ends of the rope, though: The .next pointer (or whatever name you gave it) of the JobNode object pointing to the JobNode object being removed is redirected to point to the same JobNode object that the .next pointer of the JobNode object being removed. This effectively ties up the rope ends.

If you are removing the second element, then you make the first element's .next pointer point to the third element. And what's the third element? Simple: It is the element pointed to by the .next pointer of the second element (the element being removed).

To tell it in words is complex, but in reality is quite a simple process. I bet Wikipedia even has a code snipped on linked lists.
If you want to delete the node we have over here .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Job* LinkedJobList::remove( int fromWhere)
{
	Job* RemoveJob= new Job;
   JobNode* CurrentNode= new JobNode;
  JobNode* PrevNode= list;
  
 int i=0;
 if (fromWhere> count) 
{
	 cout<<"error"; 
		return NULL;
	}
	CurrentNode=list;
 while ( i < fromWhere)
		{
			PrevNode=list;
			CurrentNode=CurrentNode->next;			
			list=list->next;
			i++;
		}
 RemoveJob= CurrentNode;
 if ( CurrentNode != NULL )
   {
      if (PrevNode != NULL)
      {
         PrevNode->next = CurrentNode->next;
      }
      else
      {
         list = CurrentNode->next;
      }
      
delete RemoveJob;
}
  

 count--;                         // Decrement the count in the IntSet
  
 return PrevNode;}
And I really hope this is not the indentation "standard" you use for your code.
Topic archived. No new replies allowed.