个人笔记-数据结构-线性表

99 阅读5分钟

一、线性表

1.线性表的顺序实现:顺序表(构造、插入、删除、遍历)

#include<iostream>
constexpr auto MaxSize = 100;
struct List
{
	int length;
	//int data[MaxSize];
	int* data;
};

List* Init(int size)
{
	List* list = new List;
	list->length = 0;
	list->data = new int[size];
	return list;
}

bool ListInsert(List* list, int pos, int data)
{
	if (pos < 1 || pos > (list->length + 1) || list->length >= MaxSize) return false;

	for (int i=list->length; i>=pos; i--)
	{
		list->data[i] = list->data[i - 1];
	}
	list->data[pos - 1] = data;
	list->length++;
	return true;
}

bool ListDelete(List* list, int pos)
{
	if (pos < 1 || pos > list->length) return false;
	for (int i=pos; i<list->length; i++)
	{
		list->data[i - 1] = list->data[i];
	}
	list->length--;
	return true;
}

void PrintList(List* list)
{
	for (int i=0; i<list->length; i++)
	{
		printf("%d\n", list->data[i]);
	}
}

int main()
{
	List* list = Init(MaxSize);
	ListInsert(list, 1, 0);
	ListInsert(list, 1, 1);
	ListInsert(list, 1, 2);
	ListInsert(list, 1, 3);
	ListInsert(list, 1, 4);
	//PrintList(list);
	ListDelete(list, 2);
	PrintList(list);
}

2.线性表的链式实现:单链表(构造、插入、删除、遍历)

#include<iostream>

struct Node
{
	int data;
	Node* next;
};

Node* Init()
{
	Node* list = new Node;
	list->data = 0;
	list->next = nullptr;
	return list;
}

void HeadInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	node->next = list->next;
	list->next = node;
	list->data++;
}

void TailInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	Node* head = list;
	while (head->next)
	{
		head = head->next;
	}
	node->next = head->next;
	head->next = node;
	list->data++;
}

void LinkDelete(Node* list, int data)
{
	Node* pre = list;
	Node* cur = list->next;
	while (cur /*cur != nullptr*/)
	{
		if (cur->data == data) 
		{
			Node* temp = cur;
			cur = cur->next;
			pre->next = temp->next;
			list->data--;
			delete(temp);
			continue;
		}
		pre = cur;
		cur = cur->next;
	}
}

void PrintLink(Node* list)
{
	Node* node = list->next;
	while (node)
	{
		printf("%d\n", node->data);
		node = node->next;
	}
}

int main()
{
	Node* list = Init();
	HeadInsert(list, 1);
	HeadInsert(list, 2);
	HeadInsert(list, 2);
	HeadInsert(list, 4);
	HeadInsert(list, 5);
	TailInsert(list, 6);
	TailInsert(list, 7);
	TailInsert(list, 8);
	TailInsert(list, 9);
	TailInsert(list, 10);
	//PrintLink(list);
	LinkDelete(list, 2);
	PrintLink(list);
}


3.单循环链表(构造、插入、删除、遍历)

#include<iostream>

struct Node
{
	int data;
	Node* next;
};

Node* Init()
{
	Node* list = new Node;
	list->data = 0;
	list->next = list;
	return list;
}

void HeadInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	node->next = list->next;
	list->next = node;
	list->data++;
}

void TailInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	Node* head = list;
	while (head->next != list)
	{
		head = head->next;
	}
	node->next = head->next;
	head->next = node;
	list->data++;
}

void LoopLinkDelete(Node* list, int data)
{
	Node* pre = list;
	Node* cur = list->next;
	while (cur != list)
	{
		if (cur->data == data)
		{
			Node* temp = cur;
			cur = cur->next;
			pre->next = temp->next;
			list->data--;
			delete(temp);
			continue;
		}
		pre = cur;
		cur = cur->next;
	}
}

