/*
* test.cpp
*
* Created on: 2015-1-22
* Author: cainiao
*/
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
class List;
class Object
{ //数据类为抽象类
public:
Object() {} //构造函数不可为虚函数
virtual int Compare(Object &)=0; //纯虚函数
virtual void Print()=0; //纯虚函数
virtual ~Object() {}
};
//析构函数可为虚函数
class StringObject: public Object
{
char* sptr;
public:
StringObject() //缺省构造函数
{
sptr = NULL;
}
StringObject(char* s) //构造函数
{
sptr = (char *) malloc(strlen(s) + 1); //c是一个char型指针 ,如果不为其分配空间 那么将不能用strcmp 如果用c++ 怎么实现?new?
strcpy(sptr, s);
}
~StringObject() //析构函数
{
if (sptr)
{
free(sptr);
}
}
int Compare(Object & obj) //比较函数
{
StringObject &a = (StringObject &) (obj);
int c = strcmp(sptr, a.sptr);
return c;
}
void Print() //打印函数
{
cout << sptr << " -->";
}
};
class Node {
Object* info; //数据域用指针指向数据类对象
Node* link; //指针域
public:
Node() //生成头结点的构造函数
{
info = NULL;
link = NULL;
}
~Node() //析构函数
{}
void InsertAfter(Node* p) //在当前结点后插入一个结点
{
cout << "this Node" << this << endl;
p->link = this->link;
this->link = p;
}
Node* RemoveAfter()
{
Node *p = this->link;
this->link = this->link->link;
return p;
}
//删除当前结点的后继结点,返回该结点备用
void Linkinfo(Object* obj) //把数据对象连接到结点
{
this->info = obj;
}
friend class List;
};
class List
{
Node *head, *tail; //链表头指针和尾指针
public:
List() //构造函数,生成头结点(空链表)
{
head = tail = new Node;
}
~List()
{
MakeEmpty();
delete head;
} //析构函数
void MakeEmpty() //清空链表,只余表头结点
{
Node *tempNode = head;
while (tempNode)
{
tempNode = head->link;
head->link = tempNode->link;
delete tempNode;
}
tail = head;
}
Node* Find(Object & obj)
{
Node * p = head->link;
while (p)
{
if (p->info->Compare(obj))
return p;
p = p->link;
}
return NULL;
}
//搜索数据域与定值相同的结点,返回该结点的地址
int Length() //计算单链表长度
{
Node *p = head->link;
int length = 0;
while (p)
{
++length;
p = p->link;
}
return length;
}
void PrintList() //打印链表的数据域
{
Node *p = head->link;
while (p)
{
p->info->Print();
p = p->link;
}
cout << endl;
}
void InsertFront(Node* p) //可用来向前生成链表
{
p->link = this->head->link;
this->head->link = p;
}
void InsertRear(Node* p) //可用来向后生成链表
{
this->tail->link = p;
this->tail = p;
}
void InsertOrder(Node * p) //按升序生成链表
{
Node *q = this->head->link;
cout << "this->head->link " << this->head->link << endl;
Node *r = head;
if (!q)
{
head->InsertAfter(p);
} else
{
cout << q << endl;
while (q)
{
int a = q->info->Compare(*(p->info));
if (a >= 0)
{
break;
}
r = q;
q = q->link;
}
r->InsertAfter(p);
}
}
Node* CreatNode() //创建一个结点(孤立结点)
{
Node * p = new Node;
return p;
}
Node* DeleteNode(Node* q)
{
Node *p = this->head->link;
while (!(p->link->info->Compare(*(q->info))))
{
p = p->link;
}
p->link = p->link->link;
return q;
}
};
//删除指定结点
int main()
{
Node * P1;
StringObject* p;
List list1, list2, list3;
int i;
char *a[5] = { "fog", "eat", "zear", "cheep", "box" };
char *sp = "cat";
for (i = 0; i < 5; i++)
{
p = new StringObject(a[i]); //建立数据对象
P1 = list1.CreatNode(); //建立结点
P1->Linkinfo(p); //数据对象连接到结点
list1.InsertFront(P1); //向前生成list1
p = new StringObject(a[i]);
P1 = list2.CreatNode();
P1->Linkinfo(p);
list2.InsertRear(P1); //向后生成list2
p = new StringObject(a[i]);
P1 = list3.CreatNode();
P1->Linkinfo(p);
list3.InsertOrder(P1);
} //升序创建list3
list1.PrintList();
cout << "list1长度:" << list1.Length() << endl;
list2.PrintList();
cout << "list2长度: " << list2.Length() << endl;
list3.PrintList();
cout << "list3长度: " << list3.Length() << endl;
list3.PrintList();
cout << "要求删除的字符串\"cat\"" << endl;
p = new StringObject(sp);
P1 = list1.Find(*p);
delete p;
if (P1 != NULL)
{
cout << "删除cat:" << endl;
P1 = list1.DeleteNode(P1);
list1.PrintList();
delete P1;
list1.PrintList();
cout << "list1长度:" << list1.Length() << endl;
}
else
cout << "未找到" << endl;
cout << "清空list1: " << endl;
list1.MakeEmpty(); //清空list1
list1.Length();
list1.PrintList();
return 0;
}
* test.cpp
*
* Created on: 2015-1-22
* Author: cainiao
*/
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
class List;
class Object
{ //数据类为抽象类
public:
Object() {} //构造函数不可为虚函数
virtual int Compare(Object &)=0; //纯虚函数
virtual void Print()=0; //纯虚函数
virtual ~Object() {}
};
//析构函数可为虚函数
class StringObject: public Object
{
char* sptr;
public:
StringObject() //缺省构造函数
{
sptr = NULL;
}
StringObject(char* s) //构造函数
{
sptr = (char *) malloc(strlen(s) + 1); //c是一个char型指针 ,如果不为其分配空间 那么将不能用strcmp 如果用c++ 怎么实现?new?
strcpy(sptr, s);
}
~StringObject() //析构函数
{
if (sptr)
{
free(sptr);
}
}
int Compare(Object & obj) //比较函数
{
StringObject &a = (StringObject &) (obj);
int c = strcmp(sptr, a.sptr);
return c;
}
void Print() //打印函数
{
cout << sptr << " -->";
}
};
class Node {
Object* info; //数据域用指针指向数据类对象
Node* link; //指针域
public:
Node() //生成头结点的构造函数
{
info = NULL;
link = NULL;
}
~Node() //析构函数
{}
void InsertAfter(Node* p) //在当前结点后插入一个结点
{
cout << "this Node" << this << endl;
p->link = this->link;
this->link = p;
}
Node* RemoveAfter()
{
Node *p = this->link;
this->link = this->link->link;
return p;
}
//删除当前结点的后继结点,返回该结点备用
void Linkinfo(Object* obj) //把数据对象连接到结点
{
this->info = obj;
}
friend class List;
};
class List
{
Node *head, *tail; //链表头指针和尾指针
public:
List() //构造函数,生成头结点(空链表)
{
head = tail = new Node;
}
~List()
{
MakeEmpty();
delete head;
} //析构函数
void MakeEmpty() //清空链表,只余表头结点
{
Node *tempNode = head;
while (tempNode)
{
tempNode = head->link;
head->link = tempNode->link;
delete tempNode;
}
tail = head;
}
Node* Find(Object & obj)
{
Node * p = head->link;
while (p)
{
if (p->info->Compare(obj))
return p;
p = p->link;
}
return NULL;
}
//搜索数据域与定值相同的结点,返回该结点的地址
int Length() //计算单链表长度
{
Node *p = head->link;
int length = 0;
while (p)
{
++length;
p = p->link;
}
return length;
}
void PrintList() //打印链表的数据域
{
Node *p = head->link;
while (p)
{
p->info->Print();
p = p->link;
}
cout << endl;
}
void InsertFront(Node* p) //可用来向前生成链表
{
p->link = this->head->link;
this->head->link = p;
}
void InsertRear(Node* p) //可用来向后生成链表
{
this->tail->link = p;
this->tail = p;
}
void InsertOrder(Node * p) //按升序生成链表
{
Node *q = this->head->link;
cout << "this->head->link " << this->head->link << endl;
Node *r = head;
if (!q)
{
head->InsertAfter(p);
} else
{
cout << q << endl;
while (q)
{
int a = q->info->Compare(*(p->info));
if (a >= 0)
{
break;
}
r = q;
q = q->link;
}
r->InsertAfter(p);
}
}
Node* CreatNode() //创建一个结点(孤立结点)
{
Node * p = new Node;
return p;
}
Node* DeleteNode(Node* q)
{
Node *p = this->head->link;
while (!(p->link->info->Compare(*(q->info))))
{
p = p->link;
}
p->link = p->link->link;
return q;
}
};
//删除指定结点
int main()
{
Node * P1;
StringObject* p;
List list1, list2, list3;
int i;
char *a[5] = { "fog", "eat", "zear", "cheep", "box" };
char *sp = "cat";
for (i = 0; i < 5; i++)
{
p = new StringObject(a[i]); //建立数据对象
P1 = list1.CreatNode(); //建立结点
P1->Linkinfo(p); //数据对象连接到结点
list1.InsertFront(P1); //向前生成list1
p = new StringObject(a[i]);
P1 = list2.CreatNode();
P1->Linkinfo(p);
list2.InsertRear(P1); //向后生成list2
p = new StringObject(a[i]);
P1 = list3.CreatNode();
P1->Linkinfo(p);
list3.InsertOrder(P1);
} //升序创建list3
list1.PrintList();
cout << "list1长度:" << list1.Length() << endl;
list2.PrintList();
cout << "list2长度: " << list2.Length() << endl;
list3.PrintList();
cout << "list3长度: " << list3.Length() << endl;
list3.PrintList();
cout << "要求删除的字符串\"cat\"" << endl;
p = new StringObject(sp);
P1 = list1.Find(*p);
delete p;
if (P1 != NULL)
{
cout << "删除cat:" << endl;
P1 = list1.DeleteNode(P1);
list1.PrintList();
delete P1;
list1.PrintList();
cout << "list1长度:" << list1.Length() << endl;
}
else
cout << "未找到" << endl;
cout << "清空list1: " << endl;
list1.MakeEmpty(); //清空list1
list1.Length();
list1.PrintList();
return 0;
}