【数据结构和算法】链表的简单实现

🐱‍🚀个人博客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

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Codingemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值