Need to complete Insert function

Need to create the insert function all other code is complete and cant be changed, struggling to get this to work at all, have tried creating a node however got no further, any help would be great, the more detailed the better

This is the part i need to complete
1
2
3
4
5
6
7
// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
bool List::insert(const char *string_p)
{
  // Please write the list insert function
  return SUCCESS;
}


FULL CODE
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

#define SUCCESS 0
#define FAIL    1

// Represents an entry object in the linked-list
class ListEntry
{
  public:
    explicit     ListEntry();
    explicit     ListEntry(const char *string_p);
                 ~ListEntry();
    string       getData();
    void         setData(const char* string_p);
    void         setData(string string);
    ListEntry   *getNext();
    ListEntry   *getPrevious();
    ListEntry   *prev_p;   // pointer to previous entry in the linked-list
    ListEntry   *next_p;   // pointer to next entry in the linked-list

  private:
    string          data;      // entry's string
};

// Represents the linked-list object
class List
{
  public:
    List();
    ~List();

    bool printForward();
    bool printReverse();
    bool insert(const char *string_p);

  private:
    int        entryCount;  // number of entries present in the linked-list
    ListEntry *head_p;      // pointer to the first entry in the list
    ListEntry *tail_p;      // pointer to the last entry in the list
};

// ListEntry constructor
ListEntry::ListEntry()
{
  this->prev_p = NULL;
  this->next_p = NULL;
  return;
}

// ListEntry constructor
ListEntry::ListEntry(const char *string_p)
{
  this->data   = string_p;
  this->prev_p = NULL;
  this->next_p = NULL;
  return;
}

// List entry destructor 
ListEntry::~ListEntry()
{
  return;
}

// Return the stored string object
string ListEntry::getData()
{
  return this->data;
}

// Set the internal string data from a char*
void ListEntry::setData(const char* string_p)
{
  this->data = string_p;
}

// Set the internal string data from a string
void ListEntry::setData(string string)
{
  this->data = string;
}
 
// Returns reference to the next entry in the list
ListEntry *ListEntry::getNext()
{
  return this->next_p;
}

// Returns reference to the previous entry in the list
ListEntry *ListEntry::getPrevious()
{
  return this->prev_p;
}

// List constructor
List::List()
{
  this->entryCount = 0;
  this->head_p     = NULL;
  this->tail_p     = NULL;
}

// List destructor
List::~List()
{
  // Delete all entries in the list
  ListEntry *entry_p   = this->head_p;
  ListEntry *current_p = this->head_p;

  while (entry_p != NULL)
  {
    current_p = entry_p; 
    entry_p = entry_p->getNext();
    delete current_p;
  }
 }

// Output linked list in order from head to tail
// printing out the string data from each list entry
bool List::printForward()
{
  ListEntry *entry_p = this->head_p;
  int count = 0;

  cout << "FORWARD: " << this->entryCount << " entries\n";
  while (entry_p != NULL)
  {
    cout << entry_p->getData() << " ";

    if (++count % 5 == 0 || entry_p == this->tail_p)
    {
      cout << endl;
    }

    entry_p = entry_p->getNext();
  }

  return SUCCESS;
}

// Output linked list in reverse order from tail to head
// printing out the string data from each list entry
bool List::printReverse()
{
  ListEntry *entry_p = this->tail_p;
  int count = 0;

  cout << "REVERSE: " << this->entryCount << " entries\n";
  while (entry_p != NULL)
  {
    cout << entry_p->getData() << " ";

    if (++count % 5 == 0 || entry_p == this->head_p)
    {
      cout << endl;
    }

    entry_p = entry_p->getPrevious();
  }

  return SUCCESS;
}

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
bool List::insert(const char *string_p)
{
  // Please write the list insert function
  return SUCCESS;
}

int main(int argc, char **argv)
{
  (void) argc;
  (void) argv;

  const char *places[] = { "Aberdeen", "Bakewell", "Cocking", "Dogdyke",
                           "Eastbourne", "Farranfadda", "Gaggin", "Hadfield",
                           "Irnham", "Jameston", "Kendal", "Langley",
                           "Malmesbury", "Netherhampton", "Ogmore", "Pant",
                           "Quedgeley", "Ramsbottom", "Sallynoggin",
                           "Talla Bheith Forest", "Upchurch", "Victoria Bridge",
                           "Wacton", "Xeroxillington", "Yelling", "Zetland" };
  
  unsigned char indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
                              2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };


  List placesList;

  // Insert every word in the places array into the
  // linked-list.
  cout << "INSERT:\n";
  for (unsigned int count = 0; count < sizeof(indexes); count++)
  {
    cout << places[indexes[count]] << " ";

    //Add a new line for console readability
    if( count > 0 && count % 5 == 0)
    {
      cout << endl;
    }

    placesList.insert(places[indexes[count]]);
  }
  cout << endl;

  // print out the list in alphabetical order
  placesList.printForward();
  cout << endl;

  // print out the list in reverse alphabetical order
  placesList.printReverse();
  cout << endl;

  return SUCCESS;
}
Why do you have getNext() and getPrevious() functions wihen prev_p and next_p are public?

There are two parts to the insert() method. First you have to figure out where to insert the item, and then you have to actually do it.

You can figure out where to insert it by walking down the list, comparing the current node to the name. Once you get to a node that whose name is >= the name you're inserting, then you've found the node that you to insert before. Put another way, the current node is the one that will come after the new node. The current node pointer might be nullptr, which means the new node gets inserted at the end.

Next, create a pointer to the node that comes before the new one. Remember, if the current node pointer is nullptr, that means the new node goes at the end of the list.

Now you have a pointer to the node that comes before (or nullptr if it goes at the beginning), and a pointer to the node that comes after (or nullptr if it goes at the end). Time to make some links!
Topic archived. No new replies allowed.