I found a data structures tutorial online that I followed through a little bit, and I wrote my own version of the linked list class it talks about. It works fine, but every time I compile it gives me a warning for each for/while loop I have: "Functions containing while are not expanded inline." It doesn't affect how the program actually works, but I'm just wondering what these warnings are about.
Here's my linked list class along with a basic main function to test it, just in case it might help.
Code:
#include <iostream>
using namespace std;
class linkedlist {
private:
// A node structure; each data value is a node
struct node {
int data;
node *link;
} *p;
public:
// Constructor
linkedlist () {
p = NULL; // Primary node
}
// Desctructor
~linkedlist () {
// Destroy the list
node *q;
while (p != NULL) {
q = p;
p = q->link;
delete q;
}
}
// Append a new data value
// @param val the data value to append
void append (int val) {
// If the list is empty
if (p == NULL) {
p = new node;
p->data = val;
p->link = NULL;
}
// If the list is not empty
else {
node *q, *t;
q = p;
while (q->link != NULL)
q = q->link;
t = new node;
t->data = val;
t->link = NULL;
q->link = t;
}
}
// Adds an item to the front of the list
// @param val the item to add
// @return true if it was added successfully
bool add_as_first (int val) {
node *q;
q = new node;
q->data = val;
q->link = p;
p = q;
return true;
}
// Adds an item after a certain position
// @param j the position to add
// @param val the item to add
// @return true if it was added successfully
bool add_after (int j, int val) {
node *q, *t;
q = p;
for (int i = 0; i<j; i++) {
q = q->link;
if (q == NULL)
return false;
}
t = new node;
t->data = val;
t->link = q->link;
q->link = t;
return true;
}
// Deletes the first instance of val
// @param val the value to find and delete
// @return true if the value was found and deleted
bool del (int val) {
// t is the node pior, q is the current node
node *q, *t;
q = p;
t = NULL;
while (q->data != val) {
if (q->link == NULL)
return false;
t = q;
q = q->link;
}
// If the data was p, the first value in the list
if (t == NULL) {
p = q->link;
delete q;
}
// If the data found was not the first in the list
else {
node *r;
r = q->link;
t->link = r;
delete q;
}
return true;
}
// Display the list
void display () {
node *q;
cout << "[";
q = p;
while (q->link != NULL) {
cout << q->data << ", ";
q = q->link;
}
cout << q->data;
cout << "]" << endl;
}
// Get the size of the list
// @return the number of nodes in the list
int size () {
int c = 0;
node *q;
for (q = p; q != NULL; q = q->link)
c++;
return c;
}
};
int main () {
linkedlist list;
list.append(1);
list.append(3);
list.append(4);
list.add_after(0, 2);
list.display();
cout << "Size: " << list.size() << endl;
return 0;
}