#ifndef Struct_h
#define Struct_h
struct INFO
{
int value;
};
struct Linklist {
INFO info;
struct Linklist* next;
};
#endif
定义函数列表
#ifndef func_h
#define func_h
struct Linklist* Create(struct INFO info);
struct Linklist* Get(struct Linklist* head, int de);
void CopyInfo(INFO* out, INFO* in);
int Length(struct Linklist* head);
int Insert(struct Linklist* head, struct INFO info, int de);
int Add(struct Linklist* head, struct INFO info);
int Delete(struct Linklist* head, int de);
void Print(struct Linklist* ctl);
void PrintAll(struct Linklist* head);
void Destroy(struct Linklist* head);
void BubbleSort(struct Linklist* head);
void SelectSort(struct Linklist* head);
void InsertSort(struct Linklist* head);
void QuickSort(struct Linklist* head, struct Linklist* tail) ;
#endif
主要操作函数
#include<stdio.h>
#include<stdlib.h>
#include"struct.h"
#include"func.h"
struct Linklist* Create(struct INFO info) {
struct Linklist* head;
if ((head = (struct Linklist*)malloc(sizeof(struct Linklist))) == NULL) {
return NULL;
}
CopyInfo(&head->info, &info);
head->next = NULL;
return head;
}
struct Linklist* Get(struct Linklist* head, int de) {
struct Linklist* ctl = head;
if (de < 0)return NULL;
while ((de--)&&ctl!=NULL)ctl = ctl->next;
return ctl;
}
void CopyInfo(INFO* out, INFO* in) {
out->value = in->value;
}
int Length(struct Linklist* head) {
int len = 0;
struct Linklist* ctl = head;
while (ctl != NULL) {
ctl = ctl->next;
len++;
}
return len;
}
int Insert(struct Linklist* head, struct INFO info, int de) {
if (de<0 || de>Length(head))return -1;
struct Linklist* ins;
if ((ins = (struct Linklist*)malloc(sizeof(struct Linklist))) == NULL) return -2;
CopyInfo(&ins->info, &info);
struct Linklist* ctl = head;
while (de--)ctl = ctl->next;
ins->next = ctl->next;
ctl->next = ins;
return 0;
}
int Add(struct Linklist* head, struct INFO info) {
return (Insert(head, info, Length(head) - 1));
}
int Delete(struct Linklist* head, int de) {
if (de<0 || de>Length(head))return -1;
struct Linklist* ctl = head;
if (de == 0) {
head = head->next;
free(ctl);
return 0;
}
while ((de--) - 1)ctl = ctl->next;
ctl->next = (ctl->next)->next;
free(ctl->next);
return 0;
}
void Print(struct Linklist* ctl) {
printf("%d\n", ctl->info.value);
}
void PrintAll(struct Linklist* head) {
struct Linklist* ctl = head;
while (ctl != NULL) {
Print(ctl);
ctl = ctl->next;
}
}
void Destroy(struct Linklist* head) {
struct Linklist* ctl = head->next;
struct Linklist* p = head;
while (ctl != NULL) {
p = ctl;
ctl = ctl->next;
free(p);
}
free(head);
}
void BubbleSort(struct Linklist* head) {
int len = Length(head);
struct Linklist* ctl = head;
for (int i = 0; i < len ; i++) {
ctl = head;
for (int j = 0; j < len - i - 1 ; j++) {
if (ctl->info.value > ctl->next->info.value) {
int temp = ctl->info.value;
ctl->info.value = ctl->next->info.value;
ctl->next->info.value = temp;
}
ctl = ctl->next;
}
}
}
void SelectSort(struct Linklist* head) {
struct Linklist* ctl=head;
int len = Length(head);
struct INFO max;
for (int i = 0; i < len; i++) {
ctl = head;
max.value = head->info.value;
int a = len;
int pos = 0;
for (int j = 0; j < len - i; j++) {
if (max.value < ctl->info.value) {
max.value = ctl->info.value;
pos = j;
}
ctl = ctl->next;
}
struct Linklist* temp_1 = Get(head, pos);
struct Linklist* temp_2 = Get(head, len-1-i);
struct INFO temp = temp_1->info;
temp_1->info = temp_2->info;
temp_2->info = temp;
}
}
void InsertSort(struct Linklist* head) {
struct Linklist* ctl = head;
struct Linklist* a = NULL, * b = NULL;
for (int i = 0; i < Length(head); i++) {
a = Get(head, i);
for (int j = i - 1; j >= 0; j--) {
b = Get(head, j);
if (a->info.value > b->info.value) {
b->next = a->next;
a->next = b;
}
else {
b->next = a;
}
}
}
}
void QuickSort(struct Linklist* head, struct Linklist* tail) {
if (head!=tail) {
int key = head->info.value;
struct Linklist* p = head;
struct Linklist* q = head->next;
while (q != tail) {
if (q->info.value < key) {
p = p->next;
int temp = p->info.value;
p->info.value = q->info.value;
q->info.value = temp;
}
q = q->next;
}
int temp = p->info.value;
p->info.value = head->info.value;
head->info.value = temp;
QuickSort(head, p);
QuickSort(p->next, tail);
}
}
主函数(示例)
#include <iostream>
#include<stdlib.h>
#include"func.h"
#include"struct.h"
int main()
{
INFO input;
int n = 0;
scanf_s("%d", &n);
scanf_s("%d", &input.value);
struct Linklist* in = Create(input);
for (int i = 1; i <= n - 1; i++) {
scanf_s("%d", &input.value);
Add(in, input);
}
QuickSort(in,NULL);
PrintAll(in);
Destroy(in);
}
Comments | NOTHING