Some Double Linked List Woes.

So I'm making a function w/3 parameters (head pointer and two items [x & y]). Then I need to print to the screen all the items between x & y ( including them). But my teacher wants it to be a double linked list and I'm unable to get it to print. So this would be my 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
30
#include <iostream>
#include "Nodes.h"

using namespace angeles_1A;
using namespace std;

int main()
{
    dnode *p;
    dnode test;
    p = &test;
    
    // Inserts link at head
    test.list_head_insert(0,p);
    
	// Inserts values into the link list
	for(int i = 0; i < 6; ++i)
	{
		test.list_insert(p, i);
	}
    
    
	test.set_xy(2,5);
    
    test.print_items(p);
    
	return 0;
    
    
}


And here would be my header file. You'll notice the Flink and Rlink functions are commented out because my compiler kept giving me an error about needing the non-static reference which I don't understand at all.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef nodes_Nodes_h
#define nodes_Nodes_h
#include <cstdlib>
using namespace std;

namespace angeles_1A
{
    class dnode
    {
        public:
            // TYPEDEFS
            typedef double value_type;
            // CONSTRUCTORs
            dnode(const value_type entry = value_type(), dnode *ptr = NULL)
            { data_field = entry; head_ptr = ptr; tail_ptr = NULL; }
            // SETS VALUES
            void set_Flink(dnode *new_Flink)
            { Flink = new_Flink;}
            void set_Rlink(dnode *new_Rlink)
            { Rlink = new_Rlink;}
                    void set_data(const value_type& new_data)
            {data_field = new_data;}
            void set_xy(value_type entry1,value_type entry2)
            {x = entry1; y = entry2;}
            // RETURNS VALUES
            value_type data() const {return data_field;}
            //const dnode *Flink() const {return Flink;}
            //dnode *Flink() {return Flink;}
            //const dnode *Rlink() const {return Rlink;}
            //dnode *Rlink() {return Rlink;}
            // MANIPULATES LINKED LIST
            void list_head_insert(const value_type& entry, dnode*& ptr)
            { head_ptr = new dnode(entry,ptr); }
            void list_insert(dnode* previous_ptr, const value_type& entry)
            {
                dnode *insert_ptr;
            
                insert_ptr = new dnode(entry, previous_ptr->Flink);
                previous_ptr->set_Flink(insert_ptr);
            }
            void print_items(dnode* ptr)
            {
                 dnode* cursor;
                 for(cursor = ptr; cursor != NULL; cursor = cursor->Flink)
                 {
                     if(cursor->data() >= x || cursor->data() >= y)
                          cout << cursor->data() << " /n";
                 }
            
                 for(cursor = tail_ptr; cursor != head_ptr; cursor = cursor->Rlink)
                 {
                     if(cursor->data() >= y || cursor->data() >= x)
                         cout << cursor->data() << " /n";
                 }
            }
        private:
            value_type data_field, x, y;
            dnode *head_ptr;
            dnode *tail_ptr;
            dnode *Flink;
            dnode *Rlink;
    };
    
    
    
    
}
#endif 


Also, sorry about not organizing it a bit more but a simple tip or a nudge in the right direction would be greatly appreciated.
Last edited on
on line 46:

if(cursor->data() >= x || cursor->data() >= y) // This is not 'between'

change to to
if(cursor->data() >= x && cursor->data() <= y) // This is 'between'

on line 50: why do you want to do the same as above just backwards?
"on line 50: why do you want to do the same as above just backwards?"

My teacher wants the program to print it both ways.

Thanks but all its outputting is (lldb). Any idea why?

Also on line 42

{ head_ptr = new dnode(entry,ptr); }

I'm not getting an error but a green highlight that says "Thread 1: breakpoint 1.1" Does this mean anything is having an issue?
Last edited on
Also on line 42
No, not on line 42, it's line 33

It's really odd what you're doing there.

Ok, every node actually has a head_ptr/tail_ptr. Shouldn't they all point to the same thing?

Is there any reason why on line 32 it is dnode*& ptr? I.e. a reference to that pointer (which means that the value of ptr can be changed).
That's by the way the reason for the acrobatics on line 9 to 11 in main()

Thanks but all its outputting is (lldb). Any idea why?
on line 19 in main() you create a lot of nodes, but they're all dismissed (because you don't set Flink or Rlink or tail_ptr). Only the most recent remains. So it's supposed to show the most recent. I have no idea what (lldb) mreans.

By the way: it is "\n" not "/n" for new line.
Topic archived. No new replies allowed.