数据结构(代码实现)(二):堆栈

//顺序存储
#definr MaxSize 最大个数
typedef struct {
            ElenmentType Data[MaxSize];
            int Top;
}Stack;

//链式存储
typedef struct Node{
            ElementType Data;
            struct Node *Next;
}LinkStack;

//链式存储构建链表
LinkStack* CreateStack()
{
LinkStack *S;
S = (LinkStack*)malloc(sizeof(struct Node));
S->Next = NULL;
return S;
}

//PUSH
void Push(ElementType item, LinkStack *S)
{    struct Node *TmpCell;
     TmpCell = (LinkStack*)malloc(size of(struct Node));
     TmpCell->Data = item;  
     TmpCell->Next = S->Next;   
     S->Next = TmpCell;}
 
 
//POP
ElementType Pop(LinkStack *S)
{
    struct Node * FisrtCell;
    ElementType TopElem;
    if (IsEmpty(S)){
        printf("堆栈空"); return NULL;
    }else {
        FirstCell = S->Next;
        S->Next = FirstCell->Next;
        TopElem = FirstCell->Data;
        free(FirstCell);
        return TopElem;
}


猜你喜欢

转载自blog.csdn.net/qq_30207251/article/details/80591396