栈与队列

栈和队列
1.栈
栈是限定只能在表的一端进行插入和删除操作的线性表,只能在top插入或删除。
top
 
 
bottom
顺序栈
定义:
typedef struct
{
ElemType stack [ MaxStackSize ]; /* MaxStackSize为可以存储的最大单元个数 */
int top; /* top为当前栈中数据元素的个数 */
}SequenceStack;
操作:
①初始化
void StackInitiate ( SequenceStack * S)
{
S -> top = 0;
}
②判断是否为空
int StackNotEmpty ( SequenceStack S)
{
if ( S.top <= 0)
retun 0;
else
return 1;
}
③进栈
int StackPush ( SequenceStack * S, ElemType x)
{
if ( S -> top >= MaxStackSize)
{
printf ("堆栈已满无法插入!\n");
return 0;
}
else
{
S -> stack [S -> top] = x;
S -> top ++;
return 1;
}
}
④出栈
int StackPop ( SequenceStack * S, ElemType * d)
{
if ( S -> top <= 0)
{
printf ("堆栈已空无数据元素出栈!\n");
return 0;
}
else
{
S -> top --;
*d = S -> stack [S -> top];
return 1;
}
}
⑤取栈顶元素
int StackTop ( SequenceStack S, ElemType * d)
{
if ( S.top <= 0)
{
printf ("堆栈已空!\n");
return 0;
}
else
{
*d = S.stack [S.top -1];
return 1;
}
}
链式栈
定义:
typedef struct snode
{
Elemtype data;
struct snode * next;
}SingleLinkedNode;
操作:
①初始化
void StackInitiate ( StackLinkedNode * * head)
{
if ( (* head = (SingleLinkedList *) malloc (sizeof (SingleLinkedList) ) ) ==NULL) exit (1);
/* 如果有内存空间,申请头结点空间并使头指针head指向头结点 */
(*head) -> next = NULL;
}
②判断是否为空
int StackNotEmpty ( SingleLinkedNode * head)
{
if ( head -> next =NULL)
return 0;
else
return 1;
}
③入栈
int StackPush ( SingleLinkedNode * head, ElemType x)
{
SingleLinkedNode * p;
if ( ( p = ( SingleLinkedNode *) malloc ( sizeof ( SingleLinkedNode) ) ) == NULL)
{
printf ( "内存空间不足无法插入!\n");
retun 0;
}
p -> data = x;
p -> next = head -> next;
head -> next = p;
return 1;
}
④出栈
int StackPop ( SingleLinkedNode * head, ElemType *d)
{
SingleLinkedNode *p = head -> next;
if ( p = NULL)
{
printf ("堆栈已空出错!");
return 0;
}
head -> next = p -> next;
* d = p -> data;
free ( p);
return 1;
]
⑤取栈顶元素
int StackTop ( SingleLinkedNode * head, ElemType * d)
{
SingleLinkedNode * p = head -> next ;
if ( p = NULL)
{
printf ("堆栈已空出错!");
return 0;
}
* d = p -> data;
return 1;
}
⑥撤销动态申请空间
void Destroy ( SingleLinkedNode * * head)
{
SingleLinkedNode *p, *p1;
p = * head;
while ( p != NULL)
{
p1 = p;
p = p -> next;
free ( p1);
}
}
2.队列
只能在某一端进入,在另一端输出。像排队一样先进先出
顺序循环队列
定义:
typedef struct
{
ElemType queue [ MaxQueueSize ];
int rear; /* rear为表尾指针 */
int front; /* front为表头指针 */
int count; /* count为计数器 */
}SequenceQueue;
操作:
①初始化
void QueueInitiate ( SequenceQueue * Q)
{
Q -> rear = 0;
Q -> front = 0;
Q -> count = 0;
}
②判断是否为空
int QueueNotEmpty ( SequenceQueue Q)
{
if ( Q.count == 0)
return 0;
else
return 1;
}
③入队列
int QueueAppend ( SequenceQueue * Q, ElemType x)
{
if ( Q -> count > 0 && Q -> rear == Q -> front)
{
printf ( "队列已满无法插入!\n");
return 0;
}
else
{
Q -> queue [ Q -> rear ] = x;
Q -> rear = ( Q -> rear + 1)%MaxQueueSize; /* 构建循环的key */
Q -> count ++;
return 1;
}
}
④出队列
int QueueDelete ( SequenceQueue * Q, ElemType * d)
{
if ( Q -> count == 0)
{
printf ("循环队列已空无数据元素出队列!\n");
return 0;
}
else
{
* d = Q -> queue [ Q -> front ];
Q -> front = ( Q -> front + 1) % MaxQueueSize;
Q -> count --;
return 1;
}
}
⑤获取队头元素
int QueueGet ( SequenceQueue * Q, ElemType * d)
{
if ( Q -> count == 0)
{
printf ("循环队列已空无数据元素可取!\n");
return 0;
}
else
{
* d = Q -> queue [ Q -> front ];
return 1;
}
}
链式队列
定义结点
typedef struct qnode
{
ElemType data;
struct qnode * next;
}LinkedQueueNode;
定义头尾指针
typedef struct
{
LinkedQueueNode * front;
LinkedQueueNode * rear;
}LQueue;
操作:
①初始化
void QueueInitiate ( LQueue * Q)
{
Q -> rear = NULL;
Q -> front = NULL;
}
②判断是否为空
int QueueNotEmpty ( LQueue Q)
{
if ( Q.front == NULL)
return 0;
else
return 1;
}
③入队列
int QueueAppend ( LQueue * Q, ElemType x)
{
LinkedQueueNode * p;
if ( ( p = ( LinkedQueueNode * ) malloc ( sizeof ( LinkedQueueNode) ) ) == NULL )
{
printf ("内存空间不足!");
return 0;
}
p -> data = x;
p -> next = NULL;
if ( Q -> rear != NULL)
Q -> rear -> next = p;
Q -> rear =p;
if ( Q-> front == NULL)
Q -> front = p;
return 1;
}
④出队列
int QueueDelete ( LQueue * Q, ElemType *d)
{
LinkedQueueNode *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 QueueGet (LQueue Q, ElemType *d)
{
if ( Q.front == NULL)
{
printf ("队列已空无数据元素出队列!\n");
return 0;
}
else
{
* d = Q.front -> data;
return 1;
}
}
⑥撤销动态申请空间
void Destroy ( LQueue Q)
{
LinkedQueueNode *p, *p1;
p = Q.front;
while ( p != NULL)
{
p1 = p;
p = p -> next;
free ( p1 );
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值