棧與隊列

棧和隊列
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 );
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章