[ create a new paste ] login | about

Link: http://codepad.org/XJ11APKC    [ raw code | output | fork ]

C++, pasted on Jun 24:
#include<iostream>
using namespace std;

struct ListElem
{
char info;
ListElem * next;
};

class List
{
ListElem* begin;
ListElem* curr;

public:
List(void)
{
begin = curr = 0;
}

List(const List& p)
{
begin = curr = 0;
ListElem* temp = p.begin;
while(temp)
{
AddEnd(temp->info);
temp = temp->next;
}
curr = begin;
}

~List(void)
{
while( begin )
{
curr = begin->next;
delete begin;
begin = curr;
}
begin = curr = 0;
}

void AddEnd(char x)
{
if(!curr)
{
this->Add(x);
return;
}
while(curr->next)
curr = curr->next;
curr->next = new ListElem;
curr = curr->next;
curr->info = x;
curr->next = 0;
}

void Add(char x)
{
if(!curr)
{
curr = new ListElem;
curr->next = 0;
curr->info = x;
begin = curr;
return;
}
ListElem* temp = curr->next;
curr->next = new ListElem;
curr = curr->next;
curr->info = x;
curr->next = temp;
}

void DelCurr()
{
if(!curr)
return;

if(!begin->next) 
{
delete begin;
curr = begin = 0;
return;
}

ListElem* temp = begin;

if(!curr->next)
{ 
while(temp->next != curr)
temp = temp->next;
delete temp->next;
curr = temp;
curr->next = 0;
return;
}
temp = curr->next->next;
curr->info = curr->next->info;
delete curr->next;
curr->next = temp;
}

void Next()
{
if(curr && curr->next)
curr = curr->next;
}

void MakeEmpty()
{
curr = begin;
while(begin)
this->DelCurr();
}

List& operator=(const List& p1)
{
if(this == &p1)
return *this;
this->~List();
ListElem* temp = p1.begin;
while(temp)
{
this->AddEnd(temp->info);
temp = temp->next;
}
return *this;
}

friend bool operator<(const List& p1,const List& p2)
{
ListElem* curr1 = p1.begin;
ListElem* curr2 = p2.begin;
while(curr1 && curr2)
{
if(curr2->info < curr1->info)
return 1;
if(curr2->info > curr1->info)
return 0;
curr1 = curr1->next;
curr2 = curr2->next;
}
if(curr2)
return 0;
if(curr1)
return 1;
return 0;
}

friend bool operator>(const List& p1,const List& p2)
{
return (p2<p1);
}

friend bool operator==(const List& p1,const List& p2)
{
return(! (p2<p1 || p1<p2));
}

friend istream& operator>>(istream& in, List& s)
{
if(in && in.peek()=='\n')
return in;
s.MakeEmpty();
while(in && in.peek()!='\n' )
{
s.AddEnd(in.get());
}
s.curr = s.begin;
in.ignore();
return in;
}

friend ostream& operator<<(ostream& out, const List& s)
{
ListElem* temp = s.begin;
while(temp)
{
out<<temp->info;
temp = temp->next;
}
return out;
}
};


Output:
1
2
In function `_start':
undefined reference to `main'


Create a new paste based on this one


Comments: