目錄
-
-
-
- (一)棧的鍊式結構體
- (二)建立一個空棧
- (三)清空棧
- (四)判空
- (五)擷取元素個數
- (六)入棧
- (七)出棧
- (八)輸出
- (八)調用的主函數
- 全部的組合代碼
(一)棧的鍊式結構體
typedef int Status;
typedef int EleType;
typedef struct StackNode {
EleType data;
struct StackNode* next;
}StackNode,* LinkStackPoi;
typedef struct LinkStack {
LinkStackPoi top;
int count;
}LinkStack;
(二)建立一個空棧
Status InitLinkStack(LinkStack* stack)
{
if (!stack)
{
return ERROR;
}
stack->top = NULL;
stack->count = 0;
return OK;
}
(三)清空棧
Status ClearLinkStack(LinkStack* stack)
{
if (!stack||!stack->count)
{
return ERROR;
}
while (stack->count)
{
StackNode* node = stack->top;
stack->top = node->next;
free(node);
stack->count--;
}
return OK;
}
(四)判空
Status EmptyLinkStack(LinkStack* stack) {
if (!stack)
{
return ERROR;
}
return stack->count == 0 ? 1 : 0;
}
(五)擷取元素個數
int GetLengthLinkStack(LinkStack* stack)
{
if (!stack )
{
return -1;
}
return stack->count;
}
(六)入棧
Status pop(LinkStack* stack,EleType *e)
{
if (!stack && stack->count)
{
return ERROR;
}
StackNode* node = stack->top;
*e = node->data;
stack->top = node->next;
free(node);
stack->count--;
return OK;
}
(七)出棧
Status push(LinkStack* stack,EleType e)
{
if (!stack)
{
return ERROR;
}
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
node->next = stack->top;
node->data = e;
stack->top = node;
stack->count++;
return OK;
}
(八)輸出
void PrintfLinkStack(LinkStack* stack)
{
if (!stack&&stack->count)
{
return;
}
StackNode* node = stack->top;
while (node)
{
printf("%d,", node->data);
node = node->next;
}
puts("");
return;
}
(八)調用的主函數
int main(int argc, char *argv[])
{
LinkStack stack;
InitLinkStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
push(&stack, 4);
push(&stack, 5);
puts("鍊棧元素:");
PrintfLinkStack(&stack);
printf("鍊棧元素個數:%d\n", GetLengthLinkStack(&stack));
EleType e1,e2;
pop(&stack, &e1);
printf("彈出第一個元素:%d\n", e1);
pop(&stack, &e2);
printf("彈出第二個元素:%d\n", e2);
puts("鍊棧元素:");
PrintfLinkStack(&stack);
printf("鍊棧元素個數:%d", GetLengthLinkStack(&stack));
printf("\n");
return 0;
}
全部的組合代碼
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int EleType;
typedef struct StackNode {
EleType data;
struct StackNode* next;
}StackNode,* LinkStackPoi;
typedef struct LinkStack {
LinkStackPoi top;
int count;
}LinkStack;
Status InitLinkStack(LinkStack* stack)
{
if (!stack)
{
return ERROR;
}
stack->top = NULL;
stack->count = 0;
return OK;
}
Status ClearLinkStack(LinkStack* stack)
{
if (!stack||!stack->count)
{
return ERROR;
}
while (stack->count)
{
StackNode* node = stack->top;
stack->top = node->next;
free(node);
stack->count--;
}
return OK;
}
Status EmptyLinkStack(LinkStack* stack) {
if (!stack)
{
return ERROR;
}
return stack->count == 0 ? 1 : 0;
}
//擷取元素個數
int GetLengthLinkStack(LinkStack* stack)
{
if (!stack )
{
return -1;
}
return stack->count;
}
Status GetTop(LinkStack* stack, StackNode** stackNode)
{
if (!stack)
{
return ERROR;
}
*stackNode = stack->top;
return OK;
}
Status pop(LinkStack* stack,EleType *e)
{
if (!stack && stack->count)
{
return ERROR;
}
StackNode* node = stack->top;
*e = node->data;
stack->top = node->next;
free(node);
stack->count--;
return OK;
}
Status push(LinkStack* stack,EleType e)
{
if (!stack)
{
return ERROR;
}
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
node->next = stack->top;
node->data = e;
stack->top = node;
stack->count++;
return OK;
}
void PrintfLinkStack(LinkStack* stack)
{
if (!stack&&stack->count)
{
return;
}
StackNode* node = stack->top;
while (node)
{
printf("%d,", node->data);
node = node->next;
}
puts("");
return;
}
int main(int argc, char *argv[])
{
LinkStack stack;
InitLinkStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
push(&stack, 4);
push(&stack, 5);
puts("鍊棧元素:");
PrintfLinkStack(&stack);
printf("鍊棧元素個數:%d\n", GetLengthLinkStack(&stack));
EleType e1,e2;
pop(&stack, &e1);
printf("彈出第一個元素:%d\n", e1);
pop(&stack, &e2);
printf("彈出第二個元素:%d\n", e2);
puts("鍊棧元素:");
PrintfLinkStack(&stack);
printf("鍊棧元素個數:%d", GetLengthLinkStack(&stack));
printf("\n");
return 0;
}