Does anyone tutor for free?

Hi, I am a college student taking C++ classes. I have some problems writing functions such as insert_head, insert_before, insert_after, where_this_goes, sorted_list, copy/clear, remove_node, search_node, and print_list with linked list. I hope that someone could help me out. Thank you.
i don't think anyone gets paid to answer queries here, just post your question, show some effort and highlight where exactly you're getting stuck (and use code-tags!)
also re insert_head etc you might also wish to search this forum (and other on-line), these are oft covered topics
This forum is full of people who are willing to help. Give your problems a try and ask about them here if you need help. This is a relatively beginner-friendly forum, as long as you put in some effort. We won't just do assignments for you, unless you get lucky.

The usual advice:
mbozzi wrote:
I strongly suggest that you ask for help publicly. This helps ensure the quality of the information you're getting (since it's usually examined by multiple people), and it makes the solution to your problem and the related discussion available to others who have the same issue.

If the offer is made, do not pay for help.


Edit: oops @gunnerfunner -- I didn't see your post.
Last edited on
no worries max, the more people reiterate the message the better not just for the OP but for anybody else who might be browsing ...
This is what I have so far. I leave some functions in blank because I don't know how to do it. I understand the purpose of those functions, but I'm weak at the language syntax.

#ifndef LINKED_LIST_FUNCTIONS_H
#define LINKED_LIST_FUNCTIONS_H
#include <Node.h>
#include <iostream>
#include <cassert>

using namespace std;
Node* _insert_head(Node& head_ptr, int the_number);
Node* _insert_before(Node* head_ptr, Node* beforeMe, int number);
Node* _insert_after(Node* head_ptr, Node* afterMe_ptr, int number);
Node* search(Node& head_ptr, int target);
Node* where_this_goes(Node* head_ptr);
Node* sorted_list();
Node* _previous(Node* head_ptr, const Node* target_ptr);
Node* remove_node(Node* head)
Node* delete_node();
ostream& _print(ostream& outs, const Node* head_ptr);

Node* _insert_head(Node* head_ptr, int number)
{
Node* temp_ptr = new Node(number);
// temp_ptr->item = number;
temp_ptr->next = head_ptr;
head_ptr = temp_ptr;
return temp_ptr;
}

Node* _insert_before(Node* head_ptr, Node* beforeMe_ptr, int number)
{
if(head_ptr == NULL)
{
_insert_head(head_ptr, number);
}
else if(beforeMe_ptr == head_ptr)
{
_insert_head(head_ptr, number);
}
else
{
Node* temp_ptr = new Node(number);
//temp_ptr->item = number;
//temp_ptr->next = NULL;
Node* walker_ptr = head_ptr;
temp_ptr->next = beforeMe_ptr;
while(walker_ptr->next != beforeMe_ptr)
walker_ptr = walker_ptr->next;
walker_ptr->next = temp_ptr;
return temp_ptr;
}
}

Node* _insert_after(Node* head_ptr, Node* afterMe_ptr, int number)
{
Node* temp_ptr = new Node(number);
temp_ptr->item = number;
temp_ptr->next = afterMe_ptr->next;
afterMe_ptr->next = temp_ptr;
return temp_ptr;
}

Node* search(Node* head_ptr, int target)
{
Node* walker_ptr = head_ptr;
while(walker_ptr)
{
if(walker_ptr->item == target)
{
return walker_ptr;
}
else if(walker_ptr->next == NULL)
{
walker_ptr = NULL;
return walker_ptr;
}
walker_ptr = walker_ptr->next;
}
}


Node* where_this_goes(Node* head_ptr)
{


}

Node* sorted_list()
{

}

Node* _previous(Node* head_ptr, const Node* target_ptr)
{
assert(target_ptr != NULL);
if(head_ptr == NULL)
{
return NULL;
}
else if(head_ptr == target_ptr)
{
return NULL;
}
const Node* walker_ptr = head_ptr;
while(walker_ptr != NULL)
{
if(walker_ptr == target_ptr)
{
return walker_ptr;
walker_ptr = walker_ptr->next;
}
}
return walker_ptr;
}

Node* remove_node(Node* head)
{

}

Node* delete_node()
{

}

void printList(Node* head_ptr)
{
Node* walker_ptr = head_ptr;
while(walker_ptr != NULL)
{

cout << walker_ptr->item << endl;
walker_ptr = walker_ptr->next;
}
}

ostream& _print(ostream& outs, const Node* head_ptr)
{
const Node* walker_ptr = head_ptr;
outs << "H-ptr->";
while(walker_ptr)
{
outs << "[" << walker_ptr->item << "]->";
walker_ptr = walker_ptr->next;
}
cout << "|||";
}

#endif // LINKED_LIST_FUNCTIONS_H

1
2
3
4
5
6
7
8
Node* _insert_head(Node* head_ptr, int number)
{
    Node* temp_ptr = new Node(number);
    // temp_ptr->item = number;
    temp_ptr->next = head_ptr;
    head_ptr = temp_ptr;   // *
    return temp_ptr;
}


head_ptr is a variable local to _insert_head. Changing the value of head_ptr inside the function will have no effect outside of the function, so while line 6 isn't doing any harm it is entirely unnecessary.

The function is meant to be called like so, assuming head is of type Node* and is pointing to a valid object or contains a null value:

head = _insert_node(head, data_value);

Note that head will be changed when the function call is complete and the returned value is assigned to it.
Last edited on
Topic archived. No new replies allowed.