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

// 叶宏杰周实践.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;

}

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

posted on 2016-04-07 19:42  oOoooOoOoOOO  阅读(210)  评论(1编辑  收藏  举报