Linked Lists – Part II

Following is a screen capture of the console containing output from the test program for linked lists written in C++ using the Visual Studio 2013 IDE:

main <<< testing widgets ...
main <<< widget=  id: 1234 name ==>widget_1234<==

main <<< testing nodes ...
main <<< node=  next: 0x0 prev: 0x0
main <<< widget=  id: 1234 name ==>widget_1234<==
main <<< p= data: 1 next: 000001DB624B10E0
main <<< p= data: 2 next: 000001DB624B1160
main <<< p= data: 3 next: 000001DB624B11E0
main <<< p= data: 4 next: 000001DB624B1260
main <<< p= data: 5 next: 0000000000000000
main <<< deleting: 1
main <<< deleting: 2
main <<< deleting: 3
main <<< deleting: 4
main <<< deleting: 5

The test code written in C++ follows:

// **** ****

#include <iostream>

// **** ****

using namespace std;

// **** ****

#include "Node.h"
//#include "SingleLinkList.h"
#include "Widget.h"
#include "Solution.h"

// **** constructor ****

Solution::Solution()
{}

// **** destructor ****

Solution::~Solution()
{}

/*
* TEST code.
*/
int main()
{

	// ***** activity message ****

	cout << "main <<< testing widgets ..." << endl;

	// **** instantiate a new Widget ****

	Widget widget = Widget();

	// **** populate widget ****

	try {
		widget.setId(1234);
	} catch (int ex) {
		if (ex == widget.WidgetInvalidId) {
			cerr << "main <<< EXCEPTION widget.setId() - WidgetInvalidId" << endl;
			exit(ex);
		}
	}
	
	try {
		widget.setName("widget_1234");
	} catch (int ex) {
		if (ex == widget.WidgetInvalidName) {
			cerr << "main <<< EXCEPTION widget.setName() - WidgetInvalidName" << endl;
			exit(ex);
		}
	}

	// **** display the widget ****

	cout << "main <<< widget= " << widget.toString() << endl;

	// ***** activity message ****

	cout << endl;
	cout << "main <<< testing nodes ..." << endl;

	// **** instantiate a node ****

	Node<Widget> node = Node<Widget>(widget);

	// **** display the node ****

	cout << "main <<< node= " << node.toString() << endl;

	// **** display the widget ****

	cout << "main <<< widget= " << node.data.toString() << endl;

	// **** build a crude list of nodes ****

	Node<int> *list = nullptr;
	for (int i = 1; i <= 5; i++) {

		// **** allocate a node ****

		Node<int> *n = new Node<int>(i);

		// **** insert node at tail of list ****

		if (list == nullptr) {
			list = n;
		} else {
			Node<int> *p = list;
			for ( ; p->next != nullptr; p = p->next) {}
			p->next = n;
		}
	}

	// **** traverse the crude list of nodes ****

	for (Node<int> *p = list; p != nullptr; p = p->next) {
		cout << "main <<< p= data: " << p->data << " next: " << p->next << endl;
	}

	// **** delete all elements from the crude list of nodes ****

	for (Node<int> *p = list; p != nullptr; ) {
		Node<int> *n = p;
		p = p->next;
		cout << "main <<< deleting: " << n->data << endl;
		delete(n);
	}

#ifdef CAKE
	// ***** activity message ****

	cout << endl;
	cout << "main <<< testing single link queue ..." << endl;

	// **** allocate single link queue ****

	SingleLinkList *slq = new SingleLinkList();
	cout << "main <<< slq: " << slq->toString() << endl;

	// **** loop inserting elements into the queue ****

	for (int i = 1; i <= 5; i++) {
		slq->enqueue(i);
	}
	cout << "main <<< slq: " << slq->toString() << endl;

	// **** loop removing elements from the queue ****
	
	while (!slq->isEmpty()) {

		// **** dequeue and display the value for the next element ****

		cout << "main <<< dequeue: " << slq->dequeue() << endl;

		// **** display the contents of the queue ****

		cout << "main <<< slq: " << slq->toString() << endl;
	} 

	// **** iterate through list ****
#endif

	// **** all done ****

	return 0;
}

Please note that in my code, the symbol CAKE is never defined. I always use it to exclude code from compilation.

Hope to at least complete the implementation of the SingleLinkList class over the weekend. If I have additional spare time will try to get done the DoubleLinkList class. That is the one I will be using when I get back to the LFU Cache implementation using C++.

If you have comments or questions regarding this or any other post in this blog I invite you to send me a message. I will not use your name unless you explicitly allow me to do so.

John

john.canessa@gmail

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.