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;
}
相关推荐
sinat_276522572 小时前
C++中move的使用
开发语言·c++
微尘82 小时前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
金博客2 小时前
Qt 模型视图(二):模型类QAbstractItemModel
c++·qt6.7.2
五味香3 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
无名之逆3 小时前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试
Beauty.5683 小时前
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
数据结构·c++·算法
jimmy.hua3 小时前
C++刷怪笼(5)内存管理
开发语言·数据结构·c++
xiaobai12 33 小时前
二叉树的遍历【C++】
开发语言·c++·算法
DieSnowK3 小时前
[项目][WebServer][Makefile & Shell]详细讲解
开发语言·c++·http·makefile·shell·项目·webserver
dc爱傲雪和技术4 小时前
在 VS Code 中调试 C++ 项目
开发语言·c++