C 언어 데이터 구조 - 창고 - 체인 테이블 실현

1736 단어 데이터 구조
스 택 헤더 파일:
#ifndef LINKSTACK_H_INCLUDED
#define LINKSTACK_H_INCLUDED
typedef struct Node{
    int data;
    struct Node *next;
}Node, *PNode;

typedef struct LinkStack{
    PNode top;
}LinkStack, *PLinkStack;

PLinkStack init(void);

int push(int data,PLinkStack pStack);

int pop(PLinkStack pStack,int *data);

int getTop(PLinkStack pStack,int *data);

void printStack(PLinkStack pStack);

#endif // LINKSTACK_H_INCLUDED

스 택 코드 구현:
#include 
#include 
#include "LinkStack.h"

PLinkStack init(void){
    PLinkStack pStack=(PLinkStack)malloc(sizeof(struct LinkStack));
    pStack->top=NULL;
}

//    : nulldata=data;
    pNode->next=pStack->top;
    pStack->top=pNode;
}

int pop(PLinkStack pStack,int *data){
    PNode tNode=pStack->top;
    if(tNode==NULL){
        return 0;
    }
    //        
    *data=tNode->data;
    pStack->top=tNode->next;
    free(tNode);
    return 1;
}

int getTop(PLinkStack pStack,int *data){
    PNode tNode=pStack->top;
    if(tNode==NULL){
        return 0;
    }
    *data=tNode->data;
    return 1;
}

void printStack(PLinkStack pStack){
    PNode tNode=pStack->top;
    while(tNode!=NULL){
        printf("Node:%d->",tNode->data);
        tNode=tNode->next;
    }
    printf("
"); } int main() { PLinkStack linkStack=init(); push(1,linkStack); push(2,linkStack); push(3,linkStack); int data; pop(linkStack,&data); printf("pop top data=%d
",data); int top; getTop(linkStack,&top); printf("current top data=%d
",top); printStack(linkStack); return 0; }

좋은 웹페이지 즐겨찾기