1. 程式人生 > >劍指offer--2

劍指offer--2

info 實現 image UC pop test tdi void TE

前言:繼續筆記分享!

面試題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,一個棧彈出,一個壓入,就可以實現隊列!!!

劍指offer--2