add 大一时期学长写的反转链表.

This commit is contained in:
2022-03-03 21:43:32 +08:00
parent 7fa640f533
commit ca13b86afc
2 changed files with 111 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
/*1025 反转链表 25 分)
给定一个常数 K 以及一个单链表 L请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6K 为 3则输出应该为 3→2→1→6→5→4如果 K 为 4则输出应该为 4→3→2→1→5→6即最后不到 K 个元素不反转。
输入格式:
每个输入包含 1 个测试用例。每个测试用例第 1 行给出第 1 个结点的地址、结点总个数正整数 N (≤105)、以及正整数 K (≤N),即要求反转的子链结点的个数。结点的地址是 5 位非负整数NULL 地址用 1 表示。
接下来有 N 行,每行格式为:
Address Data Next
其中 Address 是结点地址Data 是该结点保存的整数数据Next 是下一结点的地址。
输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同
输入样例:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
输出样例:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
*/
//#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
typedef struct LinkList
{
int Address;
int Data;
int Next;
}linklist;
int main()
{
linklist list[100000], *p1, *p2;
int h_address, num, r_num, p_address, n_address, cnt = 0, tmp;
p1 = (linklist*)malloc(sizeof(struct LinkList));
p2 = (linklist*)malloc(sizeof(struct LinkList));
scanf("%d %d %d", &h_address, &num, &r_num);
getchar();
for (int i = 0; i < num; i++)
{
int address, data, next;
scanf("%d %d %d", &address, &data, &next);
getchar();
list[address].Address = address;
list[address].Data = data;
list[address].Next = next;
}
p1 = &list[h_address];
for (;;)
{
cnt++;
if (p1->Next == -1)
break;
p1 = &list[p1->Next];
}
tmp = h_address;
while (cnt >= r_num)
{
p1 = &list[tmp];
p_address = tmp;
n_address = p1->Next;
for (int i = 0; i < r_num; i++)
{
if (i)
{
n_address = p1->Next;
p1->Next = p_address;
p_address = p1->Address;
}
p1 = &list[n_address];
}
if (p2 != NULL)
p2->Next = p_address;
if (!i)
h_address = p_address;
list[tmp].Next = n_address;
p2 = &list[tmp];
tmp = p1->Address;
cnt -= r_num;
i++;
}
p1 = &list[h_address];
for (;;)
{
if (p1->Next >= 0)
printf("%05d %d %05d\n", p1->Address, p1->Data, p1->Next);
else
printf("%05d %d %d\n", p1->Address, p1->Data, p1->Next);
if (p1->Next == -1)
break;
p1 = &list[p1->Next];
}
return 0;
}

274
c/霍夫曼编码/main.c Normal file
View File

