数据结构 链栈初始化及入出栈
发布日期:2021-05-10 03:16:59 浏览次数:16 分类:精选文章

本文共 3203 字,大约阅读时间需要 10 分钟。

������������������������������������������C������������������������������������������������������������������������push���������������pop���������������������������print���������������������������������������������������������������������������������������������������������������������������������������

������������������

���������������������������FILO������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������data��� next���data���������������������������next������������������������������

���������������

1. push������

push���������������������������������������������������������������������������������

  • ���������������������������������
  • ������������������ next ������Constants NULL������������������������������������
  • ��������������������������������������������������������������� next���
  • ������������������������������1���
  • int push(chain_stack *S, int x)
    {
    chain_stack *node = (chain_stack *)malloc(sizeof(chain_stack));
    node->data = x;
    node->next = S->next;
    S->next = node;
    return 1;
    }

    2. pop������

    pop���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    int pop(chain_stack *s)
    {
    if (s->next != NULL)
    {
    chain_stack *temp = s->next;
    int x = temp->data;
    s->next = temp->next;
    free(temp);
    return x;
    }
    return -99999; //���������������
    }

    3. print������

    print��������������������������������������������������������������������������������������������������������������� data������������������

    void print(chain_stack *S)
    {
    chain_stack *head = S->next;
    while (head != NULL)
    {
    cout << head->data << " ";
    head = head->next;
    }
    cout << endl;
    }

    4. main������

    main���������������������������������������������������������������������������������������������������������������

    int main()
    {
    chain_stack *s = (chain_stack *)malloc(sizeof(chain_stack));
    s->next = NULL;
    //������������
    push(s, 1);
    push(s, 2);
    push(s, 3);
    //���������������
    print(s);
    //���������
    int value = pop(s);
    cout << "���������������" << value << endl;
    //���������������������
    if (s->next == NULL)
    {
    cout << "������������" << endl;
    }
    else
    {
    cout << "������������" << endl;
    }
    }

    ������������

  • stack.h ��� cstdlib.h ������������������������#include������������������������������ declarations���
  • chain_stack ������������������������������������������������������ data ��� next ���������
  • push ������������������������������������������������������������������������
  • print ��������������������������������������� data ���������
  • pop ���������������������������������������������������������������������������������������������������������
  • main ������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������

    上一篇:Python opencv(八) 边缘保留滤波(EPF)
    下一篇:数据结构 双端栈初始化和入出栈

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月22日 03时24分06秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章