//Cursor.h
#ifndef CURSOR_H
#define CURSOR_H
#include <stdlib.h>
#include <iostream>
template <class Object>
class Cursor;
// Incomplete Declaration
template <class Object>
class CNode
{
public:
CNode( const Object & theElement = Object( ), CNode * n = NULL ) : element( theElement ), next( n ) { }
Object element;
CNode *next;
friend class Cursor<Object>;
};
template <class Object>
class Cursor
{
public:
Cursor( );
bool isEmpty( ) const;
void makeEmpty( );
void left ( );
void right ( );
void del ( ); //This is the delete operation. I named it del instead of delete as delete conflicts with a C++ keyword.
void back ( );
void insert( const Object & x );
void home ( );
void end ( );
void undo ( );
private:
void printText ( ) ;
CNode<Object> *header;
CNode<Object> *cursorPosition;
};
//#include "Cursor.cpp"
#endif
//Cursor.cpp
#include "Cursor.h"
#include <iostream>
using namespace std;
template <class Object>
Cursor<Object>::Cursor()
{
header = new CNode<Object>;
cursorPosition = header;
}
template<class Object>
void Cursor<Object>::printText()
{
CNode<Object> *tempPtr;
if(header == cursorPosition)
std::cout << "|";
for(tempPtr=header->next; tempPtr != NULL; tempPtr=tempPtr->next)
{
std::cout << tempPtr->element;
if( tempPtr == cursorPosition )
std::cout << "|";
}
std::cout << "\n\n" ;
}
template <class Object>
bool Cursor<Object>::isEmpty() const
{
return header->next == NULL;
std::cout << "The list is empty";
}
template <class Object>
void Cursor<Object>::makeEmpty()
{
CNode<Object> *tempPtr;
while(!isEmpty())
{
tempPtr = header;
header = header->next;
delete tempPtr;
}
}
template <class Object>
void Cursor<Object>::left()
{
if( cursorPosition != header )
{
CNode<Object> *tempPtr;
for( tempPtr = header; tempPtr->next != cursorPosition; tempPtr = tempPtr->next );
cursorPosition = tempPtr;
}
//printText();
}
template <class Object>
void Cursor<Object>::right()
{
if(cursorPosition->next != NULL)
cursorPosition = cursorPosition->next;
//printText();
}
template <class Object>
void Cursor<Object>::del()
{
if(cursorPosition->next != NULL)
{
CNode<Object> *tempPtr;
tempPtr = cursorPosition->next;
cursorPosition->next = tempPtr->next;
delete tempPtr;
}
//printText();
}
template <class Object>
void Cursor<Object>::back()
{
if(cursorPosition == header)
return;
else
{
CNode<Object> *prev, *p2p; // previous ptr & previous to previous ptr
for(prev = header; prev->next->next != cursorPosition; prev=prev->next);
for(p2p = header; p2p->next->next != prev; p2p=p2p->next);
p2p->next = prev->next;
cursorPosition = p2p;
delete prev;
}
}
template <class Object>
void Cursor<Object>::insert(const Object& x)
{
CNode<Object> *tempPtr;
tempPtr = new CNode<Object>;
tempPtr->element = x;
tempPtr->next = cursorPosition->next;
cursorPosition->next = tempPtr;
cursorPosition = tempPtr;
//printText();
}
template <class Object>
void Cursor<Object>::home()
{
if(cursorPosition == header)
printText();
else
cursorPosition = header;
printText();
}
template <class Object>
void Cursor<Object>::end()
{
for(cursorPosition = header; cursorPosition != NULL && cursorPosition->next != NULL; cursorPosition = cursorPosition->next);
printText();
}
//main.cpp
#include <iostream>
#include "Cursor.h"
using namespace std;
/*
*
*/
int main()
{
Cursor<char> text;
char x;
cout << "Cursor Navigation options are listed below" << endl;
cout << "l move left" << endl;
cout << "r move right" << endl;
cout << "d deletes character to the right" << endl;
cout << "b deletes character to the left" << endl;
cout << "i x - inserts char x after cursor" << endl << endl;
cin >> x;
while(x != '$')
{
switch(x)
{
case 'l':
text.left();
break;
case 'r':
text.right();
break;
case 'd':
text.del();
break;
case 'b':
text.back();
break;
case 'i':
cin >> x;
text.insert(x);
break;
case 'h':
text.home();
break;
case'e':
text.end();
break;
default:
cout << "Enter a valid option." << endl;
break;
}
cout << endl << endl;
cin >> x;
}
return 0;
}