C++链表插入删除遍历实现

 本文通过C++实现C++单链表相关操作:初始化、插入、删除、打印等操作。

#pragma once
#include <iostream>

//定义节点
typedef struct Node {
	int value;
	Node* next;
};
typedef Node* pNode;


class CListTest
{
	
public:
	CListTest() {
		Head = new Node();
		Head->next = NULL;
	}
	~CListTest();
public:

	//初始化链表
	void createList(int* array, int len);

	//插入一个节点
	bool insertNode(int location, int value);

	//删除一个节点
	bool deleteNode(int location);

	//链表长度
	int getLength();

	//打印链表
	void printList();
private:
	pNode Head;
};

#include "ListTest.h"

CListTest::~CListTest()
{
	pNode p = Head;
	pNode temp;
	while (p) {
		temp = p;
		p = p->next;
		delete temp;
	}
}

void CListTest::createList(int* array, int len)
{
	pNode tHead = Head;
	for (int i = 0; i < len; i++)
	{
		pNode temp = new Node;
		temp->value = array[i];
		temp->next = NULL;
		tHead->next = temp;
		//子函数中tHead的值改变,不会影响Head的地址
		tHead = temp;
	}
}

bool CListTest::insertNode(int location, int value)
{
	pNode p = Head;
	int len = getLength();
	if (location > len || location < 0) {
		std::cout << "Error:insert location is woring!" << std::endl;
		return false;
	}
	while (p && location) {
		p = p->next;
		location--;
	}

	pNode temp = new Node;
	//std::cout << "insert add:" << temp<<std::endl;
	temp->value = value;
	temp->next = p->next;
	p->next = temp;
	return true;
}

bool CListTest::deleteNode(int location)
{
	int len = getLength();
	//不能删除头指针
	if (location > len || location <= 0) {
		std::cout << "Error:insert location is woring!" << std::endl;
		return false;
	}

	pNode p = Head;
	location--;
	//定位到待删除节点前一个
	while (p && location) {
		p = p->next;
		location--;
	}

	
	pNode pDel = p->next;//待删除节点
	p->next = pDel->next;
	delete pDel;


	return true;
}

int CListTest::getLength()
{
	pNode p = Head->next;
	int len = 0;
	while (p) {
		len++;
		p = p->next;
	}
	return len;
}

void CListTest::printList()
{
	pNode p = Head->next;
	while (p) {
		std::cout << p->value << " --> ";
		p = p->next;
	}
	std::cout << std::endl;
}

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据成绩名单创建链表,可以按照以下步骤进行操作: 1. 首先,定义一个链表节点的结构体,包括成绩及指向下一个节点的指针。 2. 创建一个指向链表头节点的指针,并将其初始化为NULL。 3. 读取成绩名单,将每个成绩依次插入链表。 4. 遍历链表,输出每个成绩。 具体代码如下: ```c #include <stdio.h> #include <stdlib.h> // 链表节点的结构体 typedef struct Node { int score; // 成绩 struct Node* next; // 下一个节点指针 } Node; // 在链表末尾插入节点 void insertNode(Node** head, int score) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->score = score; newNode->next = NULL; if (*head == NULL) { // 若链表为空,则直接插入为头节点 *head = newNode; } else { // 否则找到链表末尾插入 Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } // 遍历链表,输出成绩 void traverseList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d ", temp->score); temp = temp->next; } printf("\n"); } int main() { Node* head = NULL; // 链表头节点指针 // 假设成绩名单为 [85, 90, 77, 92, 80] insertNode(&head, 85); insertNode(&head, 90); insertNode(&head, 77); insertNode(&head, 92); insertNode(&head, 80); // 遍历链表,输出成绩 traverseList(head); return 0; } ``` 以上代码根据输入的成绩名单创建了一个链表,并利用遍历函数输出链表中的每个成绩。最终输出结果为:85 90 77 92 80。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值