数据结构单链表的实现
/*类的头文件*/
#ifndef SLLIST_H
#define SLLIST_H
struct SLNode{
int data;
SLNode *next;
};
class SLList{
private:
SLNode *head;
public:
SLList();
SLList(int &item);
virtual ~SLList();
bool isEmpty()const;
int length()const;
int Find(int k, int item);
void insert(int k, int item);
void Delete(int item);
int Search(const int &item) const;
void output()const;
void Reverse();
};
#endif // SLLIST_H
/*类的实现文件*/
#include"SLList.h"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
SLList::SLList() {
head = new SLNode();
head -> next = NULL;
}
SLList::SLList(int &item) {
SLNode *node = new SLNode();
node -> data = item;
head = new SLNode();
head -> next = node;
node -> next = NULL;
}
SLList::~SLList() {
SLNode *cur = head, *ptr = head->next;// cur 指向当前结点,ptr指向当前结点的next域
while(ptr != NULL) {//
if(cur == head) {//对于链表的头指针,是不能删的
cur = head->next;
ptr = ptr->next;
} else {//f否则的话就把当前结点的下一个结点
cur = ptr;
ptr = ptr->next;
delete cur;
}
}
}
bool SLList::isEmpty() const{
if(head->next == NULL) {
return true;
}
return false;
}
int SLList::length()const {
int len = 0;
SLNode *ptr = head;
while(ptr->next != NULL) {
ptr = ptr->next;
len++;
}
return len;
}
int SLList::Find(int k, int item) {//存取,将链表中第k个结点的字段值赋给item
int cnt = 0;
SLNode *ptr = head;
while(ptr->next != NULL) {
ptr = ptr->next;
cnt++;
if(cnt == k) {
item = ptr->data;
cout<<"存取成功"<<endl;
return item;
}
}
cout<<"存取失败"<<endl;
}
void SLList::insert(int k, int item) {//在第k个结点后面插入字段值item的结点
SLNode *node = new SLNode();
node->data = item;
node->next = NULL;
if(k == 0) {
head->next = node;
// node->next = NULL;
cout<<"插入成功"<<endl;
return;
} else {
SLNode *ptr = head, *temp = NULL;
int cnt = 0;
while(ptr->next != NULL) {
cnt++;
ptr = ptr->next;
if(cnt == k) {//说明找到了第k个结点,就是ptr
temp = ptr->next;
ptr->next = node;
node->next = temp;
cout<<"插入成功"<<endl;
return;
}
}
}
cout<<"插入失败"<<endl;
}
void SLList::Delete(int item) {///删除链表中值为item的所有元素
SLNode *ptr = head, *pre = head, *cur = NULL;
while(ptr->next != NULL) {
pre = ptr;
cur = ptr->next;
if(cur->data == item) {
pre->next = cur->next;
delete cur;
cout<<"成功删除一个元素"<<endl;
ptr = pre;
} else {
ptr = cur;
}
}
}
int SLList::Search(const int& item)const {
int cnt = 0;
SLNode *ptr = head;
while(ptr->next != NULL) {
ptr = ptr->next;
cnt++;
if(ptr->data == item) {
return cnt;
}
}
return 0;
}
void SLList::output()const {
SLNode *ptr = head;
while(ptr->next != NULL) {
if(ptr == head) {
cout<<"^";
} else {
cout<<ptr->data;
}
ptr = ptr->next;
cout<<"->";
}
if(length() != 0) {
cout<<ptr->data<<endl;
} else {
cout<<"^"<<endl;
}
}
void SLList::Reverse() {
SLNode *ptr = head, *pre = head, *cur = NULL, *next = NULL;
while(ptr != NULL) {
cur = ptr;
next = cur->next;
if(pre == head) {
cur->next = NULL;
} else if(next == NULL) {
head->next = cur;
cur->next = pre;
} else {
cur->next = pre;
}
ptr = next;
pre = cur;
}
}
/*用户界面*/
#include<cstdio>
#include<iostream>
#include"SLList.h"
#include"SLList.cpp"
using namespace std;
int main() {
int n;
cout<<"单链表的测试实验"<<endl;
cout << "请输入线性表的长度n" << endl;
cin >> n;
int number;
SLList *list_l = new SLList();
for(int i = 0; i < n; ++ i) {
cout << "请输入第" << i+1 << "个数" << endl;
cin >> number;
list_l->insert(i, number);
}
cout<<"单链表创建完毕"<<endl;
int choose, pos, item;
while(true) {
cout<<"请选择你想测试的功能\n0.结束测试\n1.判断单链表是否为空\n2.返回单链表的长度\n3.对单链表执行存取操作\n4.向单链表中插入一个元素\n";
cout<<"5.删除链表中值为item的所有元素\n6.查找item在单链表中第一次出现的位置\n7.输出当前的单链表\n8.翻转当前的单链表\n";
cin>>choose;
if(choose == 0) {
break;
}
switch(choose) {
case 1:
if(list_l->isEmpty()) {
cout<<"链表为空"<<endl;
} else {
cout<<"链表不为空"<<endl;
}
break;
case 2:
cout<<"链表的长度为:"<<list_l->length()<<endl;
break;
case 3:
cout<<"请输入你想存取的元素的位置"<<endl;
cin>>pos;
item = list_l->Find(pos, item);
cout<<"第"<<pos<<"个元素的值为"<<item<<endl;
break;
case 4:
cout<<"请输入你想插入的元素的位置和元素的值"<<endl;
cin>>pos>>item;
list_l->insert(pos, item);
break;
case 5:
cout<<"请输入你想删除的元素的值"<<endl;
cin>>item;
list_l->Delete(item);
break;
case 6:
cout<<"请输入你想查找的元素的值"<<endl;
cin>>item;
pos = list_l->Search(item);
if(pos == 0) {
cout<<"你查找的元素不在链表中"<<endl;
} else {
cout<<"你要查找的元素在链表中的位置为"<<pos<<endl;
}
break;
case 7:
list_l->output();
break;
case 8:
list_l->Reverse();
break;
}
}
return 0;
}