@@ -0,0 +1,274 @@
//
// main.c
// 霍夫曼编码
// 暂时只处理26个小写字母
// Created by licsber on 2018/11/15.
// Copyright © 2018 licsber. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 26
#define START 'a'
typedef struct node{
char ch;
int weight;
struct node *lchild;
struct node *rchild;
}node, *p_node;
typedef struct sheet{
char ch;
char code[ARRAY_SIZE];
struct sheet *next;
} sheet, *p_sheet;
enum error_code{
CANNOT_ALLOCATE_MEMORY,
INPUT_IS_EMPTY
};
p_node container[ARRAY_SIZE];
char code[ARRAY_SIZE];
p_sheet line = NULL;
void error(int); // 报错
void init(void); // 初始化
void add(void); //读取元素加入树
void printWeight(void); // 输出权重
p_node build(void); // 合并两个节点 返回父节点
void freeEmpty(void); // 把权重为0的节点内存释放
p_node create(void); // 建立霍夫曼树
void freeNode(int); // 释放掉权重为0的节点
int getCount(void); // 获取权重非0节点数目
void sort(void); // 从小到大排序
void outputTree(p_node, int); // 递归输出哈夫曼树
void output(void);
p_sheet initSheet(void);
int main() {
init();
p_node root = NULL;
for (int action; ; ) {
printf("please select the action:\n");
printf("\
0.初始化\n\
1.从输入构建树\n\
2.获取非空终端节点个数\n\
3.输出字符权重\n\
4.建树\n\
5.输出字符编码\n\
6.把字符编码放进单向链表\n\
4.\n\
4.\n\
9.退出\n\
\n");
scanf("%d", &action);
getchar(); // 把缓存区里的换行符清掉
switch (action) {
case 0:
init();
break;
case 1:
add();
freeEmpty();
sort();
line = initSheet();
break;
case 2:
printf("the number of node is %d\n", getCount());
break;
case 3:
printWeight();
break;
case 4:
root =create();
break;
case 5:
if (root == NULL) {
root = create();
}
outputTree(root, 0);
break;
case 6:
output();
break;
case 9:
return 0;
default:
printf("worng input\n");
continue;
}
}
}
void error(int err_code){
switch (err_code) {
case CANNOT_ALLOCATE_MEMORY:
printf("cannot allocate memory!\n");
break;
case INPUT_IS_EMPTY:
printf("input is empty!\n");
break;
}
exit(-1);
}
void init(void){
for (int i = 0; i < ARRAY_SIZE; i++) {
container[i] = (p_node)malloc(sizeof(node));
if (!container[i]) {
error(CANNOT_ALLOCATE_MEMORY);
}
container[i] -> ch = START + i;
container[i] -> weight = 0;
container[i] -> lchild = NULL;
container[i] -> rchild = NULL;
}
printf("Init is done!\n");
}
void freeNode(int i){
free(container[i]);
container[i] = NULL;
}
void add(void){
char ch;
while ((ch = getchar()) != '\n') {
container[ch - START] -> weight++;
}
}
void sort(void){
for (int i = 0; i < ARRAY_SIZE; i++) {
if (!container[i]) {
for (int j = i; j < ARRAY_SIZE; j++) {
if (container[j]) {
container[i] = container[j];
container[j] = NULL;
break;
}
}
}
}
for (int i = 0; i < getCount(); i++) {
for (int j = 0; j < i; j++) {
if (container[i] -> weight < container[j] -> weight) {
p_node tmp = container[i];
container[i] = container[j];
container[j] = tmp;
}
}
}
}
int getCount(void){
int count = ARRAY_SIZE;
for (int i = 0; i < ARRAY_SIZE; i++) {
if (!container[i] ||
!container[i] -> weight) {
count--;
}
}
return count;
}
void printWeight(void){
for (int i = 0; i < ARRAY_SIZE; i++) {
if (!container[i]) {
continue;
}
printf("%c weighs %d\n",
container[i] -> ch,
container[i] -> weight);
}
}
p_node build(void){
p_node tmp = (p_node)malloc(sizeof(node));
if (tmp == NULL) {
error(CANNOT_ALLOCATE_MEMORY);
}
tmp -> ch = '\0';
tmp -> weight = container[0] -> weight +
container[1] -> weight;
tmp -> lchild = container[0];
tmp -> rchild = container[1];
return tmp;
}
void freeEmpty(void){
for (int i = 0; i < ARRAY_SIZE; i++) {
if (!container[i]) {
continue;
}
if (!container[i] -> weight) {
freeNode(i);
}
}
}
p_node create(void){
if (!container[1]) {
return container[0];
}
container[0] = build();
container[1] = NULL;
sort();
return create();
}
void outputTree(p_node root, int layer){
if (root -> lchild && root -> rchild) {
code[layer] = '0';
outputTree(root -> lchild, layer + 1);
code[layer] = '1';
outputTree(root -> rchild, layer + 1);
code[layer] = '\0';
}
else{
code[layer] = '\0';
printf("%c -> %s\n", root -> ch, code);
}
}
void output(void){
int weight = 0, count = 0, tmp = 0;
for (int i = 0; i < getCount(); i++) {
weight = container[i] -> weight;
if (tmp != weight) {
count++;
}
for (int j = 0; j < count; j++) {
putchar(container[i] -> ch);
}
tmp = weight;
}
putchar('\n');
}
p_sheet initSheet(void){
p_sheet head = (p_sheet)malloc(sizeof(sheet));
head -> next = NULL;
if (!head) {
error(CANNOT_ALLOCATE_MEMORY);
}
for (int i = 0; i < getCount(); i++) {
p_sheet tmp = (p_sheet)malloc(sizeof(sheet));
if (!tmp) {
error(CANNOT_ALLOCATE_MEMORY);
}
head -> ch = container[i] -> ch;
head -> code[0] = '\0';
if (!container[i+1]) {
free(tmp);
continue;
}
tmp -> next = head;
head = tmp;
}
return head;
}