void PrintLoopLink(Node* list)
{
	Node* node = list->next;
	while (node != list)
	{
		printf("%d\n", node->data);
		node = node->next;
	}
}

int main()
{
	Node* list = Init();
	HeadInsert(list, 1);
	HeadInsert(list, 2);
	HeadInsert(list, 2);
	HeadInsert(list, 4);
	HeadInsert(list, 5);
	TailInsert(list, 6);
	TailInsert(list, 7);
	TailInsert(list, 8);
	TailInsert(list, 9);
	TailInsert(list, 10);
	//PrintLoopLink(list);
	LoopLinkDelete(list, 2);
	PrintLoopLink(list);
}

4.双链表(构造、插入、删除、遍历)

#include<iostream>

struct Node
{
	int data;
	Node* pre;
	Node* next;
};

Node* Init()
{
	Node* list = new Node;
	list->data = 0;
	list->pre = nullptr;
	list->next = nullptr;
	return list;
}

void HeadInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	node->next = list->next;
	node->pre = list;
	if (list->data != 0)
	{
		list->next->pre = node;
	}
	list->next = node;
	list->data++;
}

void TailInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	Node* head = list;
	while (head->next)
	{
		head = head->next;
	}
	node->next = head->next;
	node->pre = head;
	head->next = node;
	list->data++;
}

void DoubleLinkDelete(Node* list, int data)
{
	Node* pre = list;
	Node* cur = list->next;
	while (cur /*cur != nullptr*/)
	{
		if (cur->data == data)
		{
			Node* temp = cur;
			cur = cur->next;
			pre->next = temp->next;
			if (temp->next)
			{
				temp->next->pre = pre;
			}
			list->data--;
			delete(temp);
			continue;
		}
		pre = cur;
		cur = cur->next;
	}
}

void PrintDoubleLink(Node* list)
{
	Node* node = list->next;
	while (node)
	{
		printf("%d\n", node->data);
		node = node->next;
	}
}

int main()
{
	Node* list = Init();
	HeadInsert(list, 1);
	HeadInsert(list, 2);
	HeadInsert(list, 2);
	HeadInsert(list, 4);
	HeadInsert(list, 5);
	TailInsert(list, 6);
	TailInsert(list, 7);
	TailInsert(list, 8);
	TailInsert(list, 9);
	TailInsert(list, 2);
	//PrintDoubleLink(list);
	DoubleLinkDelete(list, 2);
	PrintDoubleLink(list);
}

5.双循环链表(构造、插入、删除、遍历)

#include<iostream>

struct Node
{
	int data;
	Node* pre;
	Node* next;
};

Node* Init()
{
	Node* list = new Node;
	list->data = 0;
	list->pre = list;
	list->next = list;
	return list;
}

void HeadInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	node->next = list->next;
	node->pre = list;
	if (list->data == 0)
	{
		list->pre = node;
	}
	else
	{
		node->next->pre = node;
	}
	list->next = node;
	list->data++;
}

void TailInsert(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	node->next = list;
	node->pre = list->pre;
	list->pre->next = node;
	list->pre = node;
	list->data++;
}

void DoubleLoopLinkDelete(Node* list, int data)
{
	Node* pre = list;
	Node* cur = list->next;
	while (cur != list)
	{
		if (cur->data == data)
		{
			Node* temp = cur;
			cur = cur->next;
			pre->next = temp->next;
			temp->next->pre = pre;
			list->data--;
			delete(temp);
			continue;
		}
		pre = cur;
		cur = cur->next;
	}
}

void PrintDoubleLoopLink(Node* list)
{
	Node* node = list->next;
	while (node != list)
	{
		printf("%d\n", node->data);
		node = node->next;
	}
}

int main()
{
	Node* list = Init();
	HeadInsert(list, 1);
	HeadInsert(list, 2);
	HeadInsert(list, 2);
	HeadInsert(list, 4);
	HeadInsert(list, 5);
	TailInsert(list, 6);
	TailInsert(list, 7);
	TailInsert(list, 8);
	TailInsert(list, 9);
	TailInsert(list, 2);
	//PrintDoubleLoopLink(list);
	DoubleLoopLinkDelete(list, 2);
	PrintDoubleLoopLink(list);
}

