Danh sách liên kết với C++ – Linked list in C++


#include "iostream"
#include "ctime"
using namespace std;
class Element
{
// lop phan tu co ban
// truong du lieu quan ly
int data;
Element * next;
public:
Element(int d, Element * nxt);
friend class LList;
};

Element::Element(int d, Element * nxt):data(d), next(nxt)
{
}

class LList
{
protected:
Element * list; // con tro quan ly cac phan tu
int num; // so phan tu cua danh sach
public:
LList();
~LList();
void push_front(int d); // them vao dau
void push_back(int d); // them vao cuoi
void pop_back(); // xoa phan tu o cuoi danh sach
void pop_front(); // xoa phan tu o dau danh sach
int size()const; // kiem tra so phan tu cua danh sach
int empty()const;
int begin()const;
Element * find()const;
int end()const;
void sort();
void printList()const;
};

int LList::begin()const
{
if(num>0)
return list->data;
return INT_MAX;
}

int LList::end()const
{
Element * p = list;
if(num>0)
{
while(p->next)
p = p->next;
return p->data;
}
return INT_MAX;
}

LList::LList()
{
num = 0;
list = NULL;
cout << "LList constructor" <next!=NULL)
q = q->next;
q->next = p;
}
num ++;
}

void LList::printList()const
{
Element * p = list;
while(p)
{
cout <

data <next;
}
}

LList::~LList()
{
Element * p = list;
while(list)
{
list = list->next;
delete p;
p = list;
}
num = 0;
//cout << "LList destructor" <next;
delete p;
num --;
}
}

void LList::pop_back()
{
Element * q = list;
Element * p;
if(q)
{
p = q;
if(q->next==NULL)
list = NULL;
while(q->next!=NULL)
{
p = q;
q = q->next;
}
p->next = NULL;
delete q;
num --;
}
}

int LList::empty()const
{
return (num==0)?1:0;
}
void LList::sort()
{
Element * p, *q;
p = list;
while(p)
{
q = p->next;
while(q)
{
if(q->data

data)
{
int tam = q->data;
q->data = p->data;
p->data = tam;
}
q = q->next;
}
p = p->next;
}
}
class queue: public LList // lop hang doi ke thua tu danh sach lien ket
{
public:
queue()
{
//LList();
cout << "queue constructor" << endl;
}
};
class IntSet: public LList
{
public:
IntSet operator+(IntSet &);
IntSet operator*(IntSet &);
friend ostream & operator <data);
p = p->next;
}
p = r.list;
while(p)
{
if(!tam.find(p->data))
tam.push_front(p->data);
p = p->next;
}
return tam;
}
IntSet IntSet::operator*(IntSet & r)
{
IntSet tam;
Item * p = list;
while(p!=NULL)
{
if(r.find(p->data))
tam.push_front(p->data);
p = p->next;
}
return tam;
}
ostream & operator << (ostream &os, IntSet & r)
{
os << "{";
Item * p = r.list;
while(p)
{
os <

data <next;
}
os << "}";
return os;
}
int main()
{
queue mList;
int d; // Tested with d = 70000; 07/05/2007
time_t time = clock(); // tinh so xung nhip bat dau tu luc chuong trinh chay
// cho toi thoi diem goi ham
do
{
cout <> d;
if(d>0)
mList.push_back(d--);
}while(d>0);
mList.sort();
while(!mList.empty())
{
cout << mList.begin() << " ";
mList.pop_front();
}
time = clock() - time;
cout << (float)time/CLK_TCK;
system("pause");
return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: