m0_75048134 2023-06-10 02:20 采纳率: 100%
浏览 24
已结题

栈顶打印和栈打印出现问题

#为什么我的打印栈顶和打印功能不好使啊


//Stack On Link Structure
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define FALSE   0
#define TRUE    1
 
typedef int StackElementType; //数据元素类型定义

typedef struct node{  //顺序栈的定义
    StackElementType data;
    struct node* next;
}LinkStackNode;

typedef LinkStackNode* LinkStack;


void  InitStack(LinkStack top);//创建一个带头节点链栈 
int IsEmpty(LinkStack top);//判空 
int Push(LinkStack top, StackElementType x); //进栈 
int Pop(LinkStack top, StackElementType *x); //出栈 
int GetTop(LinkStack top, StackElementType *x); //取栈顶元素 
void PrintStack(LinkStack top); //打印栈元素(从栈顶到栈底顺序) 
void ClearStack(LinkStack top);  //把一个带头节的链栈清空(带头节点的空栈)

int main()
{
    int a,b,i;
    StackElementType *x;
    LinkStack *top = (LinkStack*)malloc(sizeof(LinkStackNode)); 
//    LinkStack top; //top就是一个指针变量 

    //生成菜单 
    char sel=' ';
    while(sel!='0')
    {
        
        printf("------栈(链式存储结构)演示系统-------\n");
        printf("   版本:1.0   作者:辛新坪 日期:XXXX-XX-XX\n"); 
        printf("------------------------------------------\n");
        printf("       1.创建空栈\n");
        printf("       2.进栈操作\n");
        printf("       3.出栈操作\n");
        printf("       4.打印栈顶元素\n");
        printf("       5.打印栈\n");
        printf("       6.清空屏幕\n");
        printf("       7.清空栈\n");
        printf("       0.退出系统\n");
        printf("请输入选项[0-7]:"); 
        sel=getch();
        switch(sel)
        {
            case '1':
                printf("创建空栈操作.\n");
                InitStack(*top);
                system("pause"); //按任意键继续 
                break;
            case '2':
                printf("进栈操作.\n"); 
                printf("请输入所需输入的数据个数:");
                scanf("%d",&b); 
               for(i = 0;i <b;i++)
               {
                   printf("请输入数据:");
                scanf("%d",&a); 
                Push(*top, a);
               }
                
                system("pause"); //按任意键继续 
                break;
            case '3':
                printf("出栈操作.\n");
                Pop(*top, x);
                system("pause"); //按任意键继续 
                break;
            case '4':
                printf("打印栈顶元素操作.\n");
                printf("%d",GetTop(*top, x));
                system("pause"); //按任意键继续 
                break;
            case '5':
                printf("打印栈操作.\n");
                PrintStack(*top);
                system("pause"); //按任意键继续 
                break;
            case '6':
                system("cls");
                break;
            case '7':
                printf("释放栈空间操作.\n");
                system("cls");
                break;
            case '0':
                printf("\n谢谢使用,再见!\n");
                break;
            default:
                printf("您输入的选项不合法,请重新选择!\n");
        }
    }

    return 0;
}
void  InitStack(LinkStack top)
{
    top->next = NULL ;
}
int IsEmpty(LinkStack top)
{
    if(top->next = NULL)
    {
    printf("此栈为空!"); 
    }
    
}
int Push(LinkStack top, StackElementType x)//进栈 
{
    LinkStackNode *temp;
    temp = (LinkStackNode*)malloc(sizeof(LinkStackNode));
    if(temp == NULL)
    return(FALSE);
    temp->data = x;
    temp->next = top->next;
    top->next = temp;
    return(TRUE); 
}
int Pop(LinkStack top, StackElementType *x)//出栈 
{
    LinkStack temp;
    temp = top->next;
    if(temp == NULL)
    return(FALSE);
    *x = temp->data;
    top->next = temp->next;
    
    free(temp);
    return(TRUE);
    
}
int GetTop(LinkStack top, StackElementType *x) //打印栈顶 
{
    if(top->next == NULL)
    return(FALSE);
    else
    {
        *x = top->data;
    //    printf("%d\n",*x);
        return(*x);
    }
}
void PrintStack(LinkStack top)
{
    LinkStackNode *temp;
    
    temp = top;
    if(top->next != NULL)
    {
        printf("%d",temp->data);
        temp = top->next;
    }
}

  • 写回答

3条回答 默认 最新

  • Loup&卡普 2023-06-12 15:06
    关注
    int GetTop(LinkStack top, StackElementType *x) //打印栈顶 
    {
        if(top->next == NULL)
        return(FALSE);
        else
        {
             x* = top->next->data; //  此处修改 *x = top->data; 
            return(*x);
        }
    }
    
    void PrintStack(LinkStack top)
    {
        LinkStackNode *temp;
        
        temp = top->next;
        while (temp != NULL) // if 改为 while,  top->next 改为 temp
        {
            printf("%d",temp->data);
            temp = temp->next; // top 改为 temp
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月24日
  • 已采纳回答 6月16日
  • 创建了问题 6月10日

悬赏问题

  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多