基本都加上注释了,所有函数在本地运行的功能都可以实现 代码如下:
#include <bits/stdc++.h>
using namespace std;
#define MAXSIZE 500
#define stop systeam("pause");
struct Node
{
int data;
Node *next;
};
class ChainList
{
public:
ChainList();
~ChainList();
void CreateChainList();
int GetChainList();
void ShowChainList();
bool EmptyChainList();
Node *FindChainList(int data);
void InsertElemAtHead(int data);
void InsertElemAtEnd(int data);
void InsertElemAtIndex(int data, int n);
void DeleteHead();
void DeleteEnd();
void DeleteIndex(int data);
void DeleteAll();
void nume();
private:
Node *head;
};
ChainList::ChainList()
{
head = new Node;
head->data = 0;
head->next = NULL;
}
ChainList::~ChainList()
{
delete head;
}
void ChainList::CreateChainList()
{
Node *pnew,*ptemp;
ptemp=this->head;
int n; cout<<"需要的结点个数:"<<endl;
cin>>n;
for(int i=0;i<n;i++){
pnew = new Node;
cout<<"请输入第"<<i+1<<"结点的值"<<endl;
cin>>pnew->data;
ptemp->next = pnew;
pnew->next = NULL;
ptemp = pnew;
}
cout<<"--链表创建完成--"<<endl;
}
int ChainList::GetChainList()
{
if(this->EmptyChainList()){
cout<<"--链表为空--"<<endl;
}
else{
int count = 0;
Node *p = this->head;
while(p->next!=NULL){
p = p->next;
count++;
}
return count;
}
}
void ChainList::ShowChainList()
{
if(this->EmptyChainList()){
cout<<"--链表为空--"<<endl;
}
else{
Node *p = this->head;
int i = 1;
while(p->next!=NULL){
p = p->next;
cout<<"第"<<i<<"个结点:"<<p->data<<endl;
i++;
}
}
}
bool ChainList::EmptyChainList()
{
if(this->head->next==NULL){
return true;
}
return false;
}
Node* ChainList::FindChainList(int data)
{
if(this->EmptyChainList()){
cout<<"--链表为空--"<<endl;
}
else{
Node* p = this->head;
int i = 0;
while (data!=i){
p = p->next;
i++;
}
return p;
}
}
void ChainList::InsertElemAtHead(int data)
{
Node*p = this->head;
Node*pnew = new Node;
pnew->data = data;
pnew->next = p->next;
p->next = pnew;
}
void ChainList::InsertElemAtEnd(int data)
{
Node *pnew = new Node;
pnew->data = data;
pnew->next = NULL;
Node* p = this->head;
while (p->next != NULL)
{
p = p->next;
}
p->next = pnew;
}
void ChainList::InsertElemAtIndex(int data, int n)
{
if (n<1 || n>this->GetChainList())
cout << "--erro输入异常--" << endl;
if (n == 1){
Node *p = this->head;
Node*pnew = new Node;
pnew->data = data;
pnew->next = p->next;
p->next = pnew;
}
else{
int i = 0;
Node *p = this->head;
while (n > i){
p = p->next;
i++;
}
Node*pnew = new Node;
pnew->data = data;
pnew->next = p->next;
p->next = pnew;
}
}
void ChainList::DeleteHead()
{
if (this->EmptyChainList()){
cout << "空链表!" << endl;
}
else{
Node*p = this->head;
Node *ptemp;
p = p->next;
ptemp = p->next;
delete p;
p = NULL;
head->next = ptemp;
}
}
void ChainList::DeleteEnd()
{
Node *p= this->head;
Node* ptemp=NULL;
if (this->EmptyChainList()){
cout << "空链表!" << endl;
}
else{
while (p->next != NULL){
ptemp = p;
p = p->next;
}
delete p;
ptemp->next = NULL;
}
}
void ChainList::DeleteIndex(int data)
{
if (this->EmptyChainList())
{
cout << "空链表!" << endl;
}
else{
Node*ptemp =this->FindChainList(data);
if (ptemp == this->head->next)
{
DeleteHead();
}
else{
Node*p = this->head;
while (p->next != ptemp)
{
p = p->next;
}
p->next = ptemp->next;
delete ptemp;
ptemp = NULL;
}
}
}
void ChainList::DeleteAll()
{
Node*p = this->head->next;
Node* ptemp = NULL;
if (this->EmptyChainList()){
cout << "空链表!" << endl;
}
else{
while (p == NULL){
ptemp = p;
p = p->next;
this->head->next = p;
ptemp->next = NULL;
delete ptemp;
}
this->head->next = NULL;
cout<<"--删除成功--"<<endl;
}
}
void ChainList::nume()
{
while(1){
cout<<"1.void CreateChainList(); // 创建链表"<<endl;
cout<<"2.int GetChainList(); // 获取链表长度"<<endl;
cout<<"3.void ShowChainList(); // 显示链表"<<endl;
cout<<"4.bool EmptyChainList(); // 判断链表是否为空"<<endl;
cout<<"5.Node *FindChainList(int data); //查找链表中一个位次为data的结点,并且将他的结点指针返回"<<endl;
cout<<"6.void InsertElemAtHead(int data); //在头部插入元素"<<endl;
cout<<"7.void InsertElemAtEnd(int data); //在尾部插入指定的元素"<<endl;
cout<<"8.void InsertElemAtIndex(int data, int n); //在指定位置插入指定元素"<<endl;
cout<<"9.void DeleteHead(); //头部删除"<<endl;
cout<<"10.void DeleteEnd(); //尾部删除"<<endl;
cout<<"11.void DeleteIndex(int data); 删除链表中第data的结点"<<endl;
cout<<"12.void DeleteAll(); //删除所有数据 "<<endl;
int num; cin>>num;
int data;
int n;
Node *p = new Node;
switch(num){
case 0: system("cls");
cout<<"==2s后退出程序=="<<endl; _sleep(2000);
return;
case 1: system("cls");
CreateChainList();
break;
case 2: system("cls");
cout<<"链表长度为:"<<GetChainList()<<endl;
break;
case 3: system("cls");
ShowChainList();
break;
case 4: system("cls");
if(EmptyChainList()){
cout<<"链表为空"<<endl;
}
else{
cout<<"链表不为空"<<endl;
}
break;
case 5: system("cls");
cout<<"请输入需要查找的结点: "<<endl;
cin>>data;
p = FindChainList(data);
cout<<"该结点的数据为:"<<p->data<<endl;
break;
case 6: system("cls");
cout<<"输入需要插入的元素"<<endl;
cin>>data;
InsertElemAtHead(data);
break;
case 7: system("cls");
cout<<"输入需要插入的元素"<<endl;
cin>>data;
InsertElemAtEnd(data);
break;
case 8: system("cls");
cout<<"输入需要插入的元素"<<endl;
cin>>data;
cout<<"输入元素位置"<<endl;
cin>>n;
InsertElemAtIndex(data,n);
break;
case 9: system("cls");
DeleteHead();
break;
case 10: system("cls");
DeleteEnd();
break;
case 11: system("cls");
cout<<"输入需要删除的位置"<<endl;
cin>>data;
DeleteIndex(data);
break;
case 12: system("cls");
DeleteAll();
break;
}
}
}
int main(void)
{
ChainList CL;
CL.nume();
return 0;
}
|