Linked List Problem

I'm having some trouble getting my linked list to work, here is the prompt.

Write a function with three parameters. The first parameter is a head pointer for a linked list of items, and the next two parameters are items x and y. The function should write to the screen all items in the list that are between the first occurrence of x and the first occurrence of y. You may assume that the items can be compared for equality using ==.

Here is what I have so far, I want to know if I am on the right track.

Main:
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
int main()
{

	// Creates Pointer
	node *the_pointer;
	the_pointer = new node;

	list_head_insert(the_pointer, 0);


	// Inserts values into the link list
	for(int i = 0; i < 10; i++)
	{
		list_insert(the_pointer, i);
	}


	node::value_type x = 1;
	node::value_type y = 6;

    getItems(the_pointer, x, y);



	system("pause");
	return 0;


}




Functions
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
    
 void list_head_insert(node*& head_ptr, const node::value_type& entry)
    {
	   head_ptr = new node(entry, head_ptr);
    }



void list_insert(node* previous_ptr, const node::value_type& entry)
    {
	   node *insert_ptr;

	   insert_ptr = new node(entry, previous_ptr->link( ));
	   previous_ptr->set_link(insert_ptr);
    }

void getItems(const node* head_ptr, const node::value_type& x, const node::value_type& y)
	{
		bool isvalid = false;
		while (head_ptr != NULL)
		{
			if(head_ptr->data() == x)
			{
				isvalid = true;
			}
			if(isvalid == true)
			{
				cout << head_ptr->data();
			}
			if(head_ptr->data() == y)
			{
				isvalid = false;
			}
		}

	}
Last edited on
1. You should use bool type for true or false.

2. Have two of them for x and y.

3. Actually traverse the list (not doing that at the moment).
Last edited on
3. Actually traverse the list (not doing that at the moment).

How would I go about doing that?
Do you have a function in the head_ptr that returns the next node?
Last edited on
Topic archived. No new replies allowed.