Commit 46ef8353 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Node example

parent f41b6086
#ifndef _NODE_HPP
#define _NODE_HPP
#include <iomanip>
#include <iostream>
using namespace std;
template <typename T>
class Node
{
public:
Node()
{
ptrPrev = nullptr;
ptrNext = nullptr;
}
void Display()
{
cout << left
<< setw( 10 ) << data
<< setw( 20 ) << ptrPrev
<< setw( 20 ) << ptrNext << endl;
}
T data;
Node* ptrNext;
Node* ptrPrev;
};
#endif
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Nodes" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Nodes" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Nodes" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="Node.hpp" />
<Unit filename="main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Nodes" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Nodes" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Nodes" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="Node.hpp" />
<Unit filename="main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="640" topLine="21" />
</Cursor>
</File>
<File name="Node.hpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="376" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include "Node.hpp"
void OneNode()
{
cout << endl << "ONE NODE" << endl;
cout << left
<< setw( 10 ) << "data"
<< setw( 20 ) << "prev"
<< setw( 20 ) << "next" << endl;
cout << "----------------------------------------------" << endl;
Node<string> node;
node.data = "one node";
node.Display();
}
void TwoNodes()
{
cout << endl << "TWO NODES" << endl;
cout << left
<< setw( 10 ) << "data"
<< setw( 20 ) << "prev"
<< setw( 20 ) << "next" << endl;
cout << "----------------------------------------------" << endl;
Node<string> node1, node2;
node1.data = "first";
node2.data = "second";
node1.ptrNext = &node2;
node2.ptrPrev = &node1;
node1.Display();
node2.Display();
}
void ThreeNodes()
{
cout << endl << "THREE NODES" << endl;
cout << left
<< setw( 10 ) << "data"
<< setw( 20 ) << "prev"
<< setw( 20 ) << "next" << endl;
cout << "----------------------------------------------" << endl;
Node<string> node1, node2, node3;
node1.data = "first";
node2.data = "second";
node3.data = "third";
node1.ptrNext = &node2;
node2.ptrPrev = &node1;
node2.ptrNext = &node3;
node3.ptrPrev = &node2;
node1.Display();
node2.Display();
node3.Display();
}
int main()
{
OneNode();
TwoNodes();
ThreeNodes();
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment