每周学习总结(技术博客)

本文通过C++代码示例介绍了栈的基本概念、结构定义及操作实现,包括初始化、进栈、出栈等核心功能,并展示了如何使用这些功能进行实际操作。

// 叶宏杰周实践.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include "stdlib.h"

#include "stdio.h"

typedef char ElemType;

typedef struct  StackNode       /* 结点结构 */

{

    ElemType data;

    struct StackNode *next;

} StackNode;

typedef struct   /* 栈结构 */

{

    StackNode *top;        /* 栈顶指针 */

    int length;             /* 栈中元素个数 */

} Stack;

/*初始化*/

void InitStack(Stack *S)

{

    S->top=NULL;

    S->length=0;

}

//进栈—将数据元素x压入栈S中

void Push (Stack *s, ElemType e)

{

    StackNode * p=( StackNode *)malloc(sizeof(StackNode));

    if(!p) exit(0);

    p->data=e;

    p->next=s->top;

    s->top=p;

    s->length++;

}

//判断栈是否为空

int StackEmpty(Stack S)

{

    if(S.top==NULL) return 1;

    else return 0;

}

void pop(Stack *s, ElemType *e)

{  StackNode *p;

if(StackEmpty(*s))  {  printf("Stack is empty"); exit(0); }

p=s->top;

s->top = p->next;

*e = p->data;

free (p);

s->length--;

}

 

//出栈

void PrintStack(Stack *s){

    if(s==NULL) return;

    StackNode *p=s->top;

    while(p!=NULL){

        printf("%c ",p->data);

        p=p->next;

    }

    printf("Length=%d\n",s->length);

}

 

int main(int argc, char* argv[])

{ElemType e;

    Stack s;

    InitStack(&s);

    Push(&s,'A');

    Push(&s,'B');

    Push(&s,'C');

    Push(&s,'D');

    pop(&s,&e);

    printf("e=%c\n",e);

    pop(&s,&e);

    printf("e=%c\n",e);

    Push(&s,'E');

    pop(&s,&e);

    printf("e=%c\n",e);

    pop(&s,&e);

    printf("e=%c\n",e);

    pop(&s,&e);

    printf("e=%c\n",e);

    PrintStack(&s);

    return 0;

}

在学习的时候一定要注意这几点,不会的一定要问明白,不管谁能让他教会你知识就是你的了,要学会让知识为我所用。在看书的时候一定要做好标记,特别是不懂的地方一定要标明是什么意思。

转载于:https://www.cnblogs.com/dakaceng/p/5365075.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值