list链表的创建,排序,插入, test ok

1. 链表的建立,打印

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <iostream>
#include <string.h>
#include <string>

using namespace std;

struct node {
	int data;
	struct node* next;
};
struct node* initHead()
{
	struct node* newNode = (struct node*)malloc(sizeof(struct node));
	if (nullptr == newNode)
	{
		return nullptr;
	}
	newNode->data = 0;
	newNode->next = nullptr;
	return newNode;
}
struct node* initNode(int num)
{
	struct node* newNode = (struct node*)malloc(sizeof(struct node));
	if (nullptr == newNode)
	{
		return nullptr;
	}
	newNode->data = num;
	newNode->next = nullptr;
	return newNode;
}
void insertNode(struct node *head ,struct node * node)
{
	if (nullptr == head)
	{
		return;
	}
	if (nullptr == node)
	{
		return;
	}
	head->next = node;
}


void nodePrint(struct node* head)
{
	if (nullptr == head->next)
	{
		return;
	}
	node* temp_print_node = head->next;
	while (nullptr != temp_print_node)
	{
		printf(",%c", temp_print_node->data);
		temp_print_node = temp_print_node->next;
	}
}

int main(int argc, char* argv[]) 
{

	node* A = initHead();
	node* B = initNode('B');
	node* C = initNode('C');
	node* D = initNode('D');
	node* E = initNode('E');
	node* F = initNode('F');
	node* G = initNode('G');

	insertNode(A, B);
	insertNode(B, C);
	insertNode(C, D);
	insertNode(D, E);
	insertNode(E, F);
	insertNode(F, G);
	nodePrint(A);
	system("pause");
	return 0;
}

输出打印

2. 链表冒泡排序法

C/C++编程技术:数据结构与算法之二叉树(一个小时掌握)_哔哩哔哩_bilibili

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <iostream>
#include <string.h>
#include <string>

using namespace std;

struct node {
	int data;
	struct node* next;
};
struct node* initHead()
{
	struct node* newNode = (struct node*)malloc(sizeof(struct node));
	if (nullptr == newNode)
	{
		return nullptr;
	}
	newNode->data = 0;
	newNode->next = nullptr;
	return newNode;
}
struct node* initNode(int num)
{
	struct node* newNode = (struct node*)malloc(sizeof(struct node));
	if (nullptr == newNode)
	{
		return nullptr;
	}
	newNode->data = num;
	newNode->next = nullptr;
	return newNode;
}
void insertNode(struct node *head ,struct node * node)
{
	if (nullptr == head)
	{
		return;
	}
	if (nullptr == node)
	{
		return;
	}
	head->next = node;
}


void nodePrint(struct node* head)
{
	if (nullptr == head->next)
	{
		return;
	}
	node* temp_print_node = head->next;
	while (nullptr != temp_print_node)
	{
		printf(",%d", temp_print_node->data);
		temp_print_node = temp_print_node->next;
	}
}
void maopao(struct node* head)
{
	node* turn = nullptr;
	node* move = nullptr;
	node* save = nullptr;
	for (turn = head->next; turn->next != nullptr; turn = turn->next)
	{
		for (move = head->next; move->next != save; move = move->next)
		{
			if ((move->data) > (move->next->data))
			{
				int temp_data = move->data;
			    move->data = move->next->data;
				move->next->data = temp_data;
			}
		}
		save = move;
	}
}

int main(int argc, char* argv[]) 
{

	node* A = initHead();
	node* B = initNode(3);
	node* C = initNode(2);
	node* D = initNode(1);
	node* E = initNode(6);
	node* F = initNode(9);
	node* G = initNode(4);

	insertNode(A, B);
	insertNode(B, C);
	insertNode(C, D);
	insertNode(D, E);
	insertNode(E, F);
	insertNode(F, G);
	std::cout << "main before" << std::endl;
	nodePrint(A);
	maopao(A);
	std::cout << "main after" << std::endl;
	nodePrint(A);
	system("pause");
	return 0;
}
相关推荐
CodeWithMe2 小时前
【C++】线程池
开发语言·c++
wuqingshun3141592 小时前
蓝桥杯 2. 确定字符串是否是另一个的排列
数据结构·c++·算法·职场和发展·蓝桥杯
hu_yuchen3 小时前
C++:BST、AVL、红黑树
开发语言·c++
炯哈哈3 小时前
【上位机——MFC】视图
开发语言·c++·mfc·上位机
我也不曾来过13 小时前
继承(c++版 非常详细版)
开发语言·c++
1白天的黑夜14 小时前
贪心算法-2208.将数组和减半的最小操作数-力扣(LeetCode)
c++·算法·leetcode·贪心算法
AAAA劝导tx4 小时前
List--链表
数据结构·c++·笔记·链表·list
愚润求学5 小时前
【Linux】进程优先级和进程切换
linux·运维·服务器·c++·笔记
Dream it possible!5 小时前
LeetCode 热题 100_最小路径和(92_64_中等_C++)(多维动态规划)
c++·leetcode·动态规划
纪元A梦5 小时前
华为OD机试真题——阿里巴巴找黄金宝箱Ⅰ(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
java·c语言·javascript·c++·python·华为od·go