呜,刚开始学数据结构,原来c++没学过,有点困难,不过肯定能学好的,我要加油。以下代码借鉴了我两位聪明帅气的室友,qcj和wyz。也许有很多不完善的地方,因为我刚学,知道了会马上改过来。
#include <iostream>
using namespace std;
typedef int ElementType;
class LinkList
{
private:
class Node
{
public:
ElementType data;
Node* next;
Node() :next(0) {}
Node(ElementType dataValue) :data(dataValue), next(0) {}
};
public:
typedef Node* NodePointer;
LinkList();
LinkList(const LinkList& origList);
~LinkList();
const LinkList& operator = (const LinkList& rightSide);
bool empty();
void insert(ElementType dataVal, int idex);
void erase(int index);
NodePointer search(ElementType dataVal);
void display(ostream& out) const;
int nodeCount();
void reverse();
bool ascendingOrder();
void ListMerge(LinkList& templist);
void MergeList(LinkList& listA, LinkList& listB);
ElementType get(NodePointer temp);
private:
NodePointer first;
int mySize;
friend ostream& operator<<(ostream& out, const LinkList& List);
friend istream& operator>>(istream& in, LinkList& List);
};
ostream& operator<<(ostream& out, const LinkList& List)
{
LinkList::NodePointer ptr = List.first;
while (ptr != 0)
{
out << ptr->data << ' ';
ptr = ptr->next;
}
out << endl;
return out;
}
istream& operator>> (istream& in, LinkList& List)
{
ElementType a, b;
in >> a;
for (int i = 0; i < a; i++)
{
in >> b;
List.insert(b, i);
}
return in;
}
LinkList::LinkList()
{
mySize = 0;
first = 0;
}
LinkList::LinkList(const LinkList& rightSide)
{
if (rightSide.mySize == 0)
{
first = 0;
mySize = 0;
return;
}
this->mySize = rightSide.mySize;
NodePointer ptr = new Node(rightSide.first->data);
first = ptr;
int index = 0;
for (ptr = rightSide.first->next; ptr != 0; ptr = ptr->next)
{
index++;
this->insert(ptr->data, index);
}
}
LinkList::~LinkList()
{
NodePointer p = first;
while (first != 0)
{
p = first;
first = first->next;
delete p;
}
}
const LinkList& LinkList::operator=(const LinkList& rightSide)
{
int index = 0;
if (rightSide.mySize == 0)
{
first = 0;
mySize = 0;
return *this;
}
NodePointer ptr = new Node(rightSide.first->data);
this->first = ptr;
this->mySize = rightSide.mySize;
for (ptr = rightSide.first->next; ptr != 0; ptr = ptr->next)
{
index++;
this->insert(ptr->data, index);
}
return *this;
}
bool LinkList::empty()
{
if (mySize) return 1;
else return 0;
}
void LinkList::insert(ElementType dataVal, int index)
{
NodePointer ptr = new Node(dataVal);
if (index > mySize+ 1)
{
cout << "链表越界" << endl;
}
else if (index == 0)
{
mySize++;
first = ptr;
}
else if (index == mySize)
{
mySize++;
NodePointer p2 = first;
while (p2->next != 0)
p2 = p2->next;
ptr->next = p2->next;
p2->next = ptr;
}
else
{
int size = 0;
NodePointer p2 = first;
while (size != index - 1)
{
size++;
p2 = p2->next;
}
ptr->next = p2->next;
p2->next = ptr;
}
}
void LinkList::erase(int index)
{
NodePointer predptr = first;
if (first == 0)
{
cout << "链表为空无法进行删除" << endl;
return;
}
if (index > mySize)
{
cout << "链表越界" << endl;
return;
}
if (index == 0)
{
NodePointer ptr = first;
first = first->next;
mySize--;
delete ptr;
}
else
{
int index2 = 0;
for (predptr = first; predptr != 0; predptr = predptr->next)
{
index2++;
if (index2 == index)
{
mySize--;
NodePointer ptr = predptr->next;
predptr->next = ptr->next;
delete ptr;
}
}
}
}
LinkList::NodePointer LinkList::search(ElementType dataVal)
{
NodePointer ptr = first;
while (ptr != 0)
{
if (ptr->data == dataVal) return ptr;
}
}
void LinkList::display(ostream& out) const
{
for (NodePointer ptr = first; ptr != 0; ptr = ptr->next)
{
out << ptr->data << " ";
}
out << endl;
}
int LinkList::nodeCount()
{
return mySize;
}
void LinkList::reverse()
{
LinkList b;
b.mySize = mySize;
NodePointer p = first, p2;
while (p->next != 0)
{
p2 = new Node(p->data);
p2->next = b.first;
b.first = p2;
p = p->next;
}
p2 = new Node(p->data);
p2->next = b.first;
b.first = p2;
(*this).~LinkList();
*this = b;
}
bool LinkList::ascendingOrder()
{
NodePointer fiptr = first, secptr = fiptr->next;
bool flag = 1;
while (secptr != 0)
{
if (fiptr->data > secptr->data)
{
flag = 0;
return flag;
}
fiptr = fiptr->next;
secptr = secptr->next;
}
return flag;
}
void LinkList::ListMerge(LinkList& templist)
{
if (first == 0)
{
new (this) LinkList(templist);
}
else
{
NodePointer p2 = this->first;
while (p2->next != 0)
p2 = p2->next;
LinkList* p = new LinkList(templist);
mySize += templist.mySize;
p2->next = p->first;
}
}
void LinkList::MergeList(LinkList& ListA, LinkList& ListB)
{
this->ListMerge(ListA);
this->ListMerge(ListB);
}
ElementType LinkList::get(NodePointer temp)
{
return temp->data;
}
int main()
{
LinkList ListA;
ListA.insert(0, 0);
ListA.insert(10, 1);
ListA.insert(20, 2);
ListA.insert(30, 3);
cout << "display测试 " << endl;
ListA.display(cout);
ListA.insert(25, 3);
cout << "插入测试" << '\n' << ListA << endl;
if (ListA.empty()) cout << "链表非空" << endl;
else cout << "链表为空" << endl;
ListA.erase(1);
cout << "删除测试" << '\n' << ListA << endl;
cout << "节点个数 " << ListA.nodeCount() << endl;
ListA.reverse();
cout << "反转测试 " << ListA << endl;
if (ListA.ascendingOrder()) cout << "升序" << endl;
else cout << "非升序" << endl;
cout << "查找指定值测试" << '\n' << ListA.get(ListA.search(30)) << endl;
LinkList ListB;
cin >> ListB;
ListA.ListMerge(ListB);
cout << "合并测试 " << ListA << endl;
LinkList ListC;
cin >> ListC;
ListA.MergeList(ListC, ListB);
cout << "3个合并测试 " << ListA << endl;
return 0;
}
|