[C/C++]数据结构 栈和队列

一:栈

1.1 栈的概念及结构

        栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底,栈中的数据元素遵守先进后出的原则.

  •         压栈:栈的插入操作叫做进栈/压栈/入栈,将数据插入栈顶
  •         出栈:栈的删除操作也叫出栈,出数据也在栈顶

1.2栈的实现

        栈的实现一般可以用数组或者链表实现,相对而言数组的结构更优一点,因为数组在尾上插入数据的代价更小 ,链表则需从头遍历到尾

支持动态增长的栈:

typedef int STDataType;
typedef struct stack
{
	int* a;
	int top;  //用于维护栈顶
	int capacity;//栈的容量
}ST;

常用功能接口:

//栈的初始化
void STInit(ST* ps);
//压栈
void STPush(ST* ps,STDataType x);
//出栈
void STPop(ST* ps);
//取栈顶元素
STDataType STTop(ST* ps);
//判断栈是否为空
bool STEmpty(ST* ps);
//求栈的大小
int STSize(ST* ps);
//摧毁栈
void STDestroy(ST* ps);

1.栈的初始化

        要注意栈结构中的top可以初始化为0也可以初始化为-1,这里以初始化为0为例

  • 初始化为0: top的值可以表示栈元素的个数
  • top初始化位-1: top指向栈顶元素
void STInit(ST* ps)
{
	assert(ps);

	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

2.压栈

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	//扩容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* ret = (STDataType*)realloc(ps->a,sizeof(STDataType)*newcapacity);
		if (ret == NULL)
		{
			perror("realloc");
			return;
		}
		ps->a = ret;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

3.出栈

void STPop(ST* ps)
{
	assert(ps); 
	assert(ps->top); //确保栈中还有元素

	ps->top--;
}

4.取栈顶元素

STDataType STTop(ST* ps)
{
	assert(ps);
	assert(ps->top);

	return ps->a[ps->top - 1];
}

5.判断栈是否为空

bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

6.求栈的大小

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

7.摧毁栈

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a == NULL;
	ps->top = ps->capacity = 0;
}

二. 队列

2.1 队列的概念及结构

        队列只允许一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特点,进行插入操作的一端称为队尾,进行删除操作的一端称为队头.

2.2 队列的实现

        队列也可以用数组和链表的结构实现,使用链表的结构实现会更优一点,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低.

队列的结构:

typedef int QDataType;

//链式结构:表示队列
typedef struct QueueNode {
	QDataType x;
	struct QueueNode* next;
}Node;

//队列的结构:队头和队尾分别用head和tail指针维护
typedef struct Queue
{
	Node* head;
	Node* tail;
	int size;
}Queue;

接口:

//队列的初始化
void QueueInit(Queue* ps);
//入队列
void QueuePush(Queue* ps,QDataType x);
//出队列
void QueuePop(Queue* ps);
//判断队列是否为空
bool QueueEmpty(Queue* ps);
//取队头元素
QDataType QueueFront(Queue* ps);
//取队尾元素
QDataType QueueTail(Queue* ps);
//求队列大小
int QueueSize(Queue* ps);
//摧毁队列
void QueueDestory(Queue* ps);

1.队列的初始化


void QueueInit(Queue* ps)
{
	assert(ps);

	ps->head = ps->tail = NULL;
	ps->size = 0;
}

2.入队列

void QueuePush(Queue* ps, QDataType x)
{
	assert(ps);
	//创建新节点
	Node* newnode = (Node*)malloc(sizeof(Node));
	if (newnode == NULL)
	{
		perror("malloc");
		return;
	}
	newnode->next = NULL;
	newnode->x = x;

	//尾插
	if (ps->tail == NULL)
	{
		ps->head = ps->tail = newnode;
	}
	else
	{
		ps->tail->next = newnode;
		ps->tail = ps->tail->next;
	}
	ps->size++;
}

3.出队列

void QueuePop(Queue* ps)
{
	assert(ps);
	assert(ps->head);

	if (ps->head->next == NULL)
	{
		ps->head = ps->tail = NULL;
	}
	else
	{
		Node* next = ps->head->next;
		free(ps->head);
		ps->head = next;
	}
	
	ps->size--;
}

4.判断队列是否为空

bool QueueEmpty(Queue* ps)
{
	assert(ps);

	return ps->tail == NULL;
}

5.取队头元素

QDataType QueueFront(Queue* ps)
{
	assert(ps);
	assert(ps->head);

	return ps->head->x;
}

6.取队尾元素

QDataType QueueTail(Queue* ps)
{
	assert(ps);
	assert(ps->tail);
	return ps->tail->x;
}

7.求队列大小

int QueueSize(Queue* ps)
{
	assert(ps);
	return ps->size;
}

8.摧毁队列

void QueueDestory(Queue* ps)
{
	assert(ps);

	Node* cur = ps->head;
	while (cur)
	{
		Node* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head=ps->tail = NULL;
}


 

猜你喜欢

转载自blog.csdn.net/m0_74910646/article/details/134477080