堆栈
顺序堆栈的操作实现(初始化,非空否,入栈,出栈,取出栈顶元素)
# include <stdio.h>
# include <stdlib.h>
# define MAXSIZE 100
# define Datatype int
typedef struct {
Datatype stack[ MAXSIZE] ;
int top;
} Sqstack;
void Initiate ( Sqstack * s)
{
s-> top = 0 ;
}
int StackEmpty ( Sqstack s)
{
if ( s. top <= 0 ) return 0 ;
else return 1 ;
}
int StackPush ( Sqstack* s, Datatype x)
{
if ( s-> top >= MAXSIZE)
{
printf ( "堆栈已满,无法插入\n" ) ;
return 0 ;
}
else
{
s-> stack[ s-> top] = x;
s-> top++ ;
return 1 ;
}
}
int StackPop ( Sqstack* s, Datatype * d)
{
if ( s-> top <= 0 )
{
printf ( "堆栈已空\n" ) ;
return 0 ;
}
else
{
s-> top-- ;
* d = s-> stack[ s-> top] ;
return 1 ;
}
}
int StackTop ( Sqstack s, Datatype* d)
{
if ( s. top <= 0 )
{
printf ( "堆栈已空\n" ) ;
return 0 ;
}
else
{
* d = s. stack[ s. top - 1 ] ;
return 1 ;
}
}
int main ( )
{
Sqstack s ;
Datatype d= 0 ;
Initiate ( & s) ;
for ( int i = 0 ; i< 5 ; i++ )
StackPush ( & s, i+ 1 ) ;
StackTop ( s, & d) ;
printf ( "当前栈顶元素为:%d\n" , d) ;
printf ( "依次按顺序出栈:\n" ) ;
while ( StackEmpty ( s) )
{
StackPop ( & s, & d) ;
printf ( "%d\n" , d) ;
}
return 0 ;
}
链式堆栈的操作实现(初始化,非空否,入栈,出栈,取出栈顶元素)
# include <stdio.h>
# include <stdlib.h>
# define Datatype int
typedef struct snode {
Datatype data;
struct snode * next;
} Snode;
void Initiate ( Snode * * head)
{
* head = ( Snode* ) malloc ( sizeof ( Snode) ) ;
if ( * head == NULL )
{
printf ( "error" ) ;
return ;
}
( * head) -> next = NULL ;
}
int StackEmpty ( Snode * head)
{
if ( head-> next== NULL ) return 0 ;
else return 1 ;
}
void StackPush ( Snode* head, Datatype x)
{
Snode* p = ( Snode* ) malloc ( sizeof ( Snode) ) ;
if ( p == NULL )
{
printf ( "error!" ) ;
return ;
}
p-> data= x;
p-> next = head-> next;
head-> next = p;
}
int StackPop ( Snode* head, Datatype * d)
{
Snode* p = head-> next;
if ( p == NULL )
{
printf ( "堆栈已空,出栈错误!" ) ;
return 0 ;
}
else
{
head-> next = p-> next;
* d = p-> data;
free ( p) ;
return 1 ;
}
}
int StackTop ( Snode* head, Datatype* d)
{
Snode* p = head-> next;
if ( p== NULL )
{
printf ( "堆栈已空\n" ) ;
return 0 ;
}
else
{
* d = p-> data;
return 1 ;
}
}
void Destory ( Snode* head)
{
Snode* p, * q;
p = head;
while ( p != NULL )
{
q = p;
p = p-> next;
free ( q) ;
}
}
int main ( )
{
Snode* head ;
Datatype d = 0 ;
Initiate ( & head) ;
for ( int i = 0 ; i< 5 ; i++ )
StackPush ( head, i+ 1 ) ;
StackTop ( head, & d) ;
printf ( "当前栈顶元素为:%d\n" , d) ;
printf ( "依次按顺序出栈:\n" ) ;
while ( StackEmpty ( head) )
{
StackPop ( head, & d) ;
printf ( "%d\n" , d) ;
}
Destory ( head) ;
return 0 ;
}
c++ stack
# include <stack>
stack< Datatype> s
s. push ( a) 元素a入栈,增加元素
s. pop ( ) 移除栈顶元素
s. top ( ) 取得栈顶元素
s. empty ( ) 检测是否为空,空为真
s. size ( ) 返回栈内元素的个数
队列
顺序队列
循环顺序队列的操作实现(初始化,非空否,入队,出栈,取队头元素)
# include <stdio.h>
# define MAXSIZE 100
# define DataType int
typedef struct {
DataType queue[ MAXSIZE] ;
int rear;
int front;
int count;
} SCQueue;
void InitiateQ ( SCQueue* s)
{
s-> rear = 0 ;
s-> front = 0 ;
s-> count = 0 ;
}
int Empty ( SCQueue Q)
{
if ( Q. count == 0 ) return 0 ;
else return 1 ;
}
int Append ( SCQueue* Q, DataType x)
{
if ( Q-> count > 0 && Q-> rear == Q-> front)
{
printf ( "队列已满\n" ) ;
}
else
{
Q-> queue[ Q-> rear] = x;
Q-> rear = ( Q-> rear + 1 ) % MAXSIZE;
Q-> count++ ;
return 1 ;
}
}
int Delete ( SCQueue* Q, DataType* d)
{
if ( Q-> count == 0 )
{
printf ( "队列已空\n" ) ;
return 0 ;
}
else
{
* d = Q-> queue[ Q-> front] ;
Q-> front = ( Q-> front + 1 ) % MAXSIZE;
Q-> count-- ;
return 1 ;
}
}
int Get ( SCQueue Q, DataType* d)
{
if ( Q. count == 0 )
{
printf ( "队列已空\n" ) ;
return 0 ;
}
else
{
* d = Q. queue[ Q. front] ;
return 1 ;
}
}
链式队列的操作实现(初始化,非空否,入队,出队,取队头元素)
# include <stdio.h>
# include <stdlib.h>
# define MAXSIZE 100
# define DataType int
typedef struct qnode
{
DataType data;
struct qnode * next;
} LQNode;
typedef struct {
LQNode* rear;
LQNode* front;
} LQueue;
void InitiateQ ( LQueue* Q)
{
Q-> front= NULL ;
Q-> rear = NULL ;
}
int Empty ( LQueue Q)
{
if ( Q. front == NULL ) return 0 ;
else return 1 ;
}
void Append ( LQueue* Q, DataType x)
{
LQNode* p;
p = ( LQNode* ) malloc ( sizeof ( LQNode) ) ;
p-> data = x;
p-> next = NULL ;
if ( Q-> rear != NULL )
{
Q-> rear-> next = p;
}
Q-> rear = p;
if ( Q-> front == NULL )
{
Q-> front = p;
}
}
int Delete ( LQueue* Q, DataType* d)
{
LQNode* p;
if ( Q-> front == NULL )
{
printf ( "队列已空\n" ) ;
return 0 ;
}
else
{
* d = Q-> front-> data;
p = Q-> front;
Q-> front = Q-> front-> next;
if ( Q-> front == NULL )
{
Q-> rear = NULL ;
}
free ( p) ;
return 1 ;
}
}
int Get ( LQueue Q, DataType* d)
{
if ( Q. front == NULL )
{
printf ( "队列已空\n" ) ;
return 0 ;
}
else
{
* d = Q. front-> data;
return 1 ;
}
}
void Destroy ( LQueue Q)
{
LQNode* p, * q;
p = Q. front;
while ( p != NULL )
{
q = p;
p = p-> next;
free ( q) ;
}
}