(一)栈
1. 链栈
typedef struct node
{
int data;
struct node * next;
} node;
node* Init ( node* head)
{
head = ( node* ) malloc ( sizeof ( node) ) ;
if ( ! head) exit ( 0 ) ;
head-> next = NULL ;
return head;
}
node* Push ( node* head, int data)
{
node* temp = ( node* ) malloc ( sizeof ( node) ) ;
if ( ! temp) exit ( 0 ) ;
temp-> data = data;
temp-> next = head-> next;
head-> next = temp;
return head;
}
node* Pop ( node* head)
{
if ( ! head-> next) cout << "此栈已空!" << endl;
node* p = head-> next;
head-> next = p-> next;
free ( p) ;
return head;
}
int Top ( node* head)
{
return head-> next-> data;
}
bool IsEmpty ( node* head)
{
if ( ! head-> next) return true ;
else return false ;
}
2. 顺序栈
# define MAXSIZE 100
typedef struct sqnode
{
int * element;
int length;
int size;
} sqnode;
sqnode* InitSq ( sqnode* head)
{
head-> element = ( int * ) malloc ( MAXSIZE * sizeof ( int ) ) ;
if ( ! head-> element) exit ( 0 ) ;
head-> length = 0 ;
head-> size = MAXSIZE;
return head;
}
void Push_sq ( sqnode* head, int data)
{
if ( head-> length >= head-> size)
{
int * p = ( int * ) realloc ( head-> element, ( head-> size + MAXSIZE) * sizeof ( int ) ) ;
if ( ! p) exit ( 0 ) ;
head-> element = p;
head-> size += MAXSIZE;
}
head-> element[ head-> length] = data;
head-> length ++ ;
}
void Pop_sq ( sqnode* head)
{
head-> length -- ;
}
int Top_sq ( sqnode* head)
{
return head-> element[ head-> length- 1 ] ;
}
bool Isempty_sq ( sqnode* head)
{
return head-> length ? false : true ;
}
3. 数组模拟栈
int sta1[ 100 ] ;
int top = 0 , base = 0 ;
void Push_arsta ( int i)
{
if ( top == 100 ) exit ( 0 ) ;
sta1[ top ++ ] = i;
}
int Pop_arsta ( )
{
if ( top == base) exit ( 0 ) ;
return sta1[ -- top] ;
}
int Length_arsta ( )
{
return top - base;
}
bool Isempty_arsta ( )
{
return top == base ? true : false ;
}
4. C++ 内置栈
stack< char > stk;
stk. push ( 'a' ) ;
cout << stk. top ( ) << endl;
cout << stk. size ( ) << endl;
stk. pop ( ) ;
cout << stk. empty ( ) << endl;
(二)队列
1. 链队列
typedef struct lqnode
{
int data;
struct lqnode * next;
} lqnode;
typedef struct que
{
lqnode* front;
lqnode* rear;
} que;
void Init_lq ( que* qu)
{
qu-> front = qu-> rear = ( lqnode* ) malloc ( sizeof ( lqnode) ) ;
if ( ! qu-> front) exit ( 0 ) ;
qu-> front-> next = qu-> rear-> next = NULL ;
}
void Enqueue ( que* qu, int data)
{
lqnode* p = ( lqnode* ) malloc ( sizeof ( lqnode) ) ;
if ( ! p) exit ( 0 ) ;
p-> data = data;
p-> next = NULL ;
qu-> rear-> next = p;
qu-> rear = p;
}
int Dequeue ( que* qu)
{
if ( qu-> front == qu-> rear) cout << "队列已为空!" << endl;
lqnode* p = qu-> front-> next;
int e = p-> data;
qu-> front-> next = p-> next;
if ( qu-> rear == p) qu-> rear = qu-> front;
free ( p) ;
return e;
}
bool Isempty_lq ( que* qu)
{
return qu-> front == qu-> rear ? true : false ;
}
void Destory_lq ( que* qu)
{
while ( qu-> front)
{
qu-> rear = qu-> front;
qu-> front = qu-> front-> next;
free ( qu-> rear) ;
}
}
2. 循环队列
typedef struct qunode
{
int * base;
int front, rear;
} qunode;
void Init_qunode ( qunode* qu)
{
qu-> base = ( int * ) malloc ( MAXSIZE * sizeof ( int ) ) ;
if ( ! qu-> base)
{
cout << "循环队列内存分配失败!" ;
exit ( 0 ) ;
}
qu-> front = qu-> rear = 0 ;
}
void Enqueue_qunode ( qunode* qu, int num)
{
if ( ( qu-> rear + 1 ) % MAXSIZE == qu-> front)
{
cout << "循环队列已满!" ;
exit ( 0 ) ;
}
qu-> base[ qu-> rear] = num;
qu-> rear = ( qu-> rear + 1 ) % MAXSIZE;
}
int Dequeue_qunode ( qunode* qu)
{
if ( qu-> front == qu-> rear) exit ( 0 ) ;
int e = qu-> base[ 0 ] ;
for ( int i = 1 ; i < qu-> rear; i ++ )
qu-> base[ i- 1 ] = qu-> base[ i] ;
qu-> rear = ( qu-> rear - 1 ) % MAXSIZE;
return e;
}
int Length_qunode ( qunode* qu)
{
return ( qu-> rear - qu-> front + MAXSIZE) % MAXSIZE;
}
3. 数组模拟队列
int que1[ 100 ] ;
int hea = 0 , tai = 0 ;
void Push_arque ( int num)
{
que1[ tai ++ ] = num;
}
int Pop_arque ( )
{
if ( hea == tai) cout << "数组模拟队列已为空!" ;
return que1[ -- tai] ;
}
int Length_quear ( )
{
return tai - hea;
}
bool Isempty_arque ( )
{
return hea == tai ? true : false ;
}
4. C++ 内置队列
queue< int > qu;
qu. push ( 1 ) ;
cout << "队列长队:" << qu. size ( ) << endl;
cout << "队列为空:" << qu. empty ( ) ;
cout << qu. front ( ) << " " ;
cout << qu. back ( ) << " " ;
qu. pop ( ) ;