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;
}