#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 SListPopFront();//头删
void SListPopBack();//尾删
void SListInsert(Node* pos,SLDataType x);//在pos的前面插入x
void SListErase(Node* pos);//删除pos位置的值
Node* SListFind(SLDataType x);//查找
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 = nullptr;
}void SList::SListPrint()//打印
{
Node* cur = head;
while (cur != nullptr)
{
cout << cur->data << "->";
cur = cur->next;
}
cout << "nullptr" << endl;
}void SList::SListPushBack(SLDataType x)//尾插
{
Node* newnode = BuySListNode(x);if (head == nullptr)
{
head = newnode;
}
else
{//找到尾结点的指针
Node* tail = head;
while (tail->next != nullptr)
{
tail = tail->next;
}
//尾结点,链接新结点
tail->next = newnode;
}
}
void SList::SListPushFront(SLDataType x)//头插
{
Node* newnode = BuySListNode(x);
newnode->next = head;
head = newnode;
}void SList::SListPopFront()//头删
{
if (head == nullptr)
{
return;
}
Node* next = head->next;
delete head;
head = next;
}void SList::SListPopBack()//尾删
{
if (head == nullptr)
{
return;
}
else if (head->next == nullptr)
{
delete head;
head = nullptr;
}
else
{
Node* tail = head;
Node* prev = nullptr;
while (tail->next != nullptr)
{
prev = tail;
tail = tail->next;
}
delete tail;
prev->next = nullptr;
}
}SList::Node* SList::SListFind(SLDataType x)
{
Node* cur = head;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return nullptr;
}void SList::SListInsert(Node* pos,SLDataType x)//在pos的前面插入x
{
if (pos == head)
{
SListPushFront(x);
}
else
{
Node* newnode = BuySListNode(x);Node* prev = head;
while (prev->next != pos)
{
prev = prev->next;
}prev->next = newnode;
newnode->next = pos;
}
}void SList::SListErase(Node* pos)//删除pos位置的值
{
if (pos == head)
{
SListPopFront();
}
else
{
Node* prev = head;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
delete pos;
}
}void SList::SListDestroy()
{
Node* cur = head;
while (cur != nullptr)
{
Node* next = cur->next;
delete cur;
cur = next;
}
head = 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.SListDestroy();
st.SListPrint();
}void test02()
{
SList st;
st.SListInit();
st.SListPushFront(1);
st.SListPushFront(2);
st.SListPushFront(3);
st.SListPushFront(4);
st.SListPrint();
SList::Node* pos = st.SListFind(1);if (pos)
{
st.SListErase(pos);
}
st.SListPrint();pos = st.SListFind(2);
if (pos)
{
st.SListErase(pos);
}
st.SListPrint();pos = st.SListFind(3);
if (pos)
{
st.SListErase(pos);
}
st.SListPrint();pos = st.SListFind(4);
if (pos)
{
st.SListErase(pos);
}
st.SListPrint();
}int main()
{
test01();
}