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;
}
相关推荐
若亦_Royi20 分钟前
C++ 的大括号的用法合集
开发语言·c++
ragnwang4 小时前
C++ Eigen常见的高级用法 [学习笔记]
c++·笔记·学习
lqqjuly7 小时前
特殊的“Undefined Reference xxx“编译错误
c语言·c++
冰红茶兑滴水7 小时前
云备份项目--工具类编写
linux·c++
刘好念7 小时前
[OpenGL]使用 Compute Shader 实现矩阵点乘
c++·计算机图形学·opengl·glsl
酒鬼猿8 小时前
C++进阶(二)--面向对象--继承
java·开发语言·c++
姚先生978 小时前
LeetCode 209. 长度最小的子数组 (C++实现)
c++·算法·leetcode
小王爱吃月亮糖9 小时前
QT开发【常用控件1】-Layouts & Spacers
开发语言·前端·c++·qt·visual studio
aworkholic9 小时前
opencv sdk for java中提示无stiching模块接口的问题
java·c++·opencv·jni·opencv4android·stiching
程序员老冯头9 小时前
第十六章 C++ 字符串
开发语言·c++