今天學習了利用數組方式的棧的C++實現,這種方式跟指針實現有很多不一樣的地方:
棧的指針實現,棧的創建申請頭結點,push需要申請新的結點,pop釋放結點,這些結點都放在第一個位置,top時,S->next->data即可。
棧的數組實現,只申請一個結點,該結點的結構體內包含,數組的最大容量、棧頂元素下標、指向整形數組的指針(用於存放和刪除新的元素)。
S->topOfStack == -1,空棧;
S->topOfStack == S->capacity - 1,滿棧;
1、聲明結點
1 struct Node 2 { 3 int capacity; //數組的最大容量 4 int topOfStack; //棧頂下標為-1表示空棧,每次添加新的元素,棧頂元素下標加1 5 int *Array; //指向整形數組的指針 6 }; 7 typedef struct Node stack;
2、空棧,滿棧判斷
1 int stackArray::isEmpty(stack *S) 2 { 3 return S->topOfStack == emptyTOS; 4 } 5 int stackArray::isFull(stack *S) 6 { 7 return S->topOfStack == S->capacity - 1; 8 }
3、創建棧
1 stackArray::stack *stackArray::createStack(int maxElements) 2 { 3 if (maxElements < minStackSize) 4 cout << "the space of stack is too short,please increase value of maxElements!" << endl; 5 6 stack *S; 7 S = (stack*)new(stack); 8 if (S == NULL) 9 cout << "Out of space! " << '\n'; 10 11 S->Array = new int[maxElements]; 12 if (S->Array == NULL) 13 cout << "Out of space! " << '\n'; 14 15 S->topOfStack = emptyTOS; //棧頂下標置-1表示空棧 16 S->capacity = maxElements; //數組最大容量賦值 17 makeEmpty(S); 18 return S; 19 }
5、push,top,pop
1 stackArray::stack *stackArray::push(stack *S) 2 { 3 if (isFull(S)) 4 { 5 cout << "stack is full!" << endl; 6 return 0; 7 } 8 int x = 0; 9 cout << "Please input the data to push: " << endl; 10 scanf_s("%d", &x); 11 S->Array[++S->topOfStack] = x; 12 return S; 13 } 14 int stackArray::top(stack *S) 15 { 16 if (isEmpty(S)) //非空判斷 17 { 18 cout << "empty stack! " << endl; 19 return -1; 20 } 21 else 22 return S->Array[S->topOfStack]; 23 } 24 stackArray::stack *stackArray::pop(stack *S) 25 { 26 if (isEmpty(S)) //非空判斷 27 { 28 cout << "empty stack! " << endl; 29 return 0; 30 } 31 else 32 { 33 S->topOfStack--; 34 return S; 35 } 36 }
6、主函數
1 int main(int argc, char * argv[]) 2 { 3 cout << '\n' << "***************************************" << '\n' << '\n'; 4 cout << "Welcome to the stackArray world! " << '\n'; 5 cout << '\n' << "***************************************" << '\n' << '\n'; 6 7 int i = 1; 8 int j = 0; 9 int topElement = 0; 10 stackArray *a = new stackArray; 11 stackArray::stack *S = NULL; 12 //int x = 0; 13 while (i) 14 { 15 cout << '\n' << "***************************************" << '\n'; 16 cout << " 0 : end the stack " << '\n'; 17 cout << " 1 : creat a stack " << '\n'; 18 cout << " 2 : display the top element of stack " << '\n'; 19 cout << " 3 : push a node in the stack " << '\n'; 20 cout << " 4 : pop a node from the stack " << '\n'; 21 cout << "***************************************" << '\n'; 22 cout << "Please input the function your want with the number above : " << '\n'; 23 scanf_s("%d", &j); 24 25 switch (j) 26 { 27 case 1: 28 cout << "CreatStack now begin : "; 29 S = a->createStack(5); 30 break; 31 case 2: 32 topElement = a->top(S); 33 cout << "The top element of stack is : " << topElement; 34 break; 35 case 3: 36 cout << "push now begin : "; 37 S = a->push(S); 38 break; 39 case 4: 40 cout << "pop now begin : "; 41 S = a->pop(S); 42 break; 43 default: 44 cout << "End the stack. "; 45 a->disposeStack(S); 46 i = 0; 47 break; 48 } 49 50 } 51 52 return 0; 53 }
效果同指針,不再贅述。