棧
順序棧
#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 ( ) {
SeqStack S;
S.init( );
S.push( 1 );
S.push( 2 );
cout << S.size() << endl;
return 0 ;
}
鏈棧
#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 ( ) {
return 0 ;
}
共享棧
順序實現
#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 ( ) {
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 ;
}
鏈式實現
應用
括號匹配問題
#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
State bracketMatch ( string str ) {
stack<char> S;
for ( auto e : str ) {
switch ( e ) {
case '{':
case '[':
case '(':
S.push(e);
break;
case '}':
case ']':
case ')': {
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 ( ) {
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
算術表達式求值
隊列
循環隊列
#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 ( ) {
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 ;
}
鏈隊列
#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 ( ) {
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 ;
}