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;
}
相关推荐
派阿喵搞电子4 小时前
在UI界面内修改了对象名,在#include “ui_mainwindow.h“没更新
c++·qt·ubuntu·ui
C++ 老炮儿的技术栈5 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
mochensage7 小时前
CSP信奥赛C++常用系统函数汇总
c++·信奥
mochensage7 小时前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
fpcc7 小时前
跟我学c++中级篇——多线程中的文件处理
c++
5:008 小时前
云备份项目
linux·开发语言·c++
乄夜8 小时前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
YYDS3149 小时前
C++动态规划-01背包
开发语言·c++·动态规划
wydaicls9 小时前
十一.C++ 类 -- 面向对象思想
开发语言·c++
姜君竹10 小时前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构