DataStructure_第三章 限定性線性表-棧與隊列 ( 順序棧 / 鏈棧 / 共享棧 / 循環隊列 / 鏈隊列 的分析及其實現 )







順序棧

/*** 
 * @Author      : acmaker
 * @Date        : 2020-03-22 10:31:14
 * @LastEditTime: 2020-03-22 13:38:59
 * @FilePath    : \myCPlusPlusCode\DataStructure\Stack\SeqStack.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 
typedef long long ll; 

#define State int
#define TRUE 1
#define FALSE 0

const int MAX_SIZE = 1e3+100;
typedef int ElemType;
typedef struct {
    ElemType data[MAX_SIZE];
    int _top;
    State init ( );
    ElemType top ( );
    State pop ( );
    State push ( int e );
    int size ( );
}SeqStack;

State SeqStack::init ( ) {
    _top = -1;
}
ElemType SeqStack::top ( ) {
    return data[_top];
}
State SeqStack::pop ( ) {
    if ( _top==-1 ) return FALSE;
    --_top;
    return TRUE;
}
State SeqStack::push ( int e ) {
    if ( _top==MAX_SIZE-1 ) return FALSE;
    data[++_top] = e;
    return TRUE;
}
int SeqStack::size( ) { 
    return _top+1;
}


int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    SeqStack S;
    
    S.init( );
    
    S.push( 1 );

    S.push( 2 );

    cout << S.size() << endl;



    return 0 ; 
} 



鏈棧


/*** 
 * @Author      : acmaker
 * @Date        : 2020-03-23 15:30:12
 * @LastEditTime: 2020-03-24 09:36:13
 * @FilePath    : \myCPlusPlusCode\DataStructure\Stack\LinkedStack.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 
typedef long long ll; 

#define TRUE 1
#define FALSE 0
typedef int State;

typedef int ElemType;
typedef struct {
    ElemType data;
    LinkedStackNode* next;
}LinkedStackNode, *LinkedStack;

ElemType top ( LinkedStack S ) {
    return S->next->data;
}

State pop ( LinkedStack S, ElemType *e ) {
    if ( S->next==NULL ) return FALSE;
    LinkedStackNode *t = S->next;
    *e = t->data;
    S->next = t->next;
    delete t;
    return TRUE;
}

State push ( LinkedStack S, ElemType e ) {
    LinkedStackNode *t = new LinkedStackNode;
    if ( t==NULL ) return FALSE;
    t->data = e;
    t->next = S->next;
    S->next = t;
    return TRUE;
}

int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    




    return 0 ; 
} 



共享棧


順序實現


/*** 
 * @Author      : acmaker
 * @Date        : 2020-03-22 14:11:34
 * @LastEditTime: 2020-03-22 14:45:23
 * @FilePath    : \myCPlusPlusCode\DataStructure\Stack\SharedStack_order.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 
typedef long long ll; 

#define State int
#define TRUE 1
#define FALSE 0

typedef int ElemType;
const int MAX_SIZE = 1e2+100;
typedef struct {
    ElemType data[MAX_SIZE];
    int _top[2];
    void init ( );
    ElemType top ( int i );
    State pop ( int i );
    State push ( int e, int i );
}SharedStack_order;

void SharedStack_order::init ( ) {
    _top[0] = -1;
    _top[1] = MAX_SIZE;
}
ElemType SharedStack_order::top ( int i ) {
    switch ( i ) {
        case 0: return data[_top[0]]; break;
        case 1: return data[_top[1]]; break;
        default: return -1;
    }
}
State SharedStack_order::pop ( int i ) {
    switch ( i ) {
        case 0: 
            if ( _top[0]==-1 ) return FALSE;
            --_top[0];
            return TRUE;
        case 1: 
            if ( _top[1]==MAX_SIZE ) return FALSE;
            ++_top[1];
        default: return FALSE;
    }
}
State SharedStack_order::push ( ElemType e, int i ) {
    switch ( i ) {
        case 0:
            if ( _top[0]+1==_top[1] ) return FALSE;
            data[++_top[0]] = e;
            return TRUE;
        case 1:
            if ( _top[1]-1==_top[0] ) return FALSE;
            data[--_top[1]] = e;
            return TRUE;
        default: return FALSE;
    }
}

int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    SharedStack_order S;

    S.init( );

    S.push( 111, 0 );

    S.push( 222, 0 );

    S.push( 111, 1 );

    S.push( 222, 1 );


    cout << S.top( 0 ) << endl;
    S.pop( 0 );

    cout << S.top( 1 ) << endl;
    S.pop( 1 );


    return 0 ; 
} 



鏈式實現




應用


括號匹配問題

/*** 
 * @Author      : acmaker
 * @Date        : 2020-03-24 11:40:25
 * @LastEditTime: 2020-03-24 12:26:49
 * @FilePath    : \myCPlusPlusCode\DataStructure\Stack\bracketMatch.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 

#define State int
#define LEFT_UNNECESSARY -2
#define RIGHT_UNNECESSARY -1
#define TYPE_ERROR 0
#define OK 1

/*** 
 * @description: 棧實現括號匹配問題
 * @param : 
 * @return: 右括號多餘返回-2, 做括號多餘返回-1, 不匹配返回0, 匹配返回1
 */
