1 双指针法遍历单链表,找中间结点、找倒数第k个结点
大家可以在自己电脑上新建一个 C++ 项目,并把这段代码复制到 main 函数前边,然后在你的 main 函数里调用 test_LinkList() 这个函数。
提示:用电脑网页版看,体验更好一些
#ifndef CODE_LINKLIST_H
#define CODE_LINKLIST_H
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *LinkList;
bool InitList(LinkList &L) {
L = (LNode *) malloc(sizeof(LNode));
if (L==NULL)
return false;
L->next = NULL;
return true;
}
void PrintList(LinkList L){
LNode *p = L->next;
int len = 0;
printf("L:头结点 —> ");
while (p!=NULL){
len++;
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n链表长度 = %d\n", len);
}
LNode * findMidNode(LinkList head){
LNode * p=head;
LNode * q=head;
while(q->next!=NULL){
q = q->next;
if (q->next!=NULL)
q = q->next;
p=p->next;
}
if(p==head)
printf("一个空链表,让我找中间结点?你一定是在逗我~");
else
printf("中间结点的值为:%d\n", p->data);
return p;
}
LNode * findDaoShuKNode(LinkList head, int k){
LNode * p=head;
LNode * q=head;
int count=0;
bool chong = false;
while(q->next!=NULL){
q = q->next;
count++;
if (count==k)
chong = true;
if(chong)
p = p->next;
}
if(p==head)
printf("这个链表长度小于%d,并不存在倒数第k个结点\n", k);
else
printf("倒数第%d个结点的值为:%d\n", k, p->data);
return p;
}
#define N 20
void test_LinkList() {
LinkList head;
InitList(head);
LNode * p = head;
for (int i=1; i<=N; i++){
p->next = (LNode *) malloc(sizeof(LNode));
p = p->next;
p->data = i;
p->next = NULL;
}
PrintList(head);
findMidNode(head);
findDaoShuKNode(head, 3);
}
#endif
2 二叉排序树的建立、插入、删除、查找
大家可以在自己电脑上新建一个 C++ 项目,并把这段代码复制到 main 函数前边,然后在你的 main 函数里调用 test_BinarySearchTree() 这个函数。
提示:用电脑网页版看,体验更好一些
#ifndef CODE_BINARYSEARCHTREE_H
#define CODE_BINARYSEARCHTREE_H
#include "stdio.h"
#include "stdlib.h"
typedef struct BSTNode{
int key;
struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
typedef struct AVLNode{
int key;
int balance;
struct AVLNode *lchild,*rchild;
}AVLNode,*AVLTree;
void visit(BSTNode * p){
printf("%d,", p->key);
}
void PreOrder(BSTree T){
if(T!=NULL){
visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void InOrder(BSTree T){
if(T!=NULL){
InOrder(T->lchild);
visit(T);
InOrder(T->rchild);
}
}
void PostOrder(BSTree T){
if(T!=NULL){
PostOrder(T->lchild);
PostOrder(T->rchild);
visit(T);
}
}
int treeDepth(BSTree T){
if (T == NULL) {
return 0;
}
else {
int l = treeDepth(T->lchild);
int r = treeDepth(T->rchild);
return l>r ? l+1 : r+1;
}
}
BSTNode * findFather(BSTree T, BSTNode * p) {
if (T==NULL)
return NULL;
if (T->lchild==p || T->rchild==p)
return T;
BSTNode * l = findFather(T->lchild, p);
if (l != NULL)
return l;
BSTNode * r = findFather(T->rchild, p);
if (r != NULL)
return r;
return NULL;
}
BSTNode *BST_Search(BSTree T,int key){
while(T!=NULL&&key!=T->key){
if(key<T->key) T=T->lchild;
else T=T->rchild;
}
return T;
}
BSTNode *BSTSearch(BSTree T,int key){
if (T==NULL)
return NULL;
if (key==T->key)
return T;
else if (key < T->key)
return BSTSearch(T->lchild, key);
else
return BSTSearch(T->rchild, key);
}
int BST_Insert(BSTree &T, int k){
if(T==NULL){
T=(BSTree)malloc(sizeof(BSTNode));
T->key=k;
T->lchild=T->rchild=NULL;
return 1;
}
else if(k==T->key)
return 0;
else if(k<T->key)
return BST_Insert(T->lchild,k);
else
return BST_Insert(T->rchild,k);
}
int BST_DeleteNode(BSTree &T, BSTNode * p){
if (p==NULL)
return 0;
BSTNode *f = findFather(T, p);
bool isLchild = false;
if (f->lchild == p)
isLchild = true;
if (p->lchild==NULL && p->rchild==NULL) {
if (isLchild){
f->lchild = NULL;
} else {
f->rchild = NULL;
}
free(p);
return 1;
}
if (p->lchild!=NULL && p->rchild==NULL) {
if (isLchild){
f->lchild = p->lchild;
} else {
f->rchild = p->lchild;
}
free(p);
return 1;
}
if (p->lchild==NULL && p->rchild!=NULL) {
if (isLchild){
f->lchild = p->rchild;
} else {
f->rchild = p->rchild;
}
free(p);
return 1;
}
BSTNode * q = p->rchild;
while(q->lchild!=NULL){
q = q->lchild;
}
p->key = q->key;
BST_DeleteNode(T, q);
return 1;
}
int BST_DeleteKey(BSTree &T, int k){
BSTNode * p = BST_Search(T, k);
if (p==NULL){
printf("【删除失败】,不存在这个关键字 %d\n", k);
return 0;
}
if (p==T){
printf("【删除失败】,关键字 %d 是根节点。答应我,我们不删除根节点,好吗?\n(注:删除根节点会更麻烦一点,有能力的同学可以自己动手实现)", k);
return 0;
}
BST_DeleteNode(T, p);
printf("【删除成功】,关键字为 %d\n", k);
}
void Creat_BST(BSTree &T,int str[],int n){
T=NULL;
int i=0;
while(i<n){
BST_Insert(T,str[i]);
i++;
}
}
typedef struct LinkNode{
BSTNode * data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
void InitQueue(LinkQueue &Q){
Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}
bool IsEmpty(LinkQueue Q){
if(Q.front==Q.rear)
return true;
else
return false;
}
void EnQueue(LinkQueue &Q,BSTNode * x){
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=x;
s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}
bool DeQueue(LinkQueue &Q,BSTNode * &x){
if(Q.front==Q.rear)
return false;
LinkNode *p=Q.front->next;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return true;
}
void LevelOrder(BSTree T){
LinkQueue Q;
InitQueue(Q);
BSTree p;
EnQueue(Q,T);
while(!IsEmpty(Q)){
DeQueue(Q, p);
visit(p);
if(p->lchild!=NULL)
EnQueue(Q,p->lchild);
if(p->rchild!=NULL)
EnQueue(Q,p->rchild);
}
}
void printBST(BSTree T){
LinkQueue Q;
InitQueue(Q);
BSTree p;
EnQueue(Q,T);
int h=treeDepth(T);
int maxLayer = pow(2, h-1);
printf("\n树的亚子:\n");
for (int i=1; i<=h; i++){
int sum = pow(2, i-1);
int gap = maxLayer/sum;
for (int j=0; j<gap/2; j++)
printf("\t");
for (int j=0; j<sum; j++){
DeQueue(Q, p);
if (p==NULL){
printf("空");
}else{
printf("%d", p->key);
}
if (p==NULL){
EnQueue(Q,NULL);
EnQueue(Q,NULL);
} else {
EnQueue(Q,p->lchild);
EnQueue(Q,p->rchild);
}
for (int k=0; k<gap; k++)
printf("\t");
}
printf("\n");
}
}
int test_BinarySearchTree() {
int str[] = {985,211,996,45,12,24};
BSTree root;
Creat_BST(root, str, 6);
printf("\n中序遍历:");
InOrder(root);
printf("\n先序遍历:");
PreOrder(root);
printf("\n后序遍历:");
PostOrder(root);
printf("\n层序遍历:");
LevelOrder(root);
printf("\n树的深度=%d", treeDepth(root));
printBST(root);
BST_Insert(root, 666);
printBST(root);
BST_Insert(root, 1000);
printBST(root);
int key1 = 2333;
BSTNode * result1;
result1 = BST_Search(root, key1);
if (result1!=NULL)
printf("关键字%d查找成功(非递归实现)\n", key1);
else
printf("关键字%d查找失败(非递归实现)\n", key1);
int key2 = 996;
BSTNode * result2;
result2 = BST_Search(root, key2);
if (result2!=NULL)
printf("关键字%d查找成功(递归实现)\n", key2);
else
printf("关键字%d查找失败(递归实现)\n", key2);
BST_DeleteKey(root, 211);
printBST(root);
return 0;
}
3 顺序存储的二叉树、链式存储的二叉树 相关的各种代码
这段代码有点多,定义了顺序存储的二叉树、链式存储的二叉树。大家可以把这段代码复制到你的 main 函数之前,并在 main 函数中调用以下几个函数来观察效果。
调用 test_SqBiTree() 函数,这个函数中写了一个例子:建立一棵顺序二叉树,并基于顺序存储的二叉树,进行先序、中序、后序遍历
调用 test_CreateBySqBiTree() 函数,这个函数中写了一个例子:根据顺序存储的二叉树,构建了一棵等价的链式存储二叉树
调用 test_CreateByBiTree() 函数,这个函数中写了一个例子:根据链式存储的二叉树,构建了一棵等价的顺序存储二叉树
调用 test_BiTreeWidth() 函数,这个函数中写了一个例子:建立一棵链式存储的二叉树,并求它的宽度(对应结课考试的算法题第二题)
调用 test_IsCompleteBinaryTree() 函数,这个函数中写了一个例子:建立二叉树,并用层序遍历的思想,判断该二叉树是否为完全二叉树
提示:用电脑网页版看,体验更好一些
#ifndef BITREEWIDTH_H
#define BITREEWIDTH_H
#include "stdio.h"
#include "stdlib.h"
typedef struct TreeNode {
int data;
bool isEmpty;
} TreeNode;
void InitSqBiTree (TreeNode t[], int length) {
for (int i=0; i<length; i++){
t[i].isEmpty=true;
}
}
bool isEmpty(TreeNode t[], int length, int index){
if (index >= length || index < 1) return true;
return t[index].isEmpty;
}
void visitNode(TreeNode node){
printf("%d ", node.data);
}
int getLchild(TreeNode t[], int length, int index){
int lChild = index * 2;
if (isEmpty(t, length, lChild)) return -1;
return lChild;
}
int getRchild(TreeNode t[], int length, int index){
int rChild = index * 2 + 1;
if (isEmpty(t, length, rChild)) return -1;
return rChild;
}
int getFather(TreeNode t[], int length, int index){
if (index == 1) return -1;
int father = index / 2;
if (isEmpty(t, length, father)) return -1;
return father;
}
void PreOrderSqTree (TreeNode *t, int length, int index){
if (isEmpty(t, length, index))
return;
visitNode(t[index]);
PreOrderSqTree(t, length, getLchild(t, length, index));
PreOrderSqTree(t, length, getRchild(t, length, index));
}
void InOrderSqTree (TreeNode *t, int length, int index){
if (isEmpty(t, length, index))
return;
InOrderSqTree(t, length, getLchild(t, length, index));
visitNode(t[index]);
InOrderSqTree(t, length, getRchild(t, length, index));
}
void PostOrderSqTree (TreeNode *t, int length, int index){
if (isEmpty(t, length, index))
return;
PostOrderSqTree(t, length, getLchild(t, length, index));
PostOrderSqTree(t, length, getRchild(t, length, index));
visitNode(t[index]);
}
int test_SqBiTree() {
TreeNode t[100];
InitSqBiTree(t, 100);
for (int i=1; i<=12; i++){
t[i].data = i;
t[i].isEmpty = false;
}
printf("【对顺序存储的二叉树先序遍历】:");
PreOrderSqTree(t, 100, 1);
printf("\n");
printf("【对顺序存储的二叉树中序遍历】:");
InOrderSqTree(t, 100, 1);
printf("\n");
printf("【对顺序存储的二叉树后序遍历】:");
PostOrderSqTree(t, 100, 1);
printf("\n");
return 0;
}
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTNode * test;
void visit(BiTNode * p){
if (p==NULL)
return;
printf("%d ", p->data);
}
void PreOrder (BiTree root){
if (root==NULL)
return;
visit(root);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
void InOrder (BiTree root){
if (root==NULL)
return;
InOrder(root->lchild);
visit(root);
InOrder(root->rchild);
}
void PostOrder (BiTree root){
if (root==NULL)
return;
PostOrder(root->lchild);
PostOrder(root->rchild);
visit(root);
}
typedef struct QNode{
BiTNode * data;
struct QNode *next;
} QNode;
typedef struct{
QNode *front,*rear;
}Queue;
void InitQueue(Queue &Q){
Q.front=Q.rear=(QNode*)malloc(sizeof(QNode));
Q.front->next=NULL;
}
bool IsEmpty(Queue Q){
if(Q.front==Q.rear)
return true;
else
return false;
}
void EnQueue(Queue &Q, BiTNode * x){
QNode *s=(QNode *)malloc(sizeof(QNode));
s->data=x;
s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}
bool DeQueue(Queue &Q, BiTNode * &x){
if(Q.front==Q.rear)
return false;
QNode *p=Q.front->next;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return true;
}
void LevelOrder(BiTree T){
Queue Q;
InitQueue(Q);
BiTree p;
EnQueue(Q,T);
while(!IsEmpty(Q)){
DeQueue(Q, p);
visit(p);
if(p->lchild!=NULL)
EnQueue(Q,p->lchild);
if(p->rchild!=NULL)
EnQueue(Q,p->rchild);
}
}
bool IsCompleteBinaryTree(BiTree T){
if (T==NULL){
printf("老哥,空树不是完全二叉树");
return false;
}
Queue Q;
InitQueue(Q);
BiTree p;
EnQueue(Q,T);
bool flag = false;
while(!IsEmpty(Q)){
DeQueue(Q, p);
if(p->lchild==NULL && p->rchild!=NULL){
return false;
}
if(p->lchild==NULL && p->rchild==NULL){
flag = true;
}
if(p->lchild!=NULL && p->rchild==NULL){
if (flag) {
return false;
}
flag = true;
}
if(p->lchild!=NULL && p->rchild!=NULL){
if (flag) {
return false;
}
}
visit(p);
if(p->lchild!=NULL)
EnQueue(Q,p->lchild);
if(p->rchild!=NULL)
EnQueue(Q,p->rchild);
}
}
BiTree creatTree(){
BiTree root = (BiTree) malloc(sizeof(BiTNode));
root->data=1;
BiTNode * p1 = (BiTNode *)malloc(sizeof(BiTNode));
p1->data=2;
root->lchild=p1;
BiTNode * p2 = (BiTNode *)malloc(sizeof(BiTNode));
p2->data=3;
root->rchild=p2;
BiTNode * p3 = (BiTNode *)malloc(sizeof(BiTNode));
p3->data=4;
p1->lchild=p3;
p3->lchild = NULL;
p3->rchild = NULL;
BiTNode * p4 = (BiTNode *)malloc(sizeof(BiTNode));
p4->data=5;
p1->rchild=p4;
BiTNode * p5 = (BiTNode *)malloc(sizeof(BiTNode));
p5->data=6;
p2->lchild=p5;
p5->lchild = NULL;
p5->rchild = NULL;
BiTNode * p6 = (BiTNode *)malloc(sizeof(BiTNode));
p6->data=7;
p2->rchild=p6;
p6->lchild = NULL;
p6->rchild = NULL;
BiTNode * p7 = (BiTNode *)malloc(sizeof(BiTNode));
p7->data=8;
p4->lchild=p7;
p7->lchild = NULL;
p7->rchild = NULL;
BiTNode * p8 = (BiTNode *)malloc(sizeof(BiTNode));
p8->data=9;
p4->rchild=p8;
BiTNode * p9 = (BiTNode *)malloc(sizeof(BiTNode));
p9->data=10;
p8->lchild=p9;
p9->lchild = NULL;
p9->rchild = NULL;
BiTNode * p10 = (BiTNode *)malloc(sizeof(BiTNode));
p10->data=11;
p8->rchild=p10;
p10->lchild = NULL;
p10->rchild = NULL;
test=p3;
return root;
}
void PreOrderWidth(BiTree T, int level, int *width){
if (T == NULL) return;
width[level]++;
PreOrderWidth(T->lchild, level + 1, width);
PreOrderWidth(T->rchild, level + 1, width);
}
#define H 10
int treeWidth (BiTree T) {
int width[H];
for (int i=0; i<H; i++)
width[i]=0;
PreOrderWidth(T, 0, width);
int maxWidth = 0;
for (int i=0; i<H; i++){
if(width[i]>maxWidth)
maxWidth =width[i];
}
return maxWidth;
}
void CreateBySqBiTree (TreeNode t[], int length, int index, BiTree &root){
if(isEmpty(t, length, index)){
root = NULL;
return;
}
root = (BiTNode *) malloc(sizeof(BiTNode));
root->data = t[index].data;
int index_lChild = getLchild(t, length, index);
CreateBySqBiTree (t, length, index_lChild, root->lchild);
int index_rChild = getRchild(t, length, index);
CreateBySqBiTree (t, length, index_rChild, root->rchild);
}
void CreateByBiTree (TreeNode t[], int length, int index, BiTree root){
if (root==NULL){
return;
}
if (index >= length) {
printf("兄弟啊,你给的这个数组,存不下这么大的树\n");
return;
}
t[index].isEmpty = false;
t[index].data = root->data;
CreateByBiTree(t, length, index*2, root->lchild);
CreateByBiTree(t, length, index*2+1, root->rchild);
}
int test_CreateBySqBiTree (){
TreeNode t[100];
InitSqBiTree(t, 100);
for (int i=1; i<=12; i++){
t[i].data = i;
t[i].isEmpty = false;
}
BiTree root;
CreateBySqBiTree(t, 100, 1, root);
printf("\n【对链式存储的二叉树先序遍历】:");
PreOrder(root);
printf("\n【对链式存储的二叉树中序遍历】:");
InOrder(root);
printf("\n【对链式存储的二叉树后序遍历】:");
PostOrder(root);
}
int test_CreateByBiTree (){
TreeNode t[100];
InitSqBiTree(t, 100);
BiTree root = creatTree();
printf("\n【对链式存储的二叉树先序遍历】:");
PreOrder(root);
printf("\n【对链式存储的二叉树中序遍历】:");
InOrder(root);
printf("\n【对链式存储的二叉树后序遍历】:");
PostOrder(root);
printf("\n【对链式存储的二叉树层序遍历】:");
LevelOrder(root);
CreateByBiTree(t, 100, 1, root);
printf("\n【链式二叉树转顺序二叉树】数组的值为:");
for (int i=1; i<100; i++){
if (t[i].isEmpty)
printf("空 ");
else
printf("%d ", t[i].data);
}
}
int test_BiTreeWidth() {
BiTree root = creatTree();
int width = treeWidth(root);
printf("小伙汁,你这棵树的宽度=%d\n", width);
return 0;
}
void test_IsCompleteBinaryTree(){
BiTree root = creatTree();
printf("\n【对链式存储的二叉树先序遍历】:");
PreOrder(root);
printf("\n【对链式存储的二叉树中序遍历】:");
InOrder(root);
printf("\n【对链式存储的二叉树层序遍历】:");
LevelOrder(root);
printf("\n【层序遍历检查是否是完全二叉树】:");
if(IsCompleteBinaryTree(root)){
printf("\n 这棵二叉树是完全二叉树");
} else {
printf("\n 这棵二叉树不是完全二叉树");
}
TreeNode t[100];
InitSqBiTree(t, 100);
for (int i=1; i<=15; i++){
t[i].data = i;
t[i].isEmpty = false;
}
BiTree T;
CreateBySqBiTree(t, 100, 1, T);
printf("\n【对链式存储的二叉树先序遍历】:");
PreOrder(T);
printf("\n【对链式存储的二叉树中序遍历】:");
InOrder(T);
printf("\n【对链式存储的二叉树层序遍历】:");
LevelOrder(T);
printf("\n【层序遍历检查是否是完全二叉树】:");
if(IsCompleteBinaryTree(T)){
printf("\n 这棵二叉树是完全二叉树");
} else {
printf("\n 这棵二叉树不是完全二叉树");
}
}
#endif
4 实现散列表(用拉链法解决冲突)
大家可以在自己电脑上新建一个 C++ 项目,并把这段代码复制到 main 函数前边,然后在你的 main 函数里调用 test_HashTable() 这个函数。跑一跑看看效果
提示:用电脑网页版看,体验更好一些
#ifndef CODE_HASHTABLE_H
#define CODE_HASHTABLE_H
#include "stdlib.h"
#include "stdio.h"
typedef struct HaNode {
int key;
struct HaNode * next;
} HaNode;
#define N 7
typedef struct {
HaNode * h;
} HashTable[N];
void InitHashTable (HashTable t){
for (int i=0; i<N; i++)
t[i].h = NULL;
}
int InsertElem (HashTable t, int key){
HaNode * p = (HaNode *)malloc(sizeof(HaNode));
p->key = key;
int index = key%N;
p->next = t[index].h;
t[index].h = p;
return 1;
}
int DeleteElem (HashTable t, int key){
int index = key%N;
int flag = 0;
HaNode * pPre = NULL;
HaNode * p = t[index].h;
while (p!=NULL) {
if (p->key==key){
if (pPre==NULL){
t[index].h = p->next;
HaNode * s = p;
p = p->next;
free(s);
} else {
pPre->next = p->next;
HaNode * s = p;
p = p->next;
free(s);
}
flag = 1;
} else {
pPre = p;
p = p->next;
}
}
return flag;
}
HaNode * GetElem (HashTable t, int key){
int index = key%N;
HaNode * p = t[index].h;
while (p!=NULL) {
if (p->key==key){
return p;
}
p = p->next;
}
return NULL;
}
void PrintHashTable(HashTable t){
printf("你的散列表长酱紫:\n");
for (int i=0; i<N; i++){
printf("HashTable[%d]——>", i);
HaNode * p = t[i].h;
while (p!=NULL){
printf("%d——>", p->key);
p=p->next;
}
printf("NULL\n");
}
}
void test_HashTable() {
HashTable t;
InitHashTable(t);
PrintHashTable(t);
for (int i=0; i<50; i++){
int key = rand()%20;
InsertElem(t, key);
}
PrintHashTable(t);
InsertElem(t, 985);
PrintHashTable(t);
InsertElem(t, 211);
PrintHashTable(t);
DeleteElem(t, 9);
PrintHashTable(t);
DeleteElem(t, 5);
PrintHashTable(t);
HaNode * result;
int key = 9;
result = GetElem(t, key);
if (result==NULL){
printf("哎呀,没找到你的结点:%d\n", key);
} else {
printf("成功找到你的结点:%d\n", key);
}
key = 985;
result = GetElem(t, key);
if (result==NULL){
printf("哎呀,没找到你的结点:%d\n", key);
} else {
printf("成功找到你的结点:%d\n", key);
}
}
5 用单链表实现多项式加法(对应:补充算法题第一题)
大家可以在自己电脑上新建一个 C++ 项目,并把这段代码复制到 main 函数前边,然后在你的 main 函数里调用 test_PolynomialAdditation() 这个函数。跑一跑看看效果
提示:用电脑网页版看,体验更好一些
注:这段代码对应强化课“补充算法题”第一题
#ifndef CODE_POLYNOMIALADDITATION_H
#define CODE_POLYNOMIALADDITATION_H
#include "stdio.h"
#include "stdlib.h"
typedef struct PoNode {
int co;
int ex;
struct PoNode *next;
} *Polynomial;
void printPolynomial (Polynomial p){
printf("【多项式】:");
if (p==NULL)
printf("这是一个空多项式\n");
while (p!=NULL){
printf("%dX^%d ", p->co, p->ex);
p = p->next;
}
printf("\n");
}
int InsertNode(Polynomial &p, int co, int ex){
struct PoNode * q = (struct PoNode *)malloc(sizeof(struct PoNode));
q->ex = ex;
q->co = co;
q->next = NULL;
struct PoNode * find = p;
struct PoNode * pre = NULL;
while (find!=NULL && find->ex < ex){
if(find->ex == ex){
free(q);
printf("【插入失败】:原本就有相同指数的项\n");
return 0;
}
pre = find;
find=find->next;
}
q->next = find;
if (pre==NULL) {
p=q;
} else {
pre->next = q;
}
printf("【插入成功】:%dX^%d \n", q->co, q->ex);
return 1;
}
int InitPolynomial(Polynomial &p, Polynomial &q) {
InsertNode(p, 2, 0);
InsertNode(p, 4, 3);
InsertNode(p, -1, 6);
InsertNode(p, 5, 7);
printPolynomial(p);
InsertNode(q, 1, 3);
InsertNode(q, 2, 7);
InsertNode(q, -2, 8);
printPolynomial(q);
}
int Addition (Polynomial &a, Polynomial &b){
if (a == NULL || b == NULL)
return 0;
struct PoNode *p=a, *q=b;
struct PoNode *pPre=NULL;
while (p!=NULL && q!= NULL) {
if (p->ex < q->ex) {
pPre = p;
p = p->next;
} else if (p->ex > q->ex) {
if (pPre == NULL){
a = q;
q = q->next;
a->next = p;
pPre = a;
} else {
struct PoNode * s = q;
q = q->next;
s->next = p;
pPre->next = s;
pPre = s;
}
} else {
p->co += q->co;
pPre = p;
p = p->next;
struct PoNode * s = q;
q = q->next;
free(s);
}
}
if (p==NULL) {
pPre->next = q;
}
}
6 对单链表实现基数排序 (对应:补充算法题第二题)
大家可以在自己电脑上新建一个 C++ 项目,并把这段代码复制到 main 函数前边,然后在你的 main 函数里调用 test_RadixSort() 这个函数。跑一跑看看效果
提示:用电脑网页版看,体验更好一些
注:这段代码对应强化课“补充算法题”第二题
#ifndef CODE_RADIXSORT_H
#define CODE_RADIXSORT_H
#include "random"
#include "stdio.h"
typedef struct RaNode {
int key;
struct RaNode * next;
} Node;
typedef struct {
RaNode * front;
RaNode * rear;
} Queue;
int getRadix(int key, int d){
if(d==1) return key % 10;
if(d==2) return (key/10) % 10;
if(d==3) return (key/100) % 10;
return -1;
}
void distribute (RaNode * head, int k, Queue list[]) {
while (head->next!=NULL){
RaNode * p = head->next;
head->next = p->next;
p->next=NULL;
int r = getRadix(p->key, k);
if (list[r].front == NULL){
list[r].front = p;
list[r].rear = p;
} else {
list[r].rear->next = p;
list[r].rear = p;
}
}
}
void collect(RaNode * head, int k, Queue list[]) {
for (int i=9; i>=0; i--) {
if (list[i].front == NULL) continue;
list[i].rear->next = head->next;
head->next = list[i].front;
list[i].front = NULL;
list[i].rear = NULL;
}
}
int RadixSort(RaNode * head) {
Queue list[10];
for (int i=0; i<10; i++){
list[i].front = NULL;
list[i].rear = NULL;
}
for (int r=1; r<=3; r++){
distribute(head, r, list);
collect(head, r, list);
}
}
void printList (RaNode * head) {
RaNode * p = head;
printf("【链表的值】:");
while (p->next!=NULL) {
printf("%d ", p->next->key);
p = p->next;
}
printf("\n");
}
int test_RadixSort() {
RaNode * head = (RaNode *) malloc (sizeof(RaNode));
head->next = NULL;
for (int i=0; i<100; i++){
RaNode * p = (RaNode *) malloc (sizeof(RaNode));
p->key = rand() % 1000;
p->next = head->next;
head->next = p;
}
printList(head);
RadixSort(head);
printList(head);
}