1链表 类声明。
节点类(用来定义节点)
class Node {
public:
int val;
Node *next;
};
链表类(用来定义链表和其实现算法)
class List {
public:
List();//构造函数
Node * next;//链表节点
void creatheadList(int val);//头插法创建一个链表
void creattailList(int val);//尾插法创建一个链表
void showList();//显示打印链表
int getlenList();//得到链表的长度
int getindexval(int index);//返回第index节点的值
int getvalindex(int val);//返回值为val的节点的索引
void insertList(int val, int index);//第index个节点插入值为val的节点
void deleteList(int index);//删除第index个节点
void reverseList();//链表反转
//void correctList(int index, int val);//将第index个节点的值改为val
void emptyList();//清空链表
private:
Node* returnindex(int index);//返回第index个节点
Node* head;
};
2 链表 类实现
//以下为实现///
/*构造函数*/
List::List() {
head = new Node;
head->next = NULL;
}
/*头插法创建链表*/
void List::creatheadList(int val) {
Node *newnode = new Node;
newnode->val = val;
newnode->next = head->next;
head->next =newnode;
cout<<"元素"<<val<<"插入成功"<<endl;
}
/*尾插法创建链表*/
void List:: creattailList(int val) {
/*temp指向尾巴*/
int len = getlenList();
Node *temp = returnindex(len);
Node *newnode;
newnode = new Node;
newnode->val = val;
newnode->next = NULL;
temp->next = newnode;
temp = newnode;
cout << "元素" << val << "插入成功" << endl;
}
/*遍历链表*/
void List:: showList() {
Node *temp = head->next;
while (temp) {
cout << temp->val << endl;
temp = temp->next;
}
cout << "链表L打印成功"<<"其共有"<<getlenList() <<"个元素" << endl;
}
/*得到链表的长度*/
int List:: getlenList() {
Node*temp = head->next;
int len = 0;
while (temp) {
len++;
temp = temp->next;
}
return len;
}
/*返回第index节点的值*/
int List::getindexval(int index) {
if (index < 1 || index>getlenList()) {
cout << "索引无效" << endl;
return -1;
Node *temp = returnindex(index);
return temp->val;
}
}
/*返回值为val的节点的索引*/
int List:: getvalindex(int val) {
Node *temp = head->next;
int count = 0;
if (head->next) {
return -1;
cout<<"此表为空表"<<endl;
}
while (temp) {
count++;
if (temp->val == val) {
return count;
}
else {
cout<<"链表中无该值"<<endl;
return -2;
}
}
}
/*第index个节点插入值为val的节点*/
void List:: insertList(int index, int val) {
if (index<1 || index>getlenList()) {
cout<<"索引无效"<<endl;
}
Node* temp = returnindex(index-1);
Node *newnode = new Node;
newnode->val = val;
newnode->next = temp->next;
temp->next = newnode;
cout<<"插入成功"<<endl;
}
/*删除第index个节点*/
void List:: deleteList(int index) {
if (index<1 || index>getlenList()) {
cout << "索引无效" << endl;
}
Node *temp = returnindex(index-1);
temp->next = temp->next->next;
cout << "删除成功" << endl;
}
/*返回第index个节点*//注意这里是内联函数
inline Node* List::returnindex(int index) {
if (index<1 || index>getlenList()) {
cout << "索引无效" << endl;
}
Node *temp = head->next;
int j = 1;
while (j < index) {
j++;
temp = temp ->next;
}
return temp;
}
void List::reverseList() {
if (!head->next) {
cout<<"该链表为空"<<endl;
return;
}
Node *p = head->next;
Node *t;
Node *q = head->next->next;
while (q!=NULL) {
t = q->next;
q->next = p;
p = q;
q = t;
}
head->next->next = NULL;
head->next = p;
cout<<"反转成功"<<endl;
}
void List::emptyList() {
if (!head->next) {
cout<<"此链表为空"<<endl;
}
Node *temp = head->next;
while (head->next) {
temp = head->next->next;
free(head->next);
head->next = temp;
}
}
3 主函数测试
int main() {
List();
List L1;
L1.creatheadList(9);
system("pause");
L1.creatheadList(8);
system("pause");
L1.creatheadList(7);
system("pause");
L1.creatheadList(6);
system("pause");
L1.creatheadList(5);
system("pause");
L1.creattailList(4);
system("pause");
L1.creattailList(3);
system("pause");
L1.creattailList(2);
system("pause");
L1.showList();
system("pause");
int a = L1.getindexval(5);
cout<< a <<endl;
L1.getindexval(20);
system("pause");
L1.insertList(2, 25);
system("pause");
L1.showList();
system("pause");
L1.deleteList(2);
system("pause");
L1.showList();
system("pause");
L1.reverseList();
system("pause");
L1.showList();
system("pause");
L1.emptyList();
system("pause");
L1.emptyList();
system("pause");
return 0;
}