Coding Doubly Linked List using templates

 

#include <iostream>

#ifndef __FS_DLL_H_
#define __FS_DLL_H_

template <class T>
class DLL;

template <class T>
class Node{
T _data;
Node<T>* _prev;
Node<T>* _next;
Node(T data, Node<T>* prev=(Node<T>*)0, Node<T>* next=(Node<T>*)0);// just incase, review later
friend class DLL<T>;
};

template <class T>
class DLL{
Node<T>* _head;
Node<T>* _curr;
Node<T>* _tail;
void copy(DLL<T>& D);
public:
DLL();
DLL(DLL<T>& D);
virtual ~DLL();
bool isEmpty();
void append(T data);
DLL<T>& operator=(DLL<T>& D);
T remove(); // removes the current node and returns the data
bool del(); // removes the current node returns false if is empty
void insert(T data); // insterts before current
bool goHead();
bool goTail();
bool goNext();
bool goPrev();
int visit(); // returns the current data
void printList(DLL<T>& d);
};

template <class T>
Node<T>::Node(T data, Node<T>* prev, Node<T>* next){
_data = data;
_prev = prev;
_next = next;
}

template <class T>
DLL<T>::DLL(){
_head = _tail = _curr = 0;
}

template <class T>
DLL<T>::~DLL(){
while(del());
}

template <class T>
void DLL<T>::copy(DLL<T>& D){
int curpos;
for(curpos=0;D<T>.goPrev();curpos++); // findout where is current
if(!D<T>.isEmpty()){
do{
this->append(D<T>.visit());
}while(D<T>.goNext());
}
for(D<T>.goHead(), this->goHead();curpos;D<T>.goNext(), this->goNext(),curpos–);
}

template <class T>
DLL<T>::DLL(DLL<T>& D){
_head = _tail = _curr = 0;
copy(D<T>);
}

template <class T>
DLL<T>& DLL<T>::operator=(DLL<T>& D){
while(del());
copy(D<T>);
return *this;
}

template <class T>
void DLL<T>::append(T data){
Node<T>* newnode = new Node<T>(data);
if(_tail){ // ! empty
_tail->_next = newnode;
newnode->_prev = _tail;
_tail = _curr = newnode;
}
else{
_tail = _curr = _head = newnode;
}
}

template <class T>
T DLL<T>::remove(){
T data = visit();
del();
return data;
}

template <class T>
bool DLL<T>::del(){
bool ok = false;
if(_curr){
ok = true;
Node<T>* todel = _curr;
if(_curr->_next){
_curr->_next->_prev = _curr->_prev;
}
else{
_tail = _tail->_prev;
}
if(_curr->_prev){
_curr->_prev->_next = _curr->_next;
}
else{
_head = _head->_next;
}
_curr = _curr->_next;
delete todel;
}
return ok;
}

template <class T>
void DLL<T>::insert(T data){
}

template <class T>
int DLL<T>::visit(){ // retruns data of current
return 0;
}

template <class T>
bool DLL<T>::goHead(){
return false;
}

template <class T>
bool DLL<T>::goTail(){
return false;
}

template <class T>
bool DLL<T>::goNext(){
return false;
}

template <class T>
bool DLL<T>::goPrev(){
return false;
}

template <class T>
bool DLL<T>::isEmpty(){
return false;
}

template <class T>
void printList(DLL<T>& d){
d.goHead();
}

#endif

Challenge: Print backwards with seekg((ios::off_type)value, ios::cur)

#include <iostream>
#include <fstream>
using namespace std;

int main(){
int i;

fstream file(“output.bin”,ios::in|ios::binary);

file.seekg((ios::off_type)0, ios::end); // goes to the end again

file.seekg((ios::off_type)(-4), ios::cur);
file.read((char*)&i, sizeof(i));
cout<<i<<“, “;

int j = 1;
int k = file.tellg();
while(j<k/sizeof(int)){
file.seekg((ios::off_type) (-8), ios::cur);
file.read((char*)&i, sizeof(i));
cout<<i<<“, “;
j++;
}
return 0;
}

For the draw() function of CCheckMark, the instructions say the function “sets the position of the cursor at the checkmark (second character of printed _format)”

I tried the following but the cursor position comes after the “Not Checked” text.

console.setPos(absRow(), absCol()+1);

Can anyone tell me what am I doing wrong here?

This functions sets the given bit on V to the given value in val

void setBit(unsigned int& V, int bitNo, bool val){

if(val) {
V = (1 << (bitNo-1)) | V;
}
else {
V = ~(1 << (bitNo-1)) & V;
}
}

This function copies bits from a starting point (bitFrom) upto a given length of variable mask and sets the bit in the corresponding position in variable V.

void copyBits(unsigned int& V, int bitFrom, int length, unsigned int mask){

int m1 = 1 << (bitFrom – 1);
int m2 = m1 & mask;

for (int i=bitFrom+1; i<=bitFrom+length; i++) {

int m1 = 1 << (i-1);
bool mbit = !!(mask&m1); //Gets ith bit of mask

setBit(V, i, mbit); //Sets ith bit of V to the ith bit of mask
}
}

Given a memory address and the number of bytes, this function prints out the bit pattern of that piece of memory

void bitDump(void* mem, unsigned int size) {

unsigned char* c = (unsigned char*) mem;
int j = 0;

for(int i=size-1; i>=0; i–) {
unsigned int m = 1 << 7;

while(m){
printf(“%d”, !!(c[i]&m));
m = m >> 1;

j++;
if (j==4) {
putchar(‘ ‘);
j = 0;
}
}
}
printf(“\n”);
}

The programs converts an integers into a string of its bits

const char* bits(unsigned int V)
{

unsigned int m = 1 << sizeof(V) * 8 – 1;

char* bitsV = new char[sizeof(V) * 8];
int i = 0;

while(m) {
bitsV[i] = 48+!!(V&m);
m = m >> 1;
i++;
}

bitsV[i] = ”;
return bitsV;
}