数组
实现一个支持动态扩容的数组
#include<iostream>
using namespace std;
class DArray {
private:
int *da;
int size;
int capacity;
public:
DArray();
void Expand();
void Push(int value);
void Display();
int GetSize();
};
DArray::DArray() {
da = new int[10];
size = 0;
capacity = 10;
}
void DArray::Expand() {
int *old_da = da;
int *new_da = new int[capacity*2];
da = new_da;
for (int i = 0; i < size; i++) {
*new_da++ = *old_da++;
}
}
void DArray::Push(int value) {
if (size == capacity) {
Expand();
}
int *push_da = da;
for (int i = 0; i < size; i++) {
*push_da++;
}
*push_da = value;
size++;
}
void DArray::Display() {
int *show_da = da;
for (int i = 0; i < size; i++) {
cout << *show_da++ << " ";
}
}
int DArray::GetSize() {
return size;
}
int main()
{
DArray darray;
darray.Push(1);
darray.Push(2);
darray.Push(3);
darray.Push(4);
darray.Push(5);
darray.Push(6);
darray.Push(7);
darray.Push(8);
darray.Push(9);
darray.Push(10);
darray.Push(11);
darray.Push(12);
darray.Push(13);
darray.Push(14);
darray.Push(15);
darray.Display();
cout << "\nsize: " << darray.GetSize() << endl;
return 0;
}
实现一个大小固定的有序数组,支持动态增删改操作
#include<iostream>
#include<algorithm>
using namespace std;
class FArray {
private:
int fa[20];
int size = 0;
public:
FArray();
void Insert(int value);
void Delete(int value);
void Modify(int loc, int value);
void Display();
int GetSize();
};
FArray::FArray() {
}
void FArray::Insert(int value) {
if (size == 0) {
fa[size] = value;
size++;
}
else if (size == 20) {
cout << "error: 数组已满!" << endl;
return;
}
else {
int loc = 0;
for (; fa[loc] < value && fa[loc]!= -858993460; loc++);
for (int i = size; i > loc; i--) {
fa[i] = fa[i-1];
}
fa[loc] = value;
size++;
}
}
void FArray::Display() {
for (int i = 0; i < size; i++) {
cout << fa[i] << " ";
}
cout << endl;
}
void FArray::Delete(int value) {
int i;
for (i = 0; fa[i] != value; i++);
if (i == size - 1 && fa[size - 1] != value) {
cout << "error: 无此数据!" << endl;
return;
}
for (i; i < size; i++) {
fa[i] = fa[i + 1];
}
size--;
}
void FArray::Modify(int loc, int value) {
if (loc<0 || loc>19 ) {
return;
}
fa[loc] = value;
sort(fa, fa + size);
}
int FArray::GetSize() {
return size;
}
int main()
{
FArray fa;
fa.Insert(1);
fa.Insert(3);
fa.Insert(4);
fa.Insert(5);
fa.Insert(4);
fa.Display();
cout << "size: " << fa.GetSize() << endl;
fa.Delete(4);
fa.Display();
fa.Modify(0, 6);
fa.Display();
return 0;
}
实现两个有序数组合并为一个有序数组
#include<iostream>
using namespace std;
int *Merge(int a[], int b[], int size1, int size2) {
int *c = new int[size1+size2];
int i = 0, j = 0, k = 0;
while (i < size1&&j < size2) {
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while(i<size1)
c[k++] = a[i++];
while (j < size2)
c[k++] = b[j++];
return c;
}
int main()
{
int a[] = { 1,2,3 };
int b[] = { 2,3,4,6 };
int sizea = sizeof(a) / sizeof(a[0]);
int sizeb = sizeof(b) / sizeof(b[0]);
int *c = Merge(a, b, sizea, sizeb);
for (int i = 0; i<sizea+sizeb; i++) {
cout<<c[i]<<" ";
}
return 0;
}
链表
实现单链表、循环链表、双向链表,支持增删操作
单链表
#include<iostream>
using namespace std;
class Node {
public:
int elem;
Node *next;
Node();
};
Node::Node() {
next = NULL;
}
class Link {
private:
Node *head;
int length;
public:
Link();
void Push(int value);
void Delete(int value);
void Display();
void Reverse();
void Merge(Link *l1, Link *l2);
Node *GetMid();
bool isEmpty();
};
Link::Link() {
head = new Node;
length = 0;
}
void Link::Push(int value) {
Node *p = head;
Node *newNode = new Node;
newNode->elem = value;
if (p->next == NULL) {
p->next = newNode;
return;
}
if (p->next->elem > value) {
newNode->next = p->next;
p->next = newNode;
return;
}
while (p->next != NULL) {
if (p->next->elem < value) {
p = p->next;
}
else {
break;
}
}
newNode->next = p->next;
p->next = newNode;
}
void Link::Delete(int value) {
Node *p = head;
while (p->next->elem != value) {
p = p->next;
}
p->next = p->next->next;
}
void Link::Display() {
Node *p = head;
while (p->next) {
cout << p->next->elem << " ";
p = p->next;
}
cout << endl;
}
void Link::Reverse() {
if (head == NULL || head->next == NULL || head->next->next == NULL) {
return;
}
Node *cursor = head;
cursor = cursor->next->next;
head->next->next = NULL;
while (cursor) {
Node *get = cursor;
cursor = cursor->next;
get->next = head->next;
head->next = get;
}
}
void Link::Merge(Link *l1, Link *l2) {
Node *p1 = l1->head;
Node *p2 = l2->head;
while (p1->next!=NULL&&p2->next != NULL) {
if (p1->next->elem >= p2->next->elem) {
Push(p2->next->elem);
p2 = p2->next;
}
else if (p1->next->elem < p2->next->elem) {
Push(p1->next->elem);
p1 = p1->next;
}
}
while (p1->next) {
Push(p1->next->elem);
p1 = p1->next;
}
while (p2->next) {
Push(p2->next->elem);
p2 = p2->next;
}
}
bool Link::isEmpty() {
if (head->next == NULL) {
return true;
}
return false;
}
int main()
{
Link h1, h2, h3;
h1.Push(1);
h1.Push(0);
h1.Push(1);
h1.Push(2);
h1.Display();
h2.Push(1);
h2.Push(2);
h2.Push(2);
h2.Push(3);
h2.Display();
h3.Merge(&h1, &h2);
cout << endl;
if (h3.isEmpty()) {
cout << "空\n";
}
h3.Display();
return 0;
}