杭州網(wǎng)站建設wguser域名歸屬查詢
一、函數(shù)實現(xiàn)
1、ClearSqStack
(1)用途
清理棧的空間。只需要棧頂指針和棧底指針相等,就說明棧已經(jīng)清空,后續(xù)新入棧的數(shù)據(jù)可以直接覆蓋,不用實際清理數(shù)據(jù),提升了清理效率。
(2)源碼
Status ClearSqStack(SqStack* S)
{JudgeAllNullPointer(S);S->TopPointer = S->BasePointer;Log("Clear SqStack : OK\n",Info);return SuccessFlag;
}
(3)參數(shù)
參數(shù)名 | 說明 |
S | 需要清理的SqStack*類型順序棧。 |
2、DestroyStack
(1)說明
銷毀棧。釋放申請的資源。
(2)源碼
Status DestroyStack(SqStack* S)
{JudgeAllNullPointer(S);free(S->BasePointer);S->TopPointer = NULL;S->BasePointer = NULL;S->SqStackMaxSize = 0;Log("Destroy SqStack : OK\n",Info);return SuccessFlag;
}
(3)參數(shù)
參數(shù)名 | 說明 |
S | 需要銷毀的SqStack*類型順序棧。 |
3、PushSqStack
(1)說明
壓棧。判斷棧是否已滿,如果已滿報錯,反之將數(shù)據(jù)壓入棧頂即可。
(2)源碼
Status PushSqStack(SqStack* S, SqElemType SE)
{JudgeAllNullPointer(S);//判斷是否棧滿if(GetSqStackLen(S) >= S->SqStackMaxSize){Log("SqStack is Full, Data cannot be pushed\n",Warning);return FailFlag;}//相同結構體之間,可以直接賦值。*(S->TopPointer) = SE;//CopySqElemType(S->TopPointer, &SE);//printf("%p, %p\n",S->TopPointer->StudentNum, (&SE)->StudentNum);S->TopPointer++;Log("Push SqStack : OK\n",Info);return SuccessFlag;
}
(3)參數(shù)
參數(shù)名 | 說明 |
S | 需要壓棧的SqStack*類型順序棧。 |
SE | 需要壓入棧的SqElemType類型數(shù)據(jù)。 |
4、PopSqStack
(1)說明
彈棧。判斷棧是否已空,如果是,就拋出錯誤。如果不是,就下移棧頂指針,將數(shù)據(jù)賦值給SE,作為傳出參數(shù)。
(2)源碼
Status PopSqStack(SqStack* S, SqElemType* SE)
{JudgeAllNullPointer(S);JudgeAllNullPointer(SE);if(JudgeSqStackIsEmpty(S) == SuccessFlag){Log("SqStack is Empty, Data cannot be poped\n",Warning);return FailFlag;}S->TopPointer--;*SE = *(S->TopPointer);//CopySqElemType(SE,S->TopPointer);//printf("%p, %p\n",S->TopPointer->StudentNum, SE->StudentNum);Log("Pop SqStack : OK\n",Info);return SuccessFlag;
}
(3)參數(shù)
參數(shù)名 | 說明 |
S | 需要初始化的SqStack*類型順序棧。 |
SE | 需要彈出棧的SqElemType*類型數(shù)據(jù)。 |
二、虛機測試
gbase@czg2 LinearTable_Stack]$ make
gcc -Wall -O3 Log.c SqStack.c main.c -o TestSqStack[gbase@czg2 LinearTable_Stack]$ ./TestSqStack
2023-2-14 9:53:20--Info--Init SqStack : OK
2023-2-14 9:53:20--Info--Push SqStack : OK
2023-2-14 9:53:20--Info--Push SqStack : OK
2023-2-14 9:53:20--Info--Push SqStack : OK
2023-2-14 9:53:20--Info--Push SqStack : OK
2023-2-14 9:53:20--Info--Push SqStack : OK
2023-2-14 9:53:20--Info--Push SqStack : OK
2023-2-14 9:53:20--Warning--SqStack is Full, Data cannot be pushed
2023-2-14 9:53:20--Warning--SqStack is Full, Data cannot be pushed
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Debug--SqStack Data :
StudentNum : X666
StudentName : Sun
StudentScore : 100
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 101
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 102
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 103
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 104
+++++++++++++++
StudentNum : X666
StudentName : Sun
StudentScore : 105
+++++++++++++++
SqStackLen : 6
SqStackMaxSize : 6
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Info--Pop SqStack : OK
2023-2-14 9:53:20--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 105
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Info--Pop SqStack : OK
2023-2-14 9:53:20--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 104
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Info--Pop SqStack : OK
2023-2-14 9:53:20--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 103
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Info--Pop SqStack : OK
2023-2-14 9:53:20--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 102
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Info--Pop SqStack : OK
2023-2-14 9:53:20--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 101
2023-2-14 9:53:20--Debug--Judge SqStack : Not Empty
2023-2-14 9:53:20--Info--Pop SqStack : OK
2023-2-14 9:53:20--Debug--SqElemType Data:
StudentNum : X666
StudentName : Sun
StudentScore : 100
2023-2-14 9:53:20--Debug--Judge SqStack : Empty
2023-2-14 9:53:20--Warning--SqStack is Empty, Data cannot be poped
2023-2-14 9:53:20--Debug--Judge SqStack : Empty
2023-2-14 9:53:20--Warning--SqStack is Empty, Data cannot be poped
2023-2-14 9:53:20--Debug--SqStack Data :
SqStackLen : 0
SqStackMaxSize : 6
2023-2-14 9:53:20--Info--Clear SqStack : OK
2023-2-14 9:53:20--Info--Destroy SqStack : OK