顺序栈 链栈

来源:互联网 发布:艾弗森生涯数据 编辑:程序博客网 时间:2024/06/05 01:09

开始积累并且熟练常用的数据结构和算法,实践实践再实践!!!!!!


以前的不足之处在于动手太少,来到一个新的环境,新的挑战,科研要搞,论文要读,代码也一定要坚持写,坚持学习,不断提高自己,少抱怨,多脚踏实地思考实践。


虽然自己很菜,虽然项目坑爹,虽然读论文有时候很枯燥很无聊甚至以后根本用不上这些毕竟产业界还是要靠真本事吃饭而不是忽悠两下就能混下去的。


没关系,至少我还有时间呀,我还有一颗努力提高评自己的决心和毅力!!!我一定会把linux c c++ 服务端编程学好的!!!不为什么,因为我喜欢研究这个呀!


加油,静下心来,每天坚持提高,笨鸟先飞!!! 


送给自己:沉淀,积累,宁静致远




看代码没用,理解了必须手写实践!!!


顺序栈:

#include <stdio.h>#include <stdlib.h>#define ElemType inttypedef struct {    ElemType *base;    int top;    int length;}Stack; /* * we keep a contract here as the same as the Linux lib * return value 0 is successful, either -1 means failed */int init_Stack(Stack *s, int length) {    if ((s->base = (ElemType *)malloc(length * sizeof(ElemType))) == NULL) {        fprintf(stdout, "init stack error\n");         return -1;    }    s->top = -1;    s->length = length;    return 0;}int empty_stack(Stack *s){    if (s->top == -1)        return 0;       //empty    else        return -1;      //not empty}int full_stack(Stack *s){    if (s->top == s->length - 1)        return 0;       //full    else        return -1;      //not full}int push_stack(Stack *s, ElemType value){    if (full_stack(s) == 0)        return -1;      //stack full, failed    s->top++;    s->base[s->top] = value;     return 0;           //successful}int pop_stack(Stack *s, ElemType *value){    if (empty_stack(s) == 0)        return -1;    *value = s->base[s->top];    s->top--;    return 0;}int top_stack(Stack *s, ElemType *value){    if (empty_stack(s) == 0)         return -1;      //faled    *value = s->base[s->top];    return 0;           //successful}void destroy_stack(Stack *s){    free(s->base);     free(s);}int main(void){    Stack *s;    if ((s = (Stack *)malloc(sizeof(Stack))) == NULL) {        fprintf(stdout, "stack obj init error\n");         exit(-1);    }    if (init_Stack(s, 15) < 0) {        fprintf(stdout, "init stack error\n");         exit(-1);    }    push_stack(s, 19);    push_stack(s, 1);    push_stack(s, 99);    ElemType test;    pop_stack(s, &test);    fprintf(stdout, "pop now is :%d\n", test);    top_stack(s, &test);     fprintf(stdout, "top now is :%d\n", test);        destroy_stack(s);    return EXIT_SUCCESS;}



链栈:


#include <stdio.h>#include <stdlib.h>#define ElemType inttypedef struct STACK{    struct STACK_NODE *head;    size_t actual;    size_t length;}Stack;typedef struct STACK_NODE{    ElemType data;    struct STACK_NODE *next;}StackNode;/* * confront to the Linux lib * return 0 means successful * return -1 means failed */int init_stack(Stack *s, size_t length){    StackNode *head = NULL;    /* make a stack head, then make stack_node */    if ((s = (Stack *)malloc(sizeof(Stack))) == NULL) {        perror("init stack error\n");         return -1;    }    /* head node */    if ((head = (StackNode *)malloc(sizeof(StackNode))) == NULL) {        perror("init stack head error\n");         return -1;    }    head->data = 0;    head->next = NULL;    s->head = head;    s->length = length;    s->actual = 0;    return 0;}void destroy_stack(Stack *s){    StackNode *head = s->head->next;    while (head != NULL) {        StackNode *tmp = head->next;        free(head);        head = tmp;    }    free(s->head);    free(s);}int is_empty(Stack *s){    if (s->head->next == NULL) // the link has a head        return 0;           //empty    else        return -1;          //not empty}int is_full(Stack *s){    if (s->actual == s->length)         return 0;           //full    else        return -1;          //not full}int push_stack(Stack *s, ElemType *value){     /* insert the node to the head */    StackNode *tmp = NULL;    if ((tmp = (StackNode *)malloc(sizeof(StackNode))) == NULL)        return -1;    tmp->data = *value;    tmp->next = s->head->next;    s->head->next = tmp;    return 0; }int pop_stack(Stack *s, ElemType *value){    if (is_empty(s) == 0)        return -1;    *value = s->head->next->data;    StackNode *tmp = s->head->next;    s->head->next = s->head->next->next;    free(tmp);    return 0;}int top_stack(Stack *s, ElemType *value){    if (is_empty(s) == 0)        return -1;    *value = s->head->next->data;    return 0;}




0 0