剑指offer--2
前言:继续笔记分享!
面试题6:暂无好的解决方法先搁浅一下
面试题7:
#include<stdio.h> #include<stdlib.h> typedef struct LIFO{ int *data; int maxlen; int top; }seqstack_t; typedef struct LIFO1{ int *data; int maxlen; int top; }seqstack_t1; //创建一个空栈 seqstack_t *CreateStack(int max) { seqstack_t *H; H = (seqstack_t *)malloc(sizeof(seqstack_t)); H->data = (int *)malloc(sizeof(int)*max); H->maxlen = max; H->top = 0; return H; } //创建一个空栈 seqstack_t1 *CreateStack1(int max) { seqstack_t1 *H; H = (seqstack_t1 *)malloc(sizeof(seqstack_t1)); H->data = (int *)malloc(sizeof(int)*max); H->maxlen = max; H->top = 0; return H; } //获取栈顶数据的值 int GetTop(seqstack_t1 *H) { if(H->top <= 0) { printf("栈空\n"); return -1; } return H->data[H->top-1]; } //栈清空 void Clear(seqstack_t *H) { H->top=0; } //向栈中插入数据 void Push(seqstack_t *H,int m) { H->data[H->top]=m; H->top++; } //向栈中插入数据 void Push1(seqstack_t1 *H,int m) { H->data[H->top]=m; H->top++; } int Pop(seqstack_t *H) { return H->data[H->top]; } int main() { seqstack_t *T=CreateStack(10); seqstack_t1 *T1=CreateStack1(10); Push(T,1); Push(T,2); Push(T,3); int i=0; --T->top; while(T->top >= 0) { Push1(T1,Pop(T)); i++; --T->top; } while(T1->top > 0) { printf("data:%-5d",GetTop(T1)); --T1->top; } printf("\n"); free(T->data); free(T); free(T1->data); free(T1); return 0; }
注:主要要懂栈后进先出LIFO,队列先进先出FIFO,一个栈弹出,一个压入,就可以实现队列!!!
作者:逆袭之路
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!