关于数组和链表的几个必知必会的代码实现

数组

实现一个支持动态扩容的数组

#include<iostream>

using namespace std;

class DArray {
private:	
	int *da;		// 指向分配的内存空间
	int size;		// 当前数组的真实大小
	int capacity;   // 允许的最大元素数,即预分配的内存空间
public:
	DArray();
	void Expand();	// 扩容
	void Push(int value);	//向尾端push元素
	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 {	// 1 2 3 5    4   
		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;
	}
	// 找到要插入的位置 1 1 1   2
	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);
	//head.Delete(3);
	//head.Reverse();
	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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值