使用场景
顺序表直接查找,链表空间方便阔容插入删除方便
C 实现
顺序表
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COLOR(a, b) "\033[" #b "m" a "\033[0m"
#define GREEN(a) COLOR(a, 32)
typedef struct Vector {
int *data;
int size, length;
} Vec;
Vec *init(int n) {
Vec *v = (Vec *) malloc(sizeof(Vec));
v->data = (int *) malloc(sizeof(int) * n);
v->size = n;
v->length = 0;
return v;
}
int expand(Vec *v) {
int extr_size = v->size;
int *p;
// realloc 自动回收,考虑已分配好内存块的重新分配
// 申请的内存变小,首地址可能不变,变大可能变,基本可能很少便
while (extr_size) {
p = (int *) realloc(v->data, sizeof(int) * (v->size + extr_size));
if (p) break;
extr_size >>= 1;
}
if (extr_size == 0) return 0;
v->data = p;
v->size += extr_size;
return 1;
}
void clear(Vec *v) {
if (v == NULL) return;
free(v->data);
free(v);
return ;
}
int insert(Vec *v, int val, int ind) {
if (v == NULL) return 0;
if (ind < 0 || ind > v->length) return 0;
if (v->length == v->size) {
if (!expand(v)) return 0;
printf(GREEN("success to expand! the Vector size is %d\n"), v->size);
}
for (int i = v->length; i > ind; i--) {
v->data[i] = v->data[i-1];
}
v->data[ind] = val;
v->length += 1;
return 1;
}
int erase(Vec *v, int ind) {
if (v == NULL) return 0;
if (ind < 0 || ind >= v->length) return 0;
for (int i = ind + 1; i < v->length; ++i) {
v->data[i - 1] = v->data[i];
}
v->length -= 1;
return 1;
}
void output(Vec *v) {
if (v == NULL) return ;
printf("Vector : [");
for (int i = 0; i < v->length; ++i) {
i && printf(",");
printf("%d", v->data[i]);
}
printf("]\n");
return ;
}
int main() {
srand(time(0));
#define max_op 20
Vec *v = init(1);
for (int i = 0; i < max_op; ++i) {
int val = rand() % 100;
int ind = rand() % (v->length + 3) - 1;
int op = rand() % 4;
switch (op) {
case 0:
case 1:
case 2: {
printf("insert %d at %d to Vector = %d\n", val, ind, insert(v, val, ind));
break;
}
case 3: {
printf("erase a iterm at %d from Vector = %d\n", ind, erase(v, ind));
break;
}
}
output(v);
printf("\n");
}
clear(v);
#undef max_op
return 0;
}
链表
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COLOR(a, b) "\033[" #b "m" a "\033[0m"
#define GREEN(a) COLOR(a, 32)
typedef struct ListNode {
int data;
struct ListNode *next;
} ListNode;
typedef struct List {
ListNode head;
int length;
} List;
ListNode *getNewNode(int);
List *getLinkList();
void clear_node(ListNode *);
void clear(List *);
int insert(List *, int, int);
int erase(List *, int);
void output(List *);
void earse(List *);
ListNode *getNewNode(int val) {
ListNode *p = (ListNode *) malloc(sizeof(ListNode));
p->data = val;
p->next = NULL;
return p;
}
List *getLinkList() {
List *l = (List *) malloc(sizeof(List));
l->head.next = NULL;
l->length = 0;
return l;
}
int insert(List *l, int ind, int val) {
if (l == NULL) return 0;
if (ind < 0 || ind > l->length) return 0;
ListNode *p = &(l->head), *node = getNewNode(val);
while (ind--) p = p->next;
node->next = p->next;
p->next = node;
l->length += 1;
return 1;
}
void clear_node(ListNode *node) {
if (node == NULL) return;
free(node);
return ;
}
void clear(List *l) {
if (l == NULL) return;
ListNode *p = l->head.next, *q;
while (p) {
q = p->next;
clear_node(p);
p = q;
}
free(l);
return ;
}
void reverse(List *l) {
if (l == NULL) return;
ListNode *p = l->head.next, *q;
l->head.next = NULL;
while (p) {
q = p->next;
p->next = l->head.next;
l->head.next = p;
p = q;
}
return ;
}
int erase(List *l, int ind) {
if (l == NULL) return 0;
if (ind < 0 || ind >= l->length) return 0;
ListNode *p = &(l->head), *q;
while(ind--) p = p->next;
q = p->next;
p->next = q->next;
free(q);
l->length -= 1;
return 1;
}
void output(List *l) {
if (l == NULL) return ;
printf("List(%d) = [", l->length);
for (ListNode *p = l->head.next; p; p = p->next) {
printf("%d->", p->data);
}
printf("NULL]\n");
return ;
}
int main() {
srand(time(0));
#define max_op 20
List *l = getLinkList();
for (int i = 0; i < max_op; ++i) {
int val = rand() % 100;
int ind = rand() % (l->length + 3) - 1;
int op = rand() % 4;
switch (op) {
case 0:
case 1:
printf("insert %d at %d to List = %d\n", val, ind, insert(l, ind, val));
break;
case 2:
printf("earse a iterm at %d from List = %d\n", ind, erase(l, ind));
break;
case 3:
printf(GREEN("reverse the list!\n"));
reverse(l);
break;
}
output(l), printf("\n");
}
#undef max_op
clear(l);
return 0;
}
C++ 实现
class Vector {
private:
int size, length;
int *data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new int[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, int value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
expand();
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void expand() {
Type *old_data = data;
size = size * 2;
data = new int[size];
for (int i = 0; i < length; ++i) {
data[i] = old_data[i];
}
delete[] old_data;
}
bool search(const int &value) {
for (int i = 0; i < length; ++i) {
if (data[i] == value) {
return true;
}
}
return false;
}
bool remove(int index) {
if (index < 0 || index >= length) {
return false;
}
for (int i = index + 1; i < length; ++i) {
data[i - 1] = data[i];
}
length = length - 1;
return true;
}
void print() {
for (int i = 0; i < length; ++i) {
if (i > 0) {
cout << " ";
}
cout << data[i];
}
cout << endl;
}
};
class Node {
public:
Type data;
Node *next;
Node(const &_data) {
data = _data;
next = NULL;
}
};
class LinkedList {
private:
Node *head;
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
Node *current_node = head;
while (current_node != NULL) {
Node *delete_node = current_node;
current_node = current_node->next;
delete delete_node;
}
}
bool insert(Node *node, int index) {
if (head == NULL) {
if (index != 0) {
return false;
}
head = node;
return true;
}
if (index == 0) {
node->next = head;
head = node;
return true;
}
Node *current_node = head;
int count = 0;
while (current_node->next != NULL && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next = node;
return true;
}
return false;
}
void output() {
if (head == NULL) {
return;
}
Node *current_node = head;
while (current_node != NULL) {
cout << current_node->data << " ";
current_node = current_node->next;
}
cout << endl;
}
bool delete_node(int index) {
if (head == NULL) {
return false;
}
Node *current_node = head;
int count = 0;
if (index == 0) {
head = head->next;
delete current_node;
return true;
}
while (current_node->next != NULL && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1 && current_node->next != NULL) {
Node *delete_node = current_node->next;
current_node->next = delete_node->next;
delete delete_node;
return true;
}
return false;
}
void reverse() {
if (head == NULL) {
return;
}
Node *next_node, *current_node;
current_node = head->next;
head->next = NULL;
while (current_node != NULL){
next_node = current_node->next;
current_node->next = head;
head = current_node;
current_node = next_node;
}
}
};