🐱🚀个人博客https://blog.csdn.net/qq_51000584/
🐱👤收录专栏:数据结构和算法
🐱👓专栏目标:分享一些学习的代码
🐱💻作者:敲代码的猫(Codemon)
c++版本
//单向链表
//节点
typedef int DATA;
struct Node {
Node() {
this->pNext = nullptr;
}
Node(DATA t_data) {
this->_data = t_data;
this->pNext = nullptr;
}
~Node() {}
Node* pNext;
DATA _data;
};
class List {
public:
List() {
this->length = 0;
this->pHead = nullptr;
this->pTail = nullptr;
}
~List() {
this->clear();
}
public:
//尾添加
void push_back(DATA t_data) {
Node* pTemp = new Node(t_data);
if (pHead == nullptr) {
pHead = pTemp;
pTail = pTemp;
}
else {
pTail->pNext = pTemp;
pTail = pTemp;
}
length++;
}
//头添加
void push_font(DATA t_data) {
Node* pTemp = new Node(t_data);
if (pHead == nullptr) {
pHead = pTemp;
pTail = pTemp;
}
else {
pTemp->pNext = pHead;
pHead = pTemp;
}
length++;
}
//返回头指针
Node* _head() {
return this->pHead;
}
//返回尾指针
Node* _tail() {
return this->pTail->pNext;
}
//清空 链表
void clear() {
Node* pNode = pHead;
while (pNode != nullptr) {
Node* pTemp = pNode;
pNode = pNode->pNext;
delete pTemp;
pTemp = nullptr;
}
this->length = 0;
pHead = nullptr;
pTail = nullptr;
}
//返回链表长度
int size() {
return this->length;
}
//遍历链表
void TravelList() {
Node* pNode = pHead;
while (pNode != nullptr) {
std::cout << pNode->_data << endl;
pNode = pNode->pNext;
}
}
private:
int length;//链表长度
Node* pHead;
Node* pTail;
};
c语言版
#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode {
int val;
struct ListNode* next;
}ListNode;
typedef struct ListHead {
struct ListNode* head;
struct ListNode* end;
int length;
}ListHead;
ListHead* CreateList() {
ListHead* list = (ListHead*)malloc(sizeof(ListHead));
list->end = NULL;
list->head = NULL;
return list;
}
ListNode* CreateNode(int n) {
ListNode* pNode = (ListNode*)malloc(sizeof(ListNode));
pNode->val = n;
pNode->next = NULL;
return pNode;
}
void pushback(ListHead* list,int n) {
ListNode* pTemp = CreateNode(n);
if (list->head == NULL) {
list->head = pTemp;
list->end = pTemp;
return;
}
ListNode* pNode = list->head;
while (pNode->next != NULL) {
pNode = pNode->next;
}
pNode->next = pTemp;
list->end = pTemp;
return;
}
void pushfont(ListHead* list, int n) {
ListNode* pTemp = CreateNode(n);
if (list->head == NULL) {
list->head = pTemp;
list->end = pTemp;
return;
}
ListNode* pNode = list->head;
list->head = pTemp;
pTemp->next = pNode;
return;
}
void DeleteList(ListHead* list) {
if (list->head == NULL) {
return;
}
ListNode* pNode = list->head;
ListNode* pTemp;
while (pNode != NULL) {
pTemp = pNode->next;
free(pNode);
pNode = pTemp;
}
list->head = NULL;
list->end = NULL;
}
void PrintList(ListHead* list) {
ListNode* pNode = list->head;
if (pNode == NULL) {
printf("empty.\n");
return;
}
while (pNode != NULL) {
printf("%d\n", pNode->val);
pNode = pNode->next;
}
return;
}
int Delete_Key(ListHead* list,int index) {
ListNode* pNode = list->head;
if (pNode == NULL) {
return 0;
}
if (index == 1) {
list->head = pNode->next;
free(pNode);
return 1;
}
int counter = 1;
while (pNode->next && counter < index-1) {
pNode = pNode->next;
counter++;
}
if (pNode->next == NULL || counter > index - 1) {
return 0;
}
ListNode* pTemp = pNode->next;
pNode->next = pNode->next->next;
free(pTemp);
return 1;
}
int Delete_Num(ListHead* list,int n) {
ListNode* pNode = list->head;
if (pNode == NULL) {
return 0;
}
if (pNode->val == n) {
list->head = pNode->next;
free(pNode);
return 1;
}
while (pNode->next) {
ListNode* pTemp = pNode->next;
if (pTemp->val == n) {
pNode->next = pNode->next->next;
free(pTemp);
}
else {
pNode = pNode->next;
}
}
return 1;
}
int Insert_Before(ListHead* list,int index,int n) {
ListNode* pNode = list->head;
if (pNode == NULL) {
return 0;
}
if (index == 1) {
ListNode* pTemp = CreateNode(n);
pTemp->next = list->head;
list->head = pTemp;
return 1;
}
int counter = 1;
while (pNode->next && counter < index - 1) {
pNode = pNode->next;
counter++;
}
if (pNode->next == NULL || counter > index - 1) {
return 0;
}
ListNode* pTemp = CreateNode(n);
pTemp->next = pNode->next;
pNode->next = pTemp;
return 1;
}
int Insert_Behind(ListHead* list,int index,int n) {
ListNode* pNode = list->head;
if (pNode == NULL) {
return 0;
}
int counter = 1;
while (pNode && counter < index) {
pNode = pNode->next;
counter++;
}
if (pNode == NULL || counter > index) {
return 0;
}
ListNode* pTemp = CreateNode(n);
pTemp->next = pNode->next;
pNode->next = pTemp;
return 1;
}
void ReverseList(ListHead* list){
if (list->head == NULL || list->head->next == NULL) {
return;
}
ListNode* pPro = NULL;
ListNode* pCur = list->head;
ListNode* pNext = list->head->next;
while (pCur) {
pCur->next = pPro;
pPro = pCur;
pCur = pNext;
if (pNext) {
pNext = pNext->next;
}
}
list->head = pPro;
}
int main() {
ListHead* list = CreateList();
PrintList(list);
pushfont(list, 5);
pushfont(list, 4);
PrintList(list);
printf("----------\n");
ReverseList(list);
//DeleteList(list);
PrintList(list);
return 0;
}
Codemon2024.02.22