//
// Created by 28339 on 2023/5/16.
//
#include <malloc.h>
#include "sequence_table.h"
SequenceTable *init() {
SequenceTable *head = (SequenceTable *) malloc(sizeof(SequenceTable));
head->len = 0;
return head;
}
void tail_insertion(SequenceTable *head, DATA data) {
if (head) {
if (head->len >= MAX) {
puts("满了已经\n");
} else {
head->data[head->len] = data;
head->len++;
}
} else {
puts("请先初始化\n");
}
}
void tail_remove(SequenceTable *head) {
if (head) {
if (head->len == 0) {
puts("空了已经\n");
} else {
head->len--;
}
} else {
puts("请先初始化\n");
}
}
void traversal(SequenceTable *head) {
if (head) {
for (int i = 0; i < head->len; ++i) {
printf("%d\t", head->data[i]);
}
puts("\n");
} else {
puts("请先初始化\n");
}
}
void index_find(SequenceTable *head, int index) {
if (head) {
if (index < 0 || index >= head->len) {
puts("下标不存在\n");
} else {
printf("%d\n", head->data[index]);
}
} else {
puts("请先初始化\n");
}
}
void index_update(SequenceTable *head, int index, DATA data) {
if (head) {
if (index < 0 || index >= head->len) {
puts("此位置不存在数据\n");
} else {
head->data[index] = data;
}
} else {
puts("请先初始化\n");
}
}
void index_insertion(SequenceTable *head, int index, DATA data) {
if (head) {
if (index < 0 || index > head->len || head->len >= MAX) {
puts("此位置无法添加\n");
} else {
int l = head->len;
head->data[l] = data;
for (; l > index; --l) {
head->data[l] = head->data[l - 1];
}
head->len++;
}
} else {
puts("请先初始化\n");
}
}
void index_remove(SequenceTable *head, int index) {
if (head) {
if (index < 0 || index >= head->len) {
puts("此位置无法删除\n");
} else {
for (int i = index; i < head->len - 1; ++i) {
head->data[i] = head->data[i + 1];
}
head->len--;
}
} else {
puts("请先初始化\n");
}
}
int value_find(SequenceTable *head, DATA data) {
if (head) {
for (int i = 0; i < head->len; ++i) {
if (data == head->data[i]) {
printf("值%d在第%d位\n", data, i);
return i;
}
}
puts("没找到\n");
return -1;
} else {
puts("请先初始化\n");
return -1;
}
}
void value_update(SequenceTable *head, DATA s_data, DATA u_data) {
int index = value_find(head, s_data);
if (index >= 0) {
index_update(head, index, u_data);
}
}
void value_insertion(SequenceTable *head, DATA s_data, DATA u_data) {
int index = value_find(head, s_data);
if (index >= 0) {
index_insertion(head, index, u_data);
}
}
void value_remove(SequenceTable *head, DATA s_data) {
int index = value_find(head, s_data);
if (index >= 0) {
index_remove(head, index);
}
}
void bubble_sort(SequenceTable *head) {
for (int i = 0; i < head->len - 1; ++i) {
for (int j = 0; j < head->len - i - 1; ++j) {
if (head->data[j] > head->data[j + 1]) {
head->data[j] = head->data[j] ^ head->data[j + 1];
head->data[j + 1] = head->data[j] ^ head->data[j + 1];
head->data[j] = head->data[j] ^ head->data[j + 1];
}
}
}
}
void select_sort(SequenceTable *head) {
for (int i = 0; i < head->len - 1; ++i) {
for (int j = i; j < head->len - 1; ++j) {
if (head->data[i] > head->data[j]) {
head->data[i] = head->data[i] ^ head->data[j];
head->data[j] = head->data[i] ^ head->data[j];
head->data[i] = head->data[i] ^ head->data[j];
}
}
}
}
void remove_repeated(SequenceTable *head) {
for (int i = 0; i < head->len; ++i) {
for (int j = i; j < head->len; ++j) {
if (head->data[i] == head->data[j]) {
index_remove(head, j);
head->len--;
j--;
}
}
}
}
void montage(SequenceTable *head, SequenceTable *next) {
if (head && next) {
for (int i = 0; i < next->len; ++i) {
tail_insertion(head, next->data[i]);
}
puts("\n");
} else {
puts("请先初始化\n");
}
}
数据结构:顺序表
最新推荐文章于 2024-08-16 22:20:39 发布