本博客将记录链表代码(单链表),后续其他链表和其他数据结构内容请看我的其他博客
头文件(SList.h)
#pragma once
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef int SLTDataType;
struct SListNode
{
int data;
struct SListNode* next;
};
typedef struct SListNode SLTNode;
void SListPrint(SLTNode* phead);
SLTNode* BuySListNode(SLTDataType x);
void SListPushBack(SLTNode*& pphead, SLTDataType x);
void SListPushFront(SLTNode*& phead, SLTDataType x);
void SListPopBack(SLTNode*& phead);
void SListPopFront(SLTNode*& phead);
SLTNode* SListFind(SLTNode*& phead, SLTDataType x);
void SListInsert(SLTNode*& phead, SLTNode* pos, SLTDataType x);
void SListErase(SLTNode*& phead, SLTNode* pos);
源文件(SList.cpp)
#include"SList.h"
void SListPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur != NULL)
{
cout << cur->data << "->";
cur = cur->next;
}
cout << "NULL" << endl;
}
SLTNode* BuySListNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPushBack(SLTNode*& phead, SLTDataType x)//注意是引用或者指针的指针!!!
{
SLTNode* newnode = BuySListNode(x);
if (phead == NULL)
{
phead = newnode;
}
else
{
//找尾节点的指针
SLTNode* tail = phead;
while (tail->next != NULL)
{
tail = tail->next;
}
//尾节点,链接新节点
tail->next = newnode;
}
}
void SListPushFront(SLTNode*& phead, SLTDataType x)
{
SLTNode* newnode = BuySListNode(x);
newnode->next = phead;
phead = newnode;
}
void SListPopFront(SLTNode*& phead)
{
SLTNode* next = phead->next;
free(phead);
phead = next;
}
void SListPopBack(SLTNode*& phead)
{
if (phead == NULL)//空
{
return;
}
else if (phead->next == NULL)//一个节点
{
free(phead);
phead = NULL;
}
else//一个以上的节点
{
SLTNode* prev = NULL;
SLTNode* tail = phead;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
tail->next = NULL;
free(tail);
prev->next = NULL;
}
}
SLTNode* SListFind(SLTNode*& phead, SLTDataType x)
{
SListNode* cur = phead;
//while(cur!=NULL)
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在pos的前面插入x
void SListInsert(SLTNode*& phead, SLTNode* pos, SLTDataType x)
{
if (pos == phead)//判断是否是头插
{
SListPushFront(phead, x);
}
else
{
SLTNode* prev = phead;
while (prev->next != pos)
{
prev = prev->next;
}
SLTNode* newnode=BuySListNode(x);
prev->next = newnode;
newnode ->next= pos;
}
}
//删除pos位置的值
void SListErase(SLTNode*& phead,SLTNode* pos)
{
if (pos ==phead)//判断是不是头删
{
SListPopFront(phead);
}
else
{
SLTNode* prev = phead;
while (prev->next!=pos)
{
prev = prev ->next;
}
prev->next = pos->next;
free(pos);
}
}
测试文件(Test.cpp)
#include"SList.h"
void TestSList1()
{
SLTNode* phead = NULL;
SListPushBack(phead, 1);
SListPushBack(phead, 2);
SListPushBack(phead, 3);
SListPushBack(phead, 4);
SListPrint(phead);
SListPushFront(phead, 0);
SListPrint(phead);
SListPushFront(phead, -1);
SListPopFront(phead);
SListPopFront(phead);
SListPrint(phead);
SListPopBack(phead);
SListPopBack(phead);
SListPrint(phead);
//在1前面加一个10
SLTNode* pos1 = SListFind(phead,1);
SListInsert(phead, pos1, 10);
SLTNode* pos2 = SListFind(phead, 2);
SListInsert(phead, pos2, 20);
SLTNode* pos3 = SListFind(phead, 3);
SListInsert(phead, pos3, 30);
SListPrint(phead);
SLTNode* pos10 = SListFind(phead, 10);
SListErase(phead, pos10);
SLTNode* pos20 = SListFind(phead, 20);
SListErase(phead, pos20);
SListPrint(phead);
}
int main()
{
TestSList1();
return 0;
}
代码演示
如果这个博客对你有帮助,给博主一个免费的点赞就是最大的帮助❤
欢迎各位点赞,收藏和关注哦❤
如果有疑问或有不同见解,欢迎在评论区留言❤
后续会继续更新大连理工大学相关课程和有关数据结构的内容和代码
点赞加关注,学习不迷路,好,本次的学习就到这里啦!!!
我们下次再见!