IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 2021-7-16 普通链表c++的实现 -> 正文阅读

[数据结构与算法]2021-7-16 普通链表c++的实现

代码有任何可改进或错误请指正

文章目录


代码

List.h

#pragma once
#include<iostream>
using namespace std;

typedef struct LNode* ListP;

struct LNode {
		int Data;
		ListP Next;
		LNode(int val, ListP Next) :Data(val), Next(NULL) {}
		LNode() = default;
		
};

class cList {
private:
	//这是一种委托    pointer
	static ListP pHead;//链表的头指针
	static ListP pTail;//暂时没用到
public:
	ListP buildList(int *arr, int n);
	void Print();
	//查询表长
	int Length();
	//按照序号查找元素
	ListP FindKth(int K);
	//按照值查找元素
	ListP Find(int x);
	//更改数值
	ListP changeANode( int n, int m);
	//插入数据
	ListP addANode(int n, int addedNum);
	//删除操作
	ListP Delete(int i);
	//清空链表
	ListP Clear();
};

list.cpp


#include "List.h"

ListP cList::pHead = NULL;
ListP cList::pTail = NULL;

ListP cList::buildList(int* arr, int n) {
	pHead = NULL;
	pTail = NULL;
	for (int i = 0; i < n; i++) {
		ListP tmp = new struct LNode(arr[i], NULL);
		/*ListP tmp = (ListP)malloc(sizeof(LNode));
		tmp->Data = arr[i];
		tmp->Next = NULL;*/
		if (pHead==NULL) {
			pHead = tmp;
			pTail = tmp;
		}
		else {
			pTail->Next = tmp;
			pTail = tmp;
		}
	}
	return pHead;
}
void cList::Print() {
	ListP p = pHead;
	while (p) {
		cout << p->Data << " ";
		p = p->Next;
	}
	if (pHead == NULL) {
		cout << "isclear~"<<endl;
	}
}
int cList::Length() {
	ListP p = pHead;
	int j = 0;
	while (p) {
		j++;
		p = p->Next;
	}
	return j;
}
//这里k不是下标而是下标+1
ListP cList::FindKth(int K) {
	ListP p = pHead;
	int i = 1;
	while (p != NULL && i < K) {
		p = p->Next;
		i++;
	}
	if (i == K) {
		return p;
	}
	else {
		return NULL;
	}
}

ListP cList::Find(int x) {
	ListP p = pHead;
	while (p != NULL && p->Data != x) {
		p = p->Next;
	}
	if (p->Data == x) {
		return p;
	}
	else {
		return p;
	}
}
//插入的位置不是下标而是下标+1
ListP cList::addANode( int n, int addedNum) {
	ListP p, s;
	if (n == 1) {
		s = (ListP)malloc(sizeof(struct LNode));
		s->Data = addedNum;
		s->Next = pHead;
		pHead = s;
		return pHead;
	}
	p = FindKth(n - 2);
	if (p) {
		cout << "参数n错误";
		return NULL;
	}
	else {
		s = (ListP)malloc(sizeof(struct LNode));
		s->Data = addedNum;
		s->Next = p->Next;
		p->Next = s;
		return s;
	}
}

ListP cList::Delete(int i) {
	ListP p, s;
	if (i == 1) {
		s = pHead;
		if (pHead != NULL) {
			pHead = pHead->Next;
		}
		else {
			return NULL;
		}
		free(s);
		return pHead;
	}
	p = FindKth(i - 1);
	if (p == NULL) {
		cout << "第" << i - 1 << "个结点不存在";
		return NULL;
	}
	else if (p->Next == NULL) {
		cout << "第" << i << "个结点不存在";
		return NULL;
	}
	else {
		s = p->Next;
		p->Next = s->Next;
		free(s);
		return pHead;
	}
}

ListP cList::changeANode(int n, int m) {
	ListP p = FindKth(n);
	 if (p) {
		 p->Data = m;
		 return p;
	 }
	 else {
		 cout << "未找到对应元素";
		 return NULL;
	 }
}
ListP cList::Clear() {
	while (Delete(1)) {	}
	return NULL;
}
#include"List.h"

int main() {
	cList cl;
	int arr[] = { 3,2,4,1,5,7,6,8 };
	cl.buildList(arr, 8);
	cl.Print();
	cl.addANode(1, 99);
	cout << endl;
	cl.Print();
	cl.Delete(1);
	cout << endl;
	cl.Print();
	cl.Clear();
	cl.Print();
	return 0;
}

总结

f反正是能用吧,其实最好加上模板。

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-17 12:36:27  更:2021-07-17 12:36:31 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年3日历 -2024/3/29 14:39:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码