找回密码
 注册
搜索
热搜: 超星 读书 找书
查看: 683|回复: 2

[【解决】] 一道简单算法设计题(略通C预言者可应助)

[复制链接]
发表于 2005-7-1 18:49:00 | 显示全部楼层 |阅读模式
帮帮忙吧,一道简单算法设计题。

往数组中读入10个整数,并逐个压入栈,打印原始表并通过弹出元素的方法打印栈内容

要求给出源程序

email:jackbudt@163.com
回复

使用道具 举报

发表于 2005-7-7 21:50:09 | 显示全部楼层
题目不清楚哦,是不要要模拟一个堆栈出来呢?
回复

使用道具 举报

lxjlan 该用户已被删除
发表于 2005-7-8 12:19:12 | 显示全部楼层
#include "malloc.h"

typedef struct astack *Stack;
typedef struct astack{
    int top;
    int maxtop;
    int *data;
}Astack;

Stack StackInit(int size)
{
    Stack S=(Stack)malloc(sizeof(*Stack));
    S->top=-1;
    S->maxtop=size;
    S->data=(int *)malloc(size*sizeof(int));
    return S;
}

int StackFull(Stack s)
{
    return S->top>=S->maxtop;
}

int StackEmpty(Stack S)
{
    return S->top==-1;
}

void push(int x,Stack S)
{
    if(!StackFull(S))S->data[++S->top]=x;
    else printf("Stack is full");
}

int pop(Stack S)
{
    if(StackEmpty(s)){printf("The stack is empty");return 0;}
    return S->data[top--];
}

void StackPrint(Stack s)
{  int i=0;
    if(StackEmpty(S))printf("the stack is empty");
    else {printf("print the stack from bottom to top :\n");while(i<=S->top)printf("/t%d",S->data);}
}

void main(void)
{
    int a[10],i;

    printf("enter the 10 number:\n");
    for(i=0;i<10;i++)
    {
        printf("the %d number is :",i+1);
        scanf("%d",&a);
    }

    S=StackInit(10);

    for(i=0;i<10;i++)
    {
        push(a,S);
    }
    StackPrint(S);

    for(i=0;i<=10;i++)
    {
        if(StackEmpty(S)){printf("stack is empty");break;}
        else printf("now,the top number is : %d\n",Pop(S));
    };

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|网上读书园地

GMT+8, 2024-9-22 01:28 , Processed in 0.130737 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表