您的位置:首页 > 编程语言 > C语言/C++

[c++]单链表(class)

2015-12-09 22:20 363 查看
//list.h
#include<iostream>
typedef char Type;
using namespace std;
class List
{
public:
List *head;
~List();
List(Type body, List *add = NULL);
List();
void Creat(List *&head);
void Insert();
void Delete(List *&head);
void Print();
void Release();
private:
bool search(Type item, List *&p);
Type body;
List *next;
Type item;
};
</pre><pre name="code" class="cpp">//list.cpp
#include<iostream>
#include"list.h"
using namespace std;
typedef char Type;
//********************************************************************
void List::Creat(List *&head)//生成函数
{
int n;
cout << "please enter the length of the list: " << endl;
cin >> n;
Type *temp = new Type
;
cout << "please enter the content of the list" << endl;
for (int i = 0; i < n;i++)  cin >> temp[i];
List *p = NULL;
this->head = new List(temp[0]);
p = this->head;
for (int i = 1; i < n; i++)
{
p->next = new List(temp[i]);
p = p->next;
}
p->next = NULL;
cout << "Save succeed!" << endl;
}
//**********************************************************************
bool List::search(Type item, List *&p)//搜索函数
{p = new List('0');
p->next = this->head;
List *temp = this->head;
if (temp->body == item)
return true;
else
{
p = this->head;
temp = p->next;
}
while (p&&temp->body != item)
{
p = p->next;
List *temp = p->next;
}
if (p) return true;
else return false;
}
//**********************************************************************
void List::Insert()//插入函数
{
Type n;
cout << "please enter the item you want to add on: " << endl;
cin >> n;
List *p = NULL;
if (search(n,p))
{
p = p->next;
Type m;
cout << "please enter the item you want to add: " << endl;
cin >> m;
List *q = new List(m,p->next);
p->next = q;
cout << "Insert success!" << endl;
}
else cout << "Don't have this item." << endl;
}
//***********************************************************************
void List::Delete(List *&head)//删除函数
{
Type n;
cout << "please enter the item you want to delete:" << endl;
cin >> n;
List *p = NULL;
if (this->search(n,p)&&head->body==n)
{
List *q = p->next;
head = q->next;
p->next = head;
delete q;
cout << "Delete success!" << endl;
}
else if (this->search(n, p))
{
List *q = p->next;
p->next = q->next;
delete q;
cout << "Delete success!" << endl;
}
else cout << "Don't have this item." << endl;
}
//************************************************************************
void List::Print()//输出函数
{
List *p = this->head;
while (p)
{
cout << p->body<<' ';
p = p->next;
}
cout << endl << "Print Succeed!" << endl;
}
//************************************************************************
void List::Release()//释放函数
{
List *p = this->head;
while (p) {
head = head->next;
delete p;
}
cout << "Release succeed!" << endl;
}
//************************************************************************
List::List(Type body, List *add)//构造函数
{
this->body = body;
this->next = add;
}

//**************************************************************************
List::~List()//析构函数
{
next = NULL;
}
//****************************************************************
List::List()
{
}
//main.cpp
#include<iostream>
#include"list.h"
using namespace std;
typedef char Type;
void main()
{
cout << "**************************************MENU*************************************" << endl;
cout << "1.Creat a list." << endl;
cout << "2.Insert a item." << endl;
cout << "3.Delete a item." << endl;
cout << "4.Print the list." << endl;
cout << "-1.END" << endl;
cout << "FBI WARNING: please press '0'to release before end the programm!" << endl;
List list1;
int n=1;
while (n!=-1)
{
cout << "please choose the number above." << endl;
cin >> n;
switch (n)
{
case 1:
list1.Creat(list1.head);
break;
case 2:
list1.Insert();
break;
case 3:
list1.Delete(list1.head);
break;
case 4:
list1.Print();
break;
case 0:
list1.Release();
default:
cout << "Wrong Input" << endl;
break;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: