V2!!
顺序表
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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;
}
void clear(Vec *v){
if (v == NULL) return;
free(v->data);
free(v);
return;
}
int expand(Vec *v){
int ex_size = v->size;
int *q;
while(ex_size){
q = (int *)realloc(v->data, sizeof(int) * (v->size +ex_size));
if (q) break;
ex_size /= 2;
}
if (ex_size == 0) return 0;
v->data = q;
v->size += ex_size;
return 1;
}
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) return 0;
printf("success to expand!\n");
}
for (int i = v->length; i>ind; i++){
v->data[i] = v->data[i - 1];
}
v->data[ind] = val;
v->length++;
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--;
return 1;
}
void output(Vec *v){
if (v == NULL) return;
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(max_op);
for (int i = 0; i <= max_op; i++){
int val = rand() % 100;
int op = 0;
if (insert(v, val, i)){
printf("insert %d success!\n", val);
}
}
#undef max_op
return 0;
}
单链表
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct ListNode{
int data;
struct ListNode *next;
}ListNode;
typedef struct List{
ListNode head;
int length;
}List;
ListNode *init_node(int val){
ListNode *p = (ListNode *)malloc(sizeof(ListNode));
p->data = val;
p->next = NULL;
return p;
}
List *init_list(){
List *l = (List *)malloc(sizeof(List));
l->head.next = NULL;
l->length = 0;
return l;
}
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;
}
}
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 = init_node(val);
while(ind --) p = p->next;
node->next = p->next;
p->next = node;
l->length++;
return 1;
}
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--;
return 1;
}
void output(List *l){
if (l == NULL) return ;
printf("List(%d) is [", 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 = init_list();
for (int i = 0; i < max_op;i++){
int val = rand() % 100;
if(insert(l, i, val)) printf("insert %d success!\n", val);
}
}
顺序表
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
template <typename Type> class Vector {
private:
int size, length;
Type *data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new Type[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, Type 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 Type[size];
for (int i = 0; i < length; ++i) {
data[i] = old_data[i];
}
delete[] old_data;
}
int search(const Type &value) {
for (int i = 0; i < length; i++){
if (data[i] == value){
return i;
}
}
return -1;
}
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;
}
};
int main() {
Vector<int> a(100);
cout << a.insert(1, 0) << endl;
cout << a.insert(0, 1) << endl;
cout << a.insert(2, 1) << endl;
cout << a.insert(1, 2) << endl;
cout << a.insert(0, 3) << endl;
cout << a.search(1) << endl;
cout << a.search(4) << endl;
return 0;
}
链表
#include <iostream>
#include<iostream>
using namespace std;
template <typename Type> class Node {
public:
Type data;
Node<Type> *next;
Node(const Type &_data) {
data = _data;
next = NULL;
}
};
template <typename Type> class LinkedList {
private:
Node<Type> *head;
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
Node<Type> *current_node = head;
while (current_node != NULL) {
Node<Type> *delete_node = current_node;
current_node = current_node->next;
delete delete_node;
}
}
bool insert(Node<Type> *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<Type> *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<Type> *current_node = head;
while (current_node != NULL) {
cout << current_node->data << " ";
current_node = current_node->next;
}
cout << endl;
}
int delete_node(int index) {
if (head == NULL){
return 0;
}
Node<Type> *current_node = head;
int count = 0;
if (index == 0){
head = head->next;
delete current_node;
return 1;
}
while(current_node->next != NULL && count < index - 1){
count ++;
current_node = current_node->next;
}
if (current_node->next == NULL || count > index - 1){
return 0;
}else if(count == index - 1){
Node<Type> *delete_node = current_node->next;
current_node->next = delete_node->next;
delete delete_node;
return 1;
}
}
void reverse() {
if (head == NULL){
return;
}
Node<Type> *current_node;
Node<Type> *next_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;
}
return;
}
};
int main() {
LinkedList<int> linkedlist;
int num;
while(scanf("%d", &num) != EOF){
if(num == 1){
int loc, data;
scanf("%d%d", &loc, &data);
Node<int> *node = new Node<int>(data);
if(linkedlist.insert(node, loc)){
cout << "success\n";
}else{
cout << "failed\n";
}
}else if(num == 2){
linkedlist.output();
}else if(num == 3){
int loc;
scanf("%d", &loc);
if(linkedlist.delete_node(loc)){
cout << "success\n";
}else{
cout << "failed\n";
}
}else if(num == 4){
linkedlist.reverse();
}
}
return 0;
}
单向循环链表
#include<iostream>
using std::cin;
template <typename Type> class Node {
public:
Type data;
Node<Type> *next;
Node(const Type &_data) {
data = _data;
next = NULL;
}
};
template <typename Type> class LinkedList {
private:
Node<Type> *head;
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
if (head == NULL){
return;
}
Node<Type> *current_node = head->next;
head->next = NULL;
while (current_node != NULL) {
Node<Type> *delete_node = current_node;
current_node = current_node->next;
delete delete_node;
}
}
void insert(Node<Type> *node, int index) {
if (head == NULL) {
if (index != 0) {
return;
}
head = node;
head->next = head;
return;
}
if (index == 0) {
node->next = head->next;
head->next = node;
return;
}
Node<Type> *current_node = head->next;
int count = 0;
while (current_node != head && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next = node;
}
if (node == head->next){
head = node;
}
}
};
int main() {
LinkedList<int> linkedlist;
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++){
Node<int> *node = new Node<int>(i);
linkedlist.insert(node, i - 1);
}
return 0;
}
双向循环链表凭序号翻转
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
template <typename Type> class Node {
public:
Type data;
Node<Type> *next;
Node<Type> *prior;
Node(const Type &_data) {
data = _data;
next = NULL;
}
};
template <typename Type> class LinkedList {
private:
Node<Type> *head;
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
if (head == NULL){
return;
}
Node<Type> *current_node = head->next;
head->next = NULL;
while (current_node != NULL) {
Node<Type> *delete_node = current_node;
current_node = current_node->next;
delete delete_node;
}
}
void insert(Node<Type> *node, int index) {
if (head == NULL) {
if (index != 0) {
return;
}
head = node;
head->next = head;
head->prior = head;
return;
}
Node<Type> *current_node = head->next;
int count = 0;
while (current_node != head && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next->prior = node;
current_node->next = node;
node->prior = current_node;
}
if (node == head->next){
head = node;
}
}
void output() {
if (head == NULL) {
return;
}
Node<Type> *current_node = head->next;
while (current_node->next != head) {
cout << current_node->data << " ";
current_node = current_node->next;
}
cout << head->data;
cout << endl;
}
void reverse_out(int index){
int count = 0;
Node<Type> *current_node = head->next;
while (current_node != head && count < index - 1) {
current_node = current_node->next;
count++;
}
Node<Type> *start_node = current_node->next;
if (count == index - 1){
while(current_node != start_node){
cout << current_node->data << " ";
current_node = current_node->prior;
}
cout << start_node->data;
cout << endl;
}
}
};
int main(){
LinkedList<int> linkedlist;
int num, data;
scanf("%d", &num);
for (int i = 0; i < num; i++){
scanf("%d", &data);
Node<int> *node = new Node<int>(data);
linkedlist.insert(node, i);
}
int start_num;
scanf("%d", &start_num);
linkedlist.reverse_out(start_num);
}