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;
}
相关推荐
dora1 小时前
逼格提起来,使用curl发送网络请求
android·c++
yy_xzz2 小时前
基于条码数据生成校验密码的C++实现方案
开发语言·c++
技术小白Byteman2 小时前
蓝桥刷题note13(排序)
开发语言·数据结构·c++·学习·算法·visualstudio
老歌老听老掉牙2 小时前
C++使用Qt Charts可视化大规模点集
c++·qt·信息可视化·点集
OneQ6662 小时前
C++自学笔记——动态创建对象
c++·笔记·算法
Dream it possible!2 小时前
LeetCode 热题 100_完全平方数(84_279_中等_C++)(动态规划(完全背包))
c++·leetcode·动态规划·完全背包
末央&4 小时前
【C++】vector的底层封装和实现
android·c++
Tadecanlan5 小时前
[C++面试] C++中各类括号的差异:[]、{}、<>、()
开发语言·c++·面试
_GR5 小时前
2023年蓝桥杯第十四届C&C++大学B组真题及代码
c语言·c++·蓝桥杯
努力努力再努力wz5 小时前
【c++深入系列】:类和对象详解(下)
java·运维·c语言·开发语言·c++