标题:分享:基本链栈操作
取消只看楼主
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
结帖率:97.5%
已结贴  问题点数:20 回复次数:3 
分享:基本链栈操作
看到论坛有个进制转换的说链栈做,就学了下。
具体实现很简单,跟链表没多大区别。 就多了一个栈顶指针,实际上还是操作尾差法的链表。

程序代码:
#include <stdio.h>
#include <malloc.h>

typedef struct node
{
    int data;
    struct node *next;
}List;

typedef struct stack
{
    int count;
    List *top;
}List_stack;

List_stack *Creat_stack()
{
    List_stack *p;

    p = (List_stack *)malloc(sizeof(p));
    if (p == NULL)
        return p;
    p->count = 0;
    p->top = NULL;

    return p;
}

List_stack *Push_stack(List_stack *p, int elem)
{
    List *temp = NULL;
    temp = (List *)malloc(sizeof(List));
    if (p == NULL || temp == NULL)
        return NULL;

    temp->next = p->top;
    p->top = temp;

    temp->data = elem;
    p->count++;

    return p;

}

void Show_stack(List_stack *p)
{
    List *temp = p->top;
    if (p == NULL)
        printf("The static is empty!\n");
    else
        while (temp != NULL)
        {
            printf("%d ", temp->data);
            temp = temp->next;
        }

}

List_stack *Pop_stack(List_stack *p)
{
    if (p == NULL)
    {
        printf("The static is empty!\n");
        return p;
    }
        
    List *temp = p->top;

    p->top = p->top->next;
    free(temp);
    p->count--;
    
    return p;
}

int main()
{
    List_stack *p;
    int i = 1, n, elem;

    p = Creat_stack();
    printf("How many elements add to stack\n");
    scanf("%d", &n);
    printf("Plese inuput %d elements\n", n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &elem);
        p = Push_stack(p, elem);
    }
    printf("Static %d elements :\n", n);
    Show_stack(p);
    printf("\nStack count : %d\n", p->count);

    printf("\nHow many stack elements to delete\n");
    scanf("%d", &n);
    printf("Delete %d elements back:", n);
    while (n-- > 0)
        p = Pop_stack(p);
    Show_stack(p);
    printf("\nStack count : %d\n", p->count);

    printf("\nHow many elements add to stack\n");
    scanf("%d", &n);
    printf("Plese inuput %d elements\n", n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &elem);
        p = Push_stack(p, elem);
    }
    printf("Add %d elements back\n", n);
    Show_stack(p);
    printf("\nStack count : %d\n", p->count);

    return 0;

}
搜索更多相关主题的帖子: count NULL temp printf elements 
2017-05-19 22:56
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
得分:0 
回复 2楼 九转星河
比起你们大佬还差远了, 望其项背啊!
2017-05-19 23:05
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
得分:0 
回复 5楼 yangfrancis
具体哪儿呢?谢谢
2017-05-20 08:32
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
得分:0 
回复 7楼 renkejun1942
那结构可有可无,每个人看法不一样。free确实是多余的

[此贴子已经被作者于2017-5-20 12:34编辑过]

2017-05-20 12:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-477172-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.229708 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved