c++线性表数组实现集合

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e8 + 10;
//线性表集合
//queue
int queue[N],queue_head = 1,queue_tail = 1;
int queue_front() {
    return queue[queue_head];
}
void queue_push(int number) {
    queue[queue_tail++] = number;
}
void queue_pop() {
    queue_head ++;
}
void print_queue() {
	for (int i = queue_head;i < queue_tail;i ++) {
		cout << queue[i] << " ";
	}
	puts("");
}
bool empty_queue() {
	return queue_head == queue_tail;
}
int queue_length() {
	return queue_tail - queue_head;
}
//deque
int deque[N],deque_head = 1e4,deque_tail = 1e4;
void deque_push_front(int number) {
    deque[--deque_head] = number;
}
void deque_pop_front() {
    deque_head ++;
}
int deque_front() {
    return deque[deque_head];
}
void deque_push_back(int number) {
    deque[deque_tail++] = number;
}
void deque_pop_back() {
    deque_tail --;
}
int deque_back() {
    return deque[deque_tail];
}
int deque_length() {
	return deque_tail - deque_head;
}
void print_deque() {
	for (int i = deque_head;i < deque_tail;i ++) {
		cout << deque[i] << " ";
	}
	puts("");
}
bool empty_deque() {
	return deque_head == deque_tail;
}
//stack
int stack[N],stack_head = 1;
void stack_push(int number) {
    stack[stack_head++] = number;
}
void stack_pop() {
    stack_head --;
}
int stack_top() {
    return stack[stack_head];
}
void print_stack() {
	for (int i = 1;i < stack_head;i ++) {
		cout << stack[i] << " ";
	}
	puts("");
}
bool empty_stack() {
	return stack_head == 1;
}
int stack_high() {
	return stack_head - 1;
}
//list

struct ListNode
{
	int data;
	ListNode* next;//结构体指针
};
void Listprintf(ListNode* phead)
{
	ListNode* cur=phead;
	while (cur != NULL)
	{
		cout << cur->data << "->";
		cur = cur->next;
	}
	cout << "NULL" << endl;
}
//尾插
void Listpushback(ListNode** pphead, int x)
{
	ListNode* newnode = new ListNode{ x,NULL };
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		ListNode* tail=  *pphead;
		while(tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
//头插
void Listpushfront(ListNode** pphead, int x)
{
	ListNode* newnode = new ListNode{ x,NULL };
	newnode->next = *pphead;
	*pphead = newnode;
}
//尾删
void Listpopback(ListNode** pphead)
{
	if (*pphead == NULL)
	{
		return;
	}
	if ((*pphead)->next == NULL)
	{
		delete(*pphead);
		*pphead = NULL;
	}
	else
	{
		ListNode* tail = *pphead;
		ListNode* prev = NULL;
		while (tail->next)
		{
			prev = tail;
			tail = tail->next;
		}
		delete(tail);
		tail = NULL;
		prev->next = NULL;
	}
}
//头删
void Listpopfront(ListNode** pphead)
{
	if (*pphead == NULL)
	{
		return;
	}
	else
	{
		ListNode* newnode = (*pphead)->next;
		delete(*pphead);
		*pphead = newnode;
	}
}
//查找元素,返回值是地址
ListNode* Listfind(ListNode* phead, int x)
{
	ListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}
//插入元素,在pos的前一个位置插入
//配合Listfind使用,具体使用见test_insert函数
void Listinsert(ListNode** phead, ListNode* pos, int x)
{
	ListNode* newnode = new ListNode{ x,NULL };
	if (*phead == pos)
	{
		newnode->next = (*phead);
		*phead = newnode;
	}
	else
	{
		ListNode* posprev = *phead;
		while (posprev->next != pos)
		{
			posprev = posprev->next;
		}
		posprev->next = newnode;
		newnode->next = pos;
	}
}
void Listinsert_after(ListNode** phead, ListNode* pos, int x)
{
	ListNode* newnode = new ListNode{ x,NULL };
	newnode->next = pos->next;
	pos->next = newnode;
}
//删除指定位置的节点
void Listerase(ListNode** pphead, ListNode* pos)
{
	if (*pphead == pos)
	{
		*pphead = pos->next;
		delete(pos);
	}
	else
	{
		ListNode* prev = *pphead;
		while (prev->next!=pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		delete(pos);
	}
}
//释放链表
void Listdestory(ListNode** pphead)
{
	ListNode* cur = *pphead;
	while(cur)
	{
		ListNode* next = cur->next;
		delete(cur);
		cur = next;
	}
	*pphead = NULL;
}
//实操
signed main() {

    return 0;
}

                
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值