State bracketMatch ( string str ) {
    stack<char> S;
    for ( auto e : str ) {
        switch ( e ) {
            case '{':
            case '[':
            case '(':
                S.push(e);
                break;
            case '}':
            case ']':
            case ')': { // 加花括號是爲了限制 變量ch 的作用域
                if ( S.empty() ) return RIGHT_UNNECESSARY;
                char ch = S.top();
                if ( ch=='{'&&e=='}' || ch=='['&&e==']' || ch=='('&&e==')' ) {
                    S.pop();
                } else {
                    return TYPE_ERROR;
                }
            }
            default: break;
        }
    }
    return ( S.empty() ? OK : LEFT_UNNECESSARY );
}

int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    cout << bracketMatch( "{12[3(4)3]21}") << endl;

    cout << bracketMatch( "{12[3(43]21}") << endl;

    cout << bracketMatch( "{12[34)3]21}") << endl;

    cout << bracketMatch( "{12[34") << endl;

    cout << bracketMatch( "4)3]21}") << endl;



    return 0 ; 
} 

1
0
0
-2
-1



算術表達式求值

















隊列




循環隊列


/*** 
 * @Author      : acmaker
 * @Date        : 2020-03-25 21:45:27
 * @LastEditTime: 2020-03-25 22:15:44
 * @FilePath    : \myCPlusPlusCode\DataStructure\Queue\SeqQueue.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */


#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 
typedef long long ll; 

typedef int State;
#define TRUE 1
#define FALSE 0

#define MAX_SIZE (int)(1e1)
typedef int ElemType;
typedef struct SeqQueue {
    ElemType e[MAX_SIZE];
    int _front, _rear;
    SeqQueue( );
    void init( );
    ElemType front ( );
    State pop ( );
    State push ( ElemType data );
    State empty ( );
}SeqQueue;
SeqQueue::SeqQueue ( ) {
    init();
}
void SeqQueue::init ( ) {
    _front = _rear = 0;
}
ElemType SeqQueue::front ( ) {
    return e[_front];
}
State SeqQueue::pop ( ) {
    if ( (_rear+1)%MAX_SIZE==_front ) return FALSE;
    _front = (_front+1)%MAX_SIZE;
    return TRUE;
}
State SeqQueue::push ( ElemType data ) {
    if ( (_rear+1)%MAX_SIZE==_front ) return FALSE;
    e[_rear] = data;
    _rear = (_rear+1)%MAX_SIZE;
    return TRUE;
}
State SeqQueue::empty ( ) {
    return ( (_rear+1)%MAX_SIZE==_front );
}

int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    SeqQueue Q;

    for ( int i = 0; i < 10; ++i ) Q.push( i );

    for ( int i = 0; i < 10; ++i ) 
        cout << Q.front() << endl,
        Q.pop();


    return 0 ; 
} 



鏈隊列


/*** 
 * @Author      : acmaker
 * @Date        : 2020-03-25 15:43:52
 * @LastEditTime: 2020-03-25 18:05:03
 * @FilePath    : \myCPlusPlusCode\DataStructure\Queue\LinkedQueue.cpp
 * @Website     : http://csdn.acmaker.vip
 * @Description : 
 */



#include <bits/stdc++.h> 
using namespace std; 
#define rg register 
#define sc scanf 
#define pf printf 
typedef long long ll; 

typedef int State;
#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef struct LinkedQueueNode {
    ElemType data;
    LinkedQueueNode *next;
}LinkedQueueNode;
typedef struct LinkedQueue {
    LinkedQueueNode *front, *rear;
}LinkedQueue;

State initQueue( LinkedQueue *Q ) {
    Q->front = new LinkedQueueNode;
    if ( Q->front==NULL ) return FALSE;
    Q->rear = Q->front;
    Q->rear->next = NULL;
    return TRUE;
}

State push ( LinkedQueue *Q, ElemType x ) {
    LinkedQueueNode *t = new LinkedQueueNode;
    if ( t==NULL ) return FALSE;
    t->data = x;
    t->next = NULL;
    Q->rear->next = t;
    Q->rear = t;
    return TRUE;
}

State pop ( LinkedQueue *Q, ElemType *x ) {
    if ( Q->front==Q->rear ) return FALSE;
    LinkedQueueNode *t = Q->front->next;
    Q->front->next = t->next;
    *x = t->data;
    free( t );
    if ( Q->rear==t ) Q->rear = Q->front;
    return TRUE;
}

int main ( ) {  // freopen( "F:\\in\\.txt" , "r" , stdin ); 

    LinkedQueue Q;

    initQueue( &Q );

    push( &Q, 1 );
    push( &Q, 2 );
    push( &Q, 3 );
    push( &Q, 4 );

    pop( &Q, new int );
    pop( &Q, new int );
    pop( &Q, new int );
    pop( &Q, new int );




    return 0 ; 
} 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章