DS_2019_5_12(栈和队列面试题)

我不知道怎样的结局,才能配得上这一路的颠沛流离.
趁着你的能力还能撑得起你的野心,那就只顾着风雨兼程!

#include <stdio.h>
#include <stdlib.h>
#pragma once
1.括号匹配问题

解题思路

  • 遍历字符串, 遇到一个字符:
  • a.如果该括号是左括号("([{"), 入栈;
  • b.如果该括号不是左括号, 检查该括号是否匹配与栈顶括号;
  • 确认栈中是否有元素,如果有,用当前括号与栈顶的括号进行比较, //匹配则继续,否则返回flase.
bool isValid(char* s){
	if (NULL == s){
		return true;
	}
	Stack st;
	StackInit(&s);
	int len = strlen(s);
	for (int i = 0; i < len; ++i){
		//左括号入栈
		if ('(' == s[i] || '[' == s[i] || '{' == s[i]){
			StackPush(&st, s[i]);
		}
		else{
			//s[i]为右括号
			if (StackEmpty(&st)){
				return false;
			}
			char ch = StackTop(&st);
			//检测左右括号是否匹配
			if ('(' == ch && ')' == s[i] || '[' == ch && ']' == s[i]
				|| '{' == ch && '}' == s[i]){
				StackPop(&st);
			}
			else{
				return false;

			}
		}
	}
	if (!StackEmpty(&st)){
		return false;
	}
	return true;
}
2.用队列实现栈
typedef struct {
	Queue _q1;
	Queue _q2;
} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
	MyStack* pms = (MyStack*)malloc(sizeof(MyStack));
	if (NULL==pms){
		assert(0);
		return NULL;
	}
	QueueInit(&pms->_q1);
	QueueInit(&pms->_q2);
	return pms;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
	assert(obj);
	if (QueueEmpty(&obj->_q1)){
		QueuePush(&obj->_q2,x);
	}
	else{
		QueuePush(&obj->_q1,x);
	}
}

/** Removes the element on top of the stack and returns that element.*/
int myStackPop(MyStack* obj) {
	assert(obj);
	if (!QueueEmpty(&obj->_q1)){
	//将q1中的前n-1个元素移动到q2中
		int size = QueueSize(&obj->_q1);
		while (size > 1){
			QueuePush(&obj->_q2,QueueFront(&obj->_q1));
			QueuePop(&obj->_q1);
			size--;
		}
		int ret = QueueBack(&obj->_q1);
		QueuePop(&obj->_q1);
		return ret;
	}
	else{
		//将q2中的前n-1个元素移动到q1中
		int size = QueueSize(&obj->_q2);
		while (size > 1){
			QueuePush(&obj->_q1, QueueFront(&obj->_q2));
			QueuePop(&obj->_q2);
			size--;
		}
		int ret = QueueBack(&obj->_q2);
		QueuePop(&obj->_q2);
		return ret;
	}
}


/** Get the top element. */
int myStackTop(MyStack* obj) {
	if (QueueEmpty(&obj->_q1)){
		return QueueBack(&obj->_q2);
	}
	else{
		return QueueBack(&obj->_q1);
	}
}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
	return QueueEmpty(&obj->_q1) && QueueEmpty(&obj->_q2);
}

void myStackFree(MyStack* obj) {
	QueueDestroy(&obj->_q1);
	QueueDestroy(&obj->_q2);
	free(obj);
}

3.用栈实现队列

解题思路

  • (1)入队列:1, 2, 3, 4(直接将数据放到s1中)
  • (2)出队列(检测s2中是否有数据) 如果有,则直接出 如果为空,则将s1中的数据搬移到s2中
  • (3)获取队头元素 (检测s2中是否有数据) 如果有:则直接返回如果没有:将s1中的数据搬移到s2中
  • (4)检测队列是否为空:s1&&s2
typedef struct {
	Stack _s1;              //模拟入队列操作
	Stack _s2;              //模拟出队列操作
} MyQueue;
/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
	MyQueue* pmq = (MyQueue*)malloc(sizeof(MyQueue));
	if (NULL == pmq){
		assert(0);
		return NULL;
	}
StackInit(&pmq->_s1);
StackInit(&pmq->_s2);
return pmq;
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
	assert(obj);
	StackPush(&obj->_s1, x);
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
	assert(obj);
	if (StackEmpty(&obj->_s2)){
		//将s1中的元素搬移到s2中
		while (!StackEmpty(&obj->_s1)){
			StackPush(&obj->_s2, StackTop(&obj->_s2));
			StackPop(&obj->_s1);
		}
	}
	int ret = StackTop(&obj->_s2);
	StackPop(&obj->_s2);
	return ret;
}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
	assert(obj);
	if (StackEmpty(&obj->_s2)){
		//将s1中的元素搬移到s2中
		while (!StackEmpty(&obj->_s1)){
			StackPush(&obj->_s2, StackTop(&obj->_s2));
			StackPop(&obj->_s1);
		}
	}
	return StackPop(&obj->_s2);
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
	return StackEmpty(&obj->_s1) && StackEmpty(&obj->_s2);
}

void myQueueFree(MyQueue* obj) {
	StackDestroy(&obj->_s1);
	StackDestroy(&obj->_s2);
	free(obj);
}

4.实现一个最小栈.

//解题思路:
(1)给入两个栈模拟实现, s1(保存数据), s2(保存最小值);
(2)入栈:
s1 : 每次都需要一个元素
s2 : data <= 栈中的最小值, s2入元素;
(3)出栈 :
s1与s2栈顶元素相等的情况下 :
s1与s2同时出栈; 否则是s1出栈;

