昨天把链表实现代码写到 写链表收获 里了,今天看了看居然没有,不知道是没保存还是怎么了,没办法,今天就把自己的代码再粘一遍吧
对了,是用vs2013编辑的
SLNode.cpp
#include<iostream>
struct SLNode{
int data;
SLNode *next;
SLNode(SLNode* nextNode = NULL){ //默认构造函数
data = NULL;
next = nextNode;
}
SLNode(const int& item, SLNode* nextNode = NULL){ //构造函数
data = item;
next = nextNode;
}
};
SLList.h
#ifndef HEAD
#define HEAD
#include"SLNode.cpp" //链表节点定义
using namespace std;
class SLList{
private:
SLNode* head; //表头指针
public:
SLList(){
head = new SLNode(); //构造函数,构造一个只有头节点的空链表
}
SLList(const int& item){
head = new SLNode(item);
}
~SLList(){};
void InsertHead(const int& item); //插入
void InsertTail(const int& item);
void Insert(int location, const int& item);
int Search(int& item); //查找
int Find(int location); //存取
void DeleteHead(); //删除
void DeleteTail();
void Delte(int location);
void Print(); //输出链表
};
#endif
SLList.cpp
#include"SLList.h"
void SLList::InsertHead(const int& item){ //表头插入节点
if (head->data == NULL){ //表头为空的情况
head->data = item;
cout << "插入表头" << "数据为:" << item << endl;
return;
}
SLNode* p=new SLNode(item,head);
head = p;
cout << "插入表头" << "数据为:" << item << endl;
}
void SLList::InsertTail(const int& item){ //表尾插入
SLNode* p;
p = head;
if (p->data == NULL){ //表头为空的情况
p->data = item;
cout << "插入表尾" << "数据为:" << item << endl;
return;
}
while ( p->next != NULL)
p = p->next;
SLNode* q = new SLNode(item);
p->next = q;
cout << "插入表尾" << "数据为:" << item << endl;
}
void SLList::Insert(int location, const int& item){
if (location <= 0){ //插入位置太小
cout << "插入位置无效"<< endl;
return;
}
if (head->data == NULL){
cout <<"链表为空" << endl;
}
SLNode* p;
p = head;
int i=1;
while (i < location&&p->next!=NULL){
p = p->next;
i++;
}
if (i < location){ //插入位置太大
cout << "链表长度不足"<< endl;
return;
}
SLNode* q = new SLNode(item);
q->next = p->next;
p->next = q;
cout << "第" << location+1 << "个节点插入成功," << "插入数据为:" << item << endl;
}
int SLList::Search(int& item){ //查找操作
int location=1;
SLNode* p;
p = head;
while (p->data != item&&p!=NULL){
location += 1;
p = p->next;
}
if (p == NULL){ //到了表尾
cout << "无此节点"<< endl;
return 0;
}
cout << "数据为"<<item<<"的节点位置为:"<<location<< endl;
return location;
}
int SLList::Find(int location){ //存取
SLNode* p;
p = head;
int i = 1;
while (i < location&&p->next != NULL){
p = p->next;
i++;
}
if (i < location){ //没找到
cout << "链表长度不足" << endl;
return 0;
}
int item;
item = p->data;
cout << "第" <<location << "个节点的数据为:" << item << endl;
return item;
}
void SLList::DeleteHead(){
if (head->data == NULL){ //链表为空则不操作
cout << "链表为空" << endl;
return;
}
else if (head->next == NULL){ //只有一个头结点
cout << "头结点的数据为:"<<head->data << endl;
delete head;
cout << "链表已删除" << endl;
return;
}
else{
SLNode* p;
p = head;
cout << "头结点的数据为:" << head->data << endl;
head = head->next;
delete p;
cout << "已删除,新的头结点数据为:"<<head->data << endl;
return;
}
}
void SLList::DeleteTail(){
SLNode* p,*q; //q指向p的前一个节点,删除p指向的节点用
p = q = head;
if (p->data == NULL){ //处理表头为空的情况
cout << "此链表为空"<< endl;
return;
}
if (head->next == NULL){ //删除到只剩头结点时
cout << "最后一个节点(表头)的数据为:"<<head->data<< endl;
delete head;
cout << "表头已被删除,请勿再删除"<< endl;
return;
}
while (p->next != NULL){
q = p;
p = p->next;
}
q->next = NULL;
cout << "被删除的表尾数据为:"<<p->data << endl;
delete p;
}
void SLList::Delte(int location){
if (location <= 0){
cout << "删除位置无效" << endl;
return;
}
if (head->data == NULL){
cout << "链表为空" << endl;
}
SLNode *p,*q; //q指向p的前一个节点,删除p指向的节点用
p = q = head;
int i = 1;
while (i < location&&p->next != NULL){
q = p;
p = p->next;
i++;
}
if (i < location){ //删除节点位置过大
cout << "链表长度不足" << endl;
return;
}
q->next = p->next;
cout << "第"<<location<<"个节点的数据为:"<<p->data << endl;
delete p;
cout << "已删除"<< endl;
}
void SLList::Print(){ //输出整个链表
SLNode* p;
p = head;
if (p->data == NULL){
cout <<"空链表" << endl;
return;
}
while (p->next != NULL){ //输出加格式控制
cout << "[" << p->data << "]->";
p = p->next;
}
cout << "[" << p->data << "]->" << "NULL" << endl;
}
main.cpp
#include"SLList.h"
int main(){
SLList alist; //创建一个空链表
cout << "已创建一个名为 alist 的空链表" << endl;
int i;
while (true){
cout <<"请选择:" << endl;
cout << "1.插入 2.删除 3.存取 4.查找 5.输出链表 6.退出(或其他任意键)" << endl;
cin >> i;
if (i == 1){ //做插入操作
cout << "请选择插入位置:" << endl;
cout << "1.表头 2.表尾 3.其他" << endl;
int j;
cin >> j;
if (j == 1){ //表头插入
cout << "请输入数据:" << endl;
int item;
cin >> item;
alist.InsertHead(item);
}
else if (j == 2){ //表尾插入
cout << "请输入数据:" << endl;
int item;
cin >> item;
alist.InsertTail(item);
}
else if (j == 3){ //指定位置处插入
cout << "请输入位置,数据将插在此位置之后:" << endl;
int location;
cin >> location;
cout << "请输入数据:" << endl;
int item;
cin >> item;
alist.Insert(location, item);
}
else
break;
}
else if (i == 2){ //做删除操作
cout << "请选择要删除的位置:"<< endl;
cout << "1.表头 2.表尾 3.其他" << endl;
int j;
cin >> j;
if (j == 1){ //表头删除
alist.DeleteHead();
}
else if (j == 2){ //表尾删除
alist.DeleteTail();
}
else if (j == 3){ //指定位置删除
cout << "请输入要删除节点的位置" << endl;
int location;
cin >> location;
alist.Delte(location);
}
else
break;
}
else if (i == 3){ //做存取操作
cout << "请输入要存取的节点的位置:"<< endl;
int location;
cin >> location;
alist.Find(location);
}
else if (i == 4){ //做查找操作
cout << "请输入要查找的数据:"<< endl;
int item;
cin >> item;
alist.Search(item);
}
else if (i == 5){ //输出整个链表
alist.Print();
}
else //退出
break;
}
return 0;
}