## Simple List Class in C++

Here is an example of a simple List class I wrote. Follow the comments to understand what is going on. This uses the node structure I wrote about here.

Best of luck!

#include <cstdlib>
#include "Node.h" //Your Node class, see previous post

using namespace std;

class List
{
public:
List(){
}
~List(){
}
void append(int d){
}
void print(){
}
void insertInOrder(int d){
}
bool findValue (int d){
}
int getSize(){
}

private:
Node *head; //Remember this struct from the previous post

/* Destroy the list
* We recursively call the destroy function to cycle through a list to the end.
* Once the end node is reached, it is deleted. Then as we progress back through
* our recursive call, each associated node is deleted from the end up.
* This is called by our destructor to clean everything up.
*/
void destroyList(Node *n){
if (n->next != nullptr){
destroyList(n->next);
}
delete n;
}

/* Add a Node to the end of a list
* Use recursion to cycle through the list to the end
* Once the end is reached, the next pointer of the last node will by nullptr.
* A new Node will then be inserted replacing that nullptr and chaining to the list
*/
Node* append(Node *n, int d){
if (n == nullptr){
return new Node(d);
}
else {
n->next= append(n->next, d);
}
return n;
}

/* Print the List
* Use recursion to loop through and print each Node in a list
*/
void print(Node *n){
cout << n->data << endl;
if (n->next != nullptr){
print(n->next);
}
}

/* Insert a Node in Numeric order
* Loop throug a list using recursion
* Once the inserting value is less than the current Node
* we know we need to insert the Node in that position
* else we keep cycling through
*/
Node* insertInOrder(Node *n, int d){
if (n == nullptr){
return new Node(d);
}
else if (d > n->data){
n->next= insertInOrder(n->next, d);
}
else {
Node* temp = n; //Temp copy
n = new Node(d); //Set the pointer to a new Node
n->next = temp; //Chain the original onto the new Node's next pointer
}
return n;
}

/* Find a Node
* Loop through the enitre list using recursion
* Return true once the Node value we want is found
*/
bool findValue(Node *n, int d){
if (n->data == d){
return true;
}
else if(n->next != nullptr){
return findValue(n->next, d);
}
return false;
}

/* Get the Size of a List
* Use recursion to cycle through the list
* all the while keeping a counter going of each Node encountered
*/
int getSize(Node *n, int i = 0){
if (n != nullptr){
++i;
return getSize(n->next, i);
}
return i;
}

};

## The typical Node structure in C++

The next couple of posts are going to require the use of a node object. I’ve seen people make the node way more complicating then it should be as a class. The point of a node is to hold data and be aware of it’s neighbors. All you really need is a simple struct.

In the instance of a stack or list, a node just really needs to know who’s next after them. Note in the code below, to create a node, I need some data passed in, that’s it. It’s up to the coder to set the next pointer in their code that is utilizing the node.

struct Node
{
int data;
Node* next;

Node(int d){
data = d;
next = nullptr;
}
};

When we start looking at linked lists and binary search trees, we might consider a struct that is aware of the nodes on both sides of it. Again, it is up to the code using the node structure to set the left and right pointers to the right node neighbors.

struct Node
{
int data;
Node* left;
Node* right;

Node(int d){
data = d;
left = nullptr;
right = nullptr;
}
};

Pretty dang simple!

## Screen

I love using screen at work, especially while I’m in a PuTTy session. Basically, screen lets you open up another bash session without disrupting a current bash session in the same window. So I can open one PuTTy session and with screen emulate multiple. This is nice because I can do something different in each screen session without loosing my place in another. Trust me, it’s awesome.

Install

apt-get install screen

or (depends on your distribution and packet manager)

yum install screen

Use

Create a screen by typing the command:

screen

Super easy, to detach from a screen:

press CTRL+”a”+”d”

Now what? There is now a floating screen session someone. How do you reconnect to it? First you can list all screen session with:

screen -ls

With knowledge of what screens exist, you can reconnect to one with the command:

screen -r <SCREEN REFERENCE>

To kill the current screen, use:

press CTRL+”a”+”\”

Other helpful shortcuts can be found by pressing:

CTRL+”a” release “?”

There you have it! Manage your screens wisely.