資料結構學習之堆疊(順序儲存)
阿新 • • 發佈:2019-01-31
【摘要】在計算機領域,堆疊是一個不容忽視的概念,堆疊是兩種資料結構。堆疊都是一種資料項按序排列的資料結構,只能在一端(稱為棧頂(top))對資料項進行插入和刪除。在微控制器應用中,堆疊是個特殊的儲存區,主要功能是暫時存放資料和地址,通常用來保護斷點和現場。要點:堆,佇列優先,先進先出[1] 。棧,先進後出(First-In/Last-Out)。
其實,堆疊的屬性主要表現在下面兩個方面:
(1)堆疊的資料是先入後出
(2)堆疊的長度取決於棧頂的高度
1、順序儲存(連續記憶體)
(1) 設計堆疊節點
typedef struct _STACK_NODE
{
int * pData;
int length;//棧的長度
int top;//棧頂指標的位置
}STACK_NODE;
(2)建立堆疊
STACK_NODE* alloca_stack(int number)
{
STACK_NODE* pStackNode = NULL;
if(0 == number)
return NULL;
pStackNode = (STACK_NODE*)malloc(sizeof(STACK_NODE));
assert(NULL != pStackNode);
memset(pStackNode, 0 , sizeof(STACK_NODE));
pStackNode->pData = (int*)malloc(sizeof(int) * number);
if(NULL == pStackNode->pData){
free(pStackNode);
pStackNode = NULL;
return NULL;
}
memset(pStackNode->pData, 0, sizeof(int) * number);
pStackNode-> length = number;
pStackNode-> top= 0 ;
return pStackNode;
}
(3)釋放堆疊
STATUS free_stack(const STACK_NODE* pStackNode)
{
if(NULL == pStackNode)
return FALSE;
assert(NULL != pStackNode->pData);
free(pStackNode->pData);
free((void*)pStackNode);
return TRUE;
}
(4)堆疊壓入資料
STATUS stack_push(STACK_NODE* pStackNode, int value)
{
if(NULL == pStackNode)
return FALSE;
if(pStackNode->length == pStackNode->top)
return FALSE;
pStackNode->pData[pStackNode->top ++] = value;
return TRUE;
}
(5)堆疊彈出資料
STATUS stack_pop(STACK_NODE* pStackNode, int* value)
{
if(NULL == pStackNode || NULL == value)
return FALSE;
if(0 == pStackNode->top)
return FALSE;
*value = pStackNode->pData[-- pStackNode->top];
return TRUE;
}
(6)統計當前堆疊中包含多少資料
int count_stack_number(const STACK_NODE* pStackNode)
{
return pStackNode->top;
}
全部程式碼如下:
//順序儲存
//Written by ZP1015
//2015.10.20
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct STACK_NODE
{
int* pData;/*陣列,長度為StackLenMax*/
int top;//棧頂指標的位置
int StackLenMax;
};
struct STACK_NODE* alloc_stack(int StackSize)
{
if(StackSize <= 0)
return NULL;
struct STACK_NODE* pStackNode = NULL;
pStackNode = (struct STACK_NODE*)malloc(sizeof(struct STACK_NODE));
if(NULL == pStackNode) {
return NULL;
}
memset(pStackNode, 0, sizeof(struct STACK_NODE));
pStackNode->pData = (int *)malloc(sizeof(int)*StackSize);
if(NULL == pStackNode->pData){
goto malloc_failed;
}
printf("%d\n",pStackNode->pData);
memset(pStackNode->pData, 0, sizeof(int) * StackSize);
pStackNode->top= -1; /*初始化從0開始*/
pStackNode->StackLenMax = StackSize;
return pStackNode;
malloc_failed:
free(pStackNode);
return NULL;
}
int free_stack(struct STACK_NODE* pStackNode)
{
if(NULL == pStackNode)
return -1;
if(NULL == pStackNode->pData) {
free(pStackNode);
return -1;
}
printf("%d\n",pStackNode->pData);
printf("[%d] %d\n",__LINE__,pStackNode->pData[2]);
free(pStackNode->pData);
free(pStackNode);
return 0;
}
int stack_push(struct STACK_NODE* pStackNode, int value)
{
/*1.異常處理*/
if(NULL == pStackNode)
return -1;
if(NULL == pStackNode->pData) {
return -1;
}
/*2.棧滿,不能壓入元素*/
if(pStackNode->top == pStackNode->StackLenMax-1)
return -1;
printf("%d\n",pStackNode->top);
pStackNode->pData[++pStackNode->top] = value;
return 0;
}
int stack_pop(struct STACK_NODE* pStackNode, int* value)
{
if(NULL == pStackNode || NULL == value)
return -1;
if(-1 == pStackNode->top)
return -1;
*value = pStackNode->pData[pStackNode->top--];
return 0;
}
int count_stack_number(struct STACK_NODE* pStackNode)
{
return (pStackNode->top+1);
}
void print_stack_node(struct STACK_NODE *pStackNode)
{
/*1.輸入的引數有誤*/
if(NULL == pStackNode) {
printf("[%d] pStackNode is illegal! \n",__LINE__);
return;
}
/*2.輸入的鏈式堆疊為空*/
if(-1 == pStackNode->top) {
printf("[%d] pStackNode is empty!\n",__LINE__);
return ;
}
struct STACK_NODE *pStackNodeTemp = pStackNode;
int count = 0;
while(count <= pStackNode->top) {
printf("%d ",pStackNodeTemp->pData[count]);
count++;;
}
printf("\n");
}
int main()
{
struct STACK_NODE *pStackNode;
pStackNode = alloc_stack(20);
int i = 0;
for (i = 0;i<10;i++) {
stack_push(pStackNode,i);
}
print_stack_node(pStackNode);
free_stack(pStackNode);
getchar();
getchar();
return 0;
}