Please reply with your comments.
Code:
#ifndef linkedlist_h
#define string_h
#include<iostream>
using namespace std;
int LLstrlen(char[]);
int LLstrlen(char str[])
{
int i = 0;
while(str[i] != '\0')
i++;
return i;
}
class node
{
private:
int elementNum;
node * next;
node * prev;
char ch;
public:
friend class string;
operator=(char);
node();
friend ostream& operator<<(ostream&, const node&);
};
node::node()
{
next = 0;
prev = 0;
}
node::operator=(char c)
{
this->ch = c;
}
ostream& operator<<(ostream & os, const node & n)
{
os << n.ch;
return os;
}
class linkedlist
{
private:
node * head;
node * current;
node * tail;
int len;
public:
friend ostream& operator<<(ostream&, linkedlist&);
linkedlist();
void clearList();
void calcLen();
int getLen();
void createNext();
void gotoNext();
void gotoPrev();
void gotoHead();
void gotoTail();
void gotoElement(int);
void setCurCh(char);
char getCurCh()const;
void setCurElementNum(int);
char * createlinkedlist();
operator=(char[]);
node operator[](int);
friend void operator>>(istream&, linkedlist&);
};
node linkedlist::operator[](int e)
{
gotoElement(e);
return *current;
}
ostream& operator<<(ostream & os, linkedlist & ll)
{
ll.gotoHead();
for(int i = 0; i < ll.len; i++)
{
os << ll.getCurCh();
ll.gotoNext();
}
return os;
}
linkedlist::operator=(char str[])
{
clearList();
int len = LLstrlen(str);
for(int i = 0; i < len; i++)
{
setCurCh(str[i]);
setCurElementNum(i);
createNext();
gotoNext();
}
this->len = len;
}
void operator>>(istream& is, linkedlist& ll)
{
ll.clearList();
char ch;
is.get(ch);
ll.setCurCh(ch);
ll.setCurElementNum(0);
int len = 0;
int i = 1;
while(ch != '\n')
{
is.get(ch);
ll.createNext();
ll.gotoNext();
ll.setCurCh(ch);
ll.setCurElementNum(i);
len++;
i++;
}
ll.len = len;
}
linkedlist::linkedlist()
{
head = new node;
current = head;
len = 0;
tail = current;
}
void linkedlist::clearList()
{
node * temp;
gotoTail();
for(int i = 0; i < len-1; i++)
{
temp = tail->prev;
delete current;
current = temp;
}
head = current;
tail = current;
}
void linkedlist::calcLen()
{
if(len == 0)
{
while(current->next != 0)
{
len++;
current = current->next;
}
}
}
int linkedlist::getLen()
{
return len;
}
void linkedlist::createNext()
{
current->next = new node;
current->next->prev = current;
}
void linkedlist::gotoNext()
{
current = current->next;
}
void linkedlist::gotoPrev()
{
current = current->prev;
}
void linkedlist::gotoHead()
{
current = head;
}
void linkedlist::gotoTail()
{
current = tail;
}
void linkedlist::gotoElement(int n)
{
gotoHead();
while(current->elementNum != n)
gotoNext();
}
void linkedlist::setCurCh(char c)
{
current->ch = c;
}
void linkedlist::setCurElementNum(int n)
{
current->elementNum = n;
}
char linkedlist::getCurCh()const
{
return current->ch;
}
char * linkedlist::createlinkedlist()
{
calcLen();
char * str = new char[len];
str[len] = '\0';
gotoHead();
for(int i = 0; i < len; i++)
{
str[i] = current->ch;
gotoNext();
}
return str;
}
#endif