6.栈(链式)

#include<iostream>

struct Node
{
	int data;
	Node* next;
};

Node* Init()
{
	Node* list = new Node;
	list->data = 0;
	list->next = nullptr;
	return list;
}

void Push(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	node->next = list->next;
	list->next = node;
	list->data++;
}

void Pop(Node* list,int& data)
{
	Node* pre = list;
	Node* cur = list->next;
	if (cur)
	{
		pre->next = cur->next;
		data = cur->data;
		list->data--;
		delete(cur);
	}
}

void PrintStack(Node* list)
{
	Node* node = list->next;
	while (node)
	{
		printf("%d\n", node->data);
		node = node->next;
	}
}

int main()
{
	Node* list = Init();
	Push(list, 1);
	Push(list, 2);
	Push(list, 3);
	Push(list, 4);
	Push(list, 5);
	//PrintStack(list);
	int out;
	Pop(list, out);
	PrintStack(list);
	printf("out == %d\n", out);
}

7.队列(链式)

#include<iostream>

struct Node
{
	int data;
	Node* next;
};

Node* Init()
{
	Node* list = new Node;
	list->data = 0;
	list->next = nullptr;
	return list;
}

void EnQueue(Node* list, int data)
{
	Node* node = new Node;
	node->data = data;
	Node* head = list;
	while (head->next)
	{
		head = head->next;
	}
	node->next = head->next;
	head->next = node;
	list->data++;
}

void DeQueue(Node* list, int& data)
{
	Node* pre = list;
	Node* cur = list->next;
	if (cur)
	{
		pre->next = cur->next;
		data = cur->data;
		list->data--;
		delete(cur);
	}
}

void PrintQueue(Node* list)
{
	Node* node = list->next;
	while (node)
	{
		printf("%d\n", node->data);
		node = node->next;
	}
}

int main()
{
	Node* list = Init();
	EnQueue(list, 1);
	EnQueue(list, 2);
	EnQueue(list, 3);
	EnQueue(list, 4);
	EnQueue(list, 5);
	//PrintQueue(list);
	int out;
	DeQueue(list, out);
	PrintQueue(list);
	printf("out == %d\n", out);
}

8.循环队列(顺序实现)

#include<iostream>

constexpr auto MaxSize = 5;

struct Queue
{
	int front;
	int rear;
	int* data;
};

Queue* Init(int size)
{
	Queue* queue = new Queue;
	queue->front = queue->rear = 0;
	queue->data = new int[size];
	return queue;
}

bool IsFull(Queue* q)
{
	return q->front == (q->rear + 1) % MaxSize ? true : false;
}

void EnQueue(Queue* q, int data)
{
	if (!IsFull(q))
	{
		q->data[q->rear] = data;
		q->rear = (q->rear + 1) % MaxSize;
	}
}

bool IsEmpty(Queue* q)
{
	return q->front == q->rear ? true : false;
}

void DeQueue(Queue* q, int& data)
{
	if (!IsEmpty(q))
	{
		data = q->data[q->front];
		q->front = (q->front + 1) % MaxSize;
	}
}

void PrintLoopQueue(Queue* q)
{
	int length = (q->rear - q->front + MaxSize) % MaxSize;
	int index = q->front;
	for (int i=0; i<length; i++)
	{
		printf("%d\n", q->data[index]);
		index = (index + 1) % MaxSize;
	}
}

int main()
{
	Queue* queue = Init(MaxSize);
	EnQueue(queue, 1);
	EnQueue(queue, 2);
	EnQueue(queue, 3);
	EnQueue(queue, 4);
	//PrintLoopQueue(queue);
	int out;
	DeQueue(queue, out);
	PrintLoopQueue(queue);
	printf("out == %d\n", out);
}