typedef struct{
	Stack ds;           //存储数据
	Stack ms;           //存储最小值
}MinStack;

MinStack* minStackCreate(){
	//MinStack ms;  ms是函数中的一个临时变量,一旦函数运行结束,该变量就被销毁
	MinStack* pms = (MinStack*)malloc(sizeof(MinStack));
	if (NULL == pms){
		assert(pms);
		return NULL;
	}
	StackInit(&pms->ds);
	StackInit(&pms->ms);
	return pms;
}

void minStackPush(MinStack* obj, int x){
	StackPush(&obj->ds, x);
	if (StackEmpty(&obj->ms) || StackPop(&obj->ms)){
		StackPush(&obj->ms, x);
	}
}

void minStackPop(MinStack* obj){
	if (StackTop(&obj->ms) == StackTop(&obj->ds)){
		StackPop(&obj->ms);
	}
	StackPop(&obj->ds);
}

int minStackTop(MinStack* obj){
	return StackTop(&obj->ds);
}

int minStackGetMin(MinStack* obj){
	return StackTop(&obj->ms);
}

void minStackFree(MinStack* obj){
	StackDestroy(&obj->ds);
	StackDestroy(&obj->ms);
	free(obj);
}
5.设计循环队列
typedef struct {
	int* _array;
	int _capacity;
	int _size;       //记录有效元素的个数
	int _front;
	int _back;

} MyCircularQueue;

/** Initialize your data structure here. Set the size of the queue to be k. */

MyCircularQueue* myCircularQueueCreate(int k) {
	MyCircularQueue* q = (MyCircularQueue*)
		malloc(sizeof(MyCircularQueue));
	if (NULL == q){
		assert(0);
		retunr NULL;
	}
	q->_array = (int*)malloc(sizeof(int)*k);
	if (NULL == q->_array){
		assert(0);
		return NULL;
	}
	q->_capacity = k;
	q->_size = 0;
	q->_front = 0;
	q->_back = 0;
	return q;
}

/** Insert an element into the circular queue. Return true if the operation is successful. */
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
	assert(obj);
	//检测队列是否满
	if (obj->_size == obj->_capacity){
		return false;
	}
	obj->_capacity[obj->_back++] = value;
	obj->_size++;
	if (obj->_back == obj->_capacity){
		obj->_back = 0;
	}
	return true;
}

/** Delete an element from the circular queue. Return true if the operation is successful. */
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
	assert(obj);
	//检测队列是或否为空
	if (0 == obj->_size){
		return false;
	}
	++obj->front;
	if (obj->_front == obj->_capacity){
		obj->_front = 0;
	}
	obj->_size--;
	return true;
}

/** Get the front item from the queue. */
int myCircularQueueFront(MyCircularQueue* obj) {
	if (0 == obj->_size){
		return -1;
	}
	return obj->_array[obj->_front];
}

/** Get the last item from the queue. */
int myCircularQueueRear(MyCircularQueue* obj) {
	if (o == obj->_size){
		return -1;
	}
	if (0 == obj->_back){
		return obj->_array[obj->_capacity - 1];
	}
	else{
		return obj->_array[obj->_back - 1];
	}
}

/** Checks whether the circular queue is empty or not. */
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
	return 0 == obj->_size;
}

/** Checks whether the circular queue is full or not. */
bool myCircularQueueIsFull(MyCircularQueue* obj) {
	return obj->_size == obj->_capacity;
}

void myCircularQueueFree(MyCircularQueue* obj) {
	free(obj->_array);
	free(obj);
}
6.使用栈将递归转化为循环
void ReversePrint(Node* pHead){
	if (NULL == pHead){
		return;
	}
	Stack s;
	StackInit(&s);
	//遍历链表,将链表中的元素放到stack中
	Node* pCur = pHead;
	while (pCur){
		StackPush(&s, pCur->data);
		pCur->next;
	}

	//将栈中所有的元素打印
	while (!StackEmpty(&s)){
		printf("%d\n", StackTop(&s));
	}
	StackPop(&s);
	StackDestroy(&s);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ds_store_exp-master是一种用于分析和利用.DS_Store文件的工具包。.DS_Store是Mac OS系统在文件夹中自动生成的隐藏文件,用于存储特定文件夹的自定义视图选项和元数据信息。ds_store_exp-master提供了一些功能,使用户能够查看和分析.DS_Store文件。 使用ds_store_exp-master的第一步是安装Python。在确保已经安装了python之后,可以从GitHub上下载ds_store_exp-master的源代码。然后,可以在命令行中导航到下载的文件所在的目录,并使用命令"python ds_store_exp.py"运行。 当程序运行后,用户会看到一个简单的命令行菜单,其中包含一些选项供用户选择。其中一个选项是"Extraction",允许用户从.DS_Store文件中提取有关文件夹和文件的信息。另一个选项是"Analysis",允许用户分析.DS_Store文件中的各种字段和标志位。用户还可以选择在指定的文件夹中创建自己的.DS_Store文件。 在提取信息或分析.DS_Store文件时,用户需要提供.DS_Store文件的路径。可以通过将.DS_Store文件拖放到命令行终端中,或手动输入.DS_Store文件的路径来完成。 使用ds_store_exp-master,您可以深入了解.DS_Store文件的内容,包括文件和文件夹的自定义视图选项、标签、图标位置等。这对于研究和理解.DS_Store文件的作用和功能非常有用。 总而言之,ds_store_exp-master是一个功能强大的工具包,可用于分析和利用.DS_Store文件。通过提供丰富的选项和功能,使用户能够深入了解.DS_Store文件的内容和功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值