C 언어 구현 창고 의 체인 저장 구조

19290 단어 데이터 구조
C 언어 구현 창고 의 체인 저장 구조
/*
 * @Author: xgh
 * @Date: 2020-06-23 22:25:32
 * @LastEditTime: 2020-06-28 14:44:10
 * @LastEditors: Please set LastEditors
 * @Description:         
 * @FilePath: \VS-CODE-C\.vscode\StackLinkedList\stackLinkedLists.c
 */
#include 
#include 

typedef int ElemType;
typedef int Statue;

#define ERROR 0
#define SUCCESS 1
#define OVER 0
#define EMPTY 0
#define NOT_EMPTY 1

//      
typedef struct StackNode
{
    ElemType data; //       
    struct StackNode *next;
} StackNode, *LinkStackPtr;

typedef struct LinkStack
{
    LinkStackPtr top; // top  
    int count;        //       
} linkStack, *LinkStack;

Statue InitStack(LinkStack *L);
Statue push(LinkStack L, ElemType e);
Statue pull(LinkStack L, ElemType *e);
int isEmpty(LinkStack L);
Statue showStack(LinkStack L);

int main(void)
{

    LinkStack linkStack;
    int i;
    ElemType data;

    //     
    InitStack(&linkStack);

    isEmpty(linkStack);

    //     
    for (i = 0; i < 6; i++)
    {
        push(linkStack, i);
    }

    showStack(linkStack);

    int count = linkStack->count;
    printf("     :");
    for(i = 0; i < count; i++){
        pull(linkStack, &data);
        printf("%4d", data);
    }
    printf("
"
); isEmpty(linkStack); printf("

"
); return 0; } /** * @description: * @param {LinkStack L: } * @return: EMPTY: * @return: NOT_EMPTY: */ int isEmpty(LinkStack L) { /* if(L->top == NULL){ printf("
"); return EMPTY; }else{ printf("
"); return NOT_EMPTY; } */
if (L->count == 0) { printf("
"
); return EMPTY; } else { printf("
"
); return NOT_EMPTY; } } /** * @description: , * @param {LinkStack L: } * @return: SUCCESS: */ Statue InitStack(LinkStack *L) { (*L) = (LinkStack)malloc(sizeof(linkStack)); if ((*L) == NULL) { printf("

"
); exit(ERROR); } printf("

"
); (*L)->top = NULL; (*L)->count = 0; return SUCCESS; } /** * @description: * @param {LinkStack L: } * @param {ElemType e: } * @return: SUCCESS: * @return: ERROR: */ Statue push(LinkStack L, ElemType e) { // LinkStackPtr pushNode = (LinkStackPtr)malloc(sizeof(StackNode)); // if (pushNode == NULL) { printf(" !

"
); return ERROR; } // e pushNode->data = e; // pushNode->next = L->top; // L->top = pushNode; ++L->count; return SUCCESS; } /** * @description: * @param {LinkStack L: } * @param {ElemType *e: } * @return: ERROR: * @return: SUCCESS: */ Statue pull(LinkStack L, ElemType *e) { if(!(L->count)){ return ERROR; } LinkStackPtr node = L->top; // *e = node->data; // L->top = node->next; free(node); // --L->count; return SUCCESS; } /** * @description: * @param {LinkStack L: } * @return: EMPTY: * @return: SUCCESS: */ Statue showStack(LinkStack L) { LinkStackPtr showNode; if (!isEmpty(L)) { return EMPTY; } showNode = L->top; printf(" :"); do { printf("%4d", showNode->data); showNode = showNode->next; } while (showNode->next != NULL); printf("%4d", showNode->data); printf("

"
); return SUCCESS; }

좋은 웹페이지 즐겨찾기