#pragma once
#include<iostream>
using namespace std;typedef int SLDataType;
class SList
{
public:
class Node//定义结点
{
public:
SLDataType data;//值域
Node* next;//指针域
};
Node* head;//头结点
public:
Node* BuySListNode(SLDataType x);//开辟新结点
void SListInit();//初始化
void SListPrint();//打印
void SListPushBack(SLDataType x);//尾插
void SListPushFront(SLDataType x);//头插
void SListPopBack();//尾删
void SListPopFront();//头删
Node* SListFind(SLDataType x);//查找
void SListInsert(Node* pos, SLDataType x);//在pos的前面插入x
void SListErase(Node* pos);//删除pos位置的值
void SListDestroy();//销毁链表
};
#include"SList.h"
SList::Node* SList::BuySListNode(SLDataType x)//开辟新结点
{
Node* newnode = new Node;
newnode->data = x;
newnode->next = nullptr;
return newnode;
}void SList::SListInit()//初始化
{
head = BuySListNode(0);
}void SList::SListPrint()//打印
{
Node* cur = head->next;
while (cur != nullptr)
{
cout << cur->data << "->";
cur = cur->next;
}
cout << "nullptr" << endl;
}void SList::SListPushBack(SLDataType x)//尾插
{
Node* newnode = BuySListNode(x);if (head->next == nullptr)
{
head->next = newnode;
}
else
{
//找到尾结点的指针
Node* tail = head->next;
while (tail->next != nullptr)
{
tail = tail->next;
}
//尾结点,链接新结点
tail->next = newnode;
}}
void SList::SListPushFront(SLDataType x)//头插
{
Node* newnode = BuySListNode(x);if (head->next == nullptr)
{
head->next = newnode;
}
else
{
Node* first = head->next;//首元结点
head->next = newnode;
newnode->next = first;
}
}void SList::SListPopBack()//尾删
{
if (head->next == nullptr)
{
return;
}
else if(head->next->next == nullptr)
{
delete head->next;
head->next = nullptr;
}
else
{
Node* tail = head->next;//找到尾结点的指针
Node* prev = nullptr;//找到尾结点的前驱的指针
while (tail->next != nullptr)
{
prev = tail;
tail = tail->next;
}
delete tail;
prev->next = nullptr;
}
}void SList::SListPopFront()//头插
{
if (head->next == nullptr)
{
return;
}
else
{
Node* first = head->next;//首元结点
Node* second = head->next->next;
delete first;
head->next = second;
}
}SList::Node* SList::SListFind(SLDataType x)//查找
{
Node* cur = head->next;
while (cur != nullptr)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return nullptr;
}void SList::SListInsert(Node* pos, SLDataType x)//在pos的位置之前插入
{
if (head->next == pos)
{
SListPushFront(x);
}
else
{
Node* newnode = BuySListNode(x);
Node* prev = head->next;//找到pos位置的前驱
while (prev->next != pos)
{
prev = prev->next;
}prev->next = newnode;
newnode->next = pos;
}
}void SList::SListErase(Node* pos)//删除pos位置的值
{
if (pos == nullptr)
{
return;
}
if (head->next == pos)
{
SListPopFront();
}
else
{
Node* prev = head->next;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
delete pos;
}
}void SList::SListDestroy()//销毁链表
{
Node* cur = head->next;
while (cur != nullptr)
{
Node* next = cur->next;
delete cur;
cur = next;
}
delete head;
head->next = nullptr;
}
#include"SList.h"
void test01()
{
SList st;
st.SListInit();
st.SListPushBack(1);
st.SListPushBack(2);
st.SListPushBack(3);
st.SListPushBack(4);
st.SListPrint();
st.SListPopFront();
st.SListPopBack();
st.SListPopBack();
st.SListPrint();
}void test02()
{
SList st;
st.SListInit();
st.SListPushFront(1);
st.SListPushFront(2);
st.SListPopFront();st.SListPushFront(3);
st.SListPushFront(4);
st.SListPopFront();
st.SListPrint();
st.SListPopFront();
st.SListPopFront();
st.SListPopFront();
st.SListPrint();
}void test03()
{
SList st;
st.SListInit();
st.SListPushBack(1);
st.SListPushBack(2);
st.SListPushBack(3);
st.SListPrint();SList::Node* pos = st.SListFind(1);
st.SListInsert(pos, 0);
st.SListInsert(nullptr, 4);
st.SListPrint();
st.SListErase(pos);
st.SListPrint();
st.SListDestroy();
st.SListPrint();
}int main()
{
//test01();
//test02();
test03();
}