久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

劍指offer之用鏈表實(shí)現(xiàn)棧(帶頭節(jié)點(diǎn))

 陳喻 2021-10-19

1 問題

用鏈表實(shí)現(xiàn)棧,棧先進(jìn)后出.

2 代碼實(shí)現(xiàn)

#include <stdio.h>
#include <stdlib.h>

#define true 1
#define false 0

typedef struct Node
{
    int value;
    struct Node *next;
} Stack;


/*
 *打印棧
 */
void print(Stack *stack)
{
    if (stack == NULL)
    {
        printf("stack is NULL\n");
        return;
    }
    struct Node *p = stack->next;
    while (p != NULL)
    {
        printf("value is: %d\n", p->value);
        p = p->next;
    }
    return;
}


/**
 *給棧添加一個(gè)節(jié)點(diǎn)
 */
int add_node(Stack *stack, int value)
{
    if (stack == NULL)
    {
        printf("stack is NULL\n");
        return false;
    }
    struct Node *node = NULL;
    node = (struct Node *)malloc(sizeof(struct Node));
    if (node == NULL)
    {
        printf("addNode malloc fail\n");
        return false;
    }
    node->value = value;
    node->next = stack->next;
    stack->next = node;
    return true;
}


/*
 *初始化棧
 */
struct Node* init()
{
    struct Node *head = NULL;
    head = (struct Node *)malloc(sizeof(struct Node));
    if (head == NULL)
    {
        return NULL;
    }
    head->next = NULL;
    head->value = 0;
    return head;
}


/*
 *打印棧的大小
 */
int size_stack(Stack *stack)
{
    if (stack == NULL)
    {
        return 0;
    }
    Stack *head = stack->next;
    int size = 0;
    while (head != NULL)
    {
        ++size;
        head = head->next;
    }
    return size;
}
  

/*
 *彈出棧頂元素
 */
int pop_stack(Stack *stack)
{
    if (stack == NULL)
    {
        printf("stack is NULL");
        return false;
    }
    struct Node *p = stack->next;
    if (p == NULL)
    {
        printf("stack->next is NULL");
        return false;
    }
    stack->next = p->next;
    free(p);
    return true;
}

/*
 *獲取棧頂元素
 */
struct Node* top_stack(Stack *stack)
{
    /**if (stack == NULL);這里自己傻逼了,多加了一個(gè)分號(hào)導(dǎo)致程序走到里面
    {
        printf("stack1 is NULL\n");
        return NULL;
    }**/
    if (stack == NULL)
    {
        printf("stack is is is NULL\n");
        return NULL;
    }
    struct Node *p = stack->next;
    if (p == NULL)
    {
        printf("stack->next is NULL");
        return NULL;
    }
    return p;
}

void clear_stack(Stack *stack)
{
    if (stack == NULL)
    {
        return;
    }
    struct Node *q, *p = stack->next;
    while(p != NULL)
    {
        q = p;
        p = p->next;
        free(q);
    }
    stack->next = NULL;
}


int main()
{

    struct Node *head = init();
    if (head == NULL)
    {
        printf("init stack fail\n");
        return false;
    }
    printf("init success\n");
    add_node(head, 1);
    add_node(head, 2);
    add_node(head, 5);
    add_node(head, 4);
    add_node(head, 3);
    print(head);
    struct Node* top = top_stack(head);
    if (top != NULL)
    {
        printf("top value is %d\n", top->value);
    }
    printf("stack size is %d\n", size_stack(head));
    int result = pop_stack(head);
    if (result == true)
    {
        printf("pop_stack success\n");
    }
    else
    {
        printf("pop_stack fail\n");
    }
    print(head);
    printf("stack size is %d\n", size_stack(head));
    clear_stack(head);
    if (head == NULL)
    {
        printf("head is NULL\n");
    }
    printf("stack size is %d\n", size_stack(head));
    head = init();
    if (head == NULL)
    {
        printf("init stack fail\n");
        return false;
    }
    printf("init success\n");
    add_node(head, 6);
    add_node(head, 5);
    add_node(head, 2);
    add_node(head, 1);
    add_node(head, 9);
    print(head);
    printf("stack size is %d\n", size_stack(head));
    return true;
}
   

3 運(yùn)行結(jié)果

init success
value is: 3
value is: 4
value is: 5
value is: 2
value is: 1
top value is 3
stack size is 5
pop_stack success
value is: 4
value is: 5
value is: 2
value is: 1
stack size is 4
stack size is 0
init success
value is: 9
value is: 1
value is: 2
value is: 5
value is: 6
stack size is 5

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多