access to a existing list in pointer function

Hey I'm a beginner in C++ so please don't blame me if the question is stupid :D

Because it was too complicated to implement the sorting function in my list I created a function with a pointer for the beginning. But now I don't know how I can take the elements of my existing list inside this function.

Explanation what I want to do: I have a text file with pairs of numbers and have to sort them principally like in this domino game. so like this [3:2][1:2][3:1](start), [1:3][3:2][2:1](end). Sometimes there are more numbers in one file and I could creat more rings.
I hope you understand me :D
Thanks for the help.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  ListPair* sortedPair(Node *head)
{

    Ringliste ringList; //<- this is the new ringlist with the new sorted pairs
    List domino;  //<- this is the list with the elements, unsorted

    //name elements of list
    Node *node_a = head;
    Node *node_b = node_a->next;
    Node *startNode = node_a;

    //put everything NULL
    RingPair *ringHead = new RingPair();
    ringHead->next = NULL;
    ListPair* listPair = NULL;
    RingPair* nextPair = NULL;

    //first element of list is first element of new list
    ringHead->node = node_a;

    while (node_a != NULL)
    {
        while (node_b != NULL)
        {
            //first two elements suit togehter
            if (node_a->b == node_b->a)
            {
                RingPair *nextPair = new RingPair();
                nextPair->node = node_b;
                ringList.insertPair(ringHead,nextPair);

                //the two elements are in new ringlist
                //have to be deleted from old list
                Node *temp_2 = node_b->next;
                //head = deleteNode(head, node_b);

                domino.deleteHead();

                //take next element
                node_a = node_b;
                if (temp_2 != NULL)
                    node_b = temp_2;
                else
                    node_b = head;
            }

            else if (node_a->b == node_b->b)
            {
                ...//the sorting algorithm is not that necessary for my problem
            }

            if ((node_a->b != node_b->a) && (node_a->b != node_b->b))
            {
                //can't make any more pair with first element of old list
                //take next element from list
                node_b = node_b->next;

                if ((node_b == NULL) && (head->next != NULL))
                {
                    node_b = head;
                }
            }

            //insert ring to be able to begin new ring
            ListPair *nextRingPair = new ListPair();
            nextRingPair->ringPair = ringHead;
            if (listPair == NULL)
                listPair = nextRingPair;
            else
            {
                ListPair *temp = listPair;
                while (temp->next != NULL)
                    temp = temp->next;
                temp->next = nextRingPair;
            }
        }

        //all elements are now in ringlist
        //delete old elements
        head = deleteNode(head, node_a);
        head = deleteNode(head, startNode);

        //new element is head
        node_a = head;
        startNode = node_a;

        //begin new ring because old one is completed
        ringHead = new RingPair();
        ringHead->node = node_a;
        ringHead->next = NULL;
    }
    return listPair;
}


You can also watch the whole code here: http://c9.io/naralli/domino
(but there are the comments German because unfortunately I have to make it for a German project :D )
Last edited on
Topic archived. No new replies allowed.