単一チェーンテーブル(class)

4828 ワード

//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[n];
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;
		}
	}
}