zoukankan      html  css  js  c++  java
  • 实验三 栈的实现及应用

    先确定一个小目标:

    建立一个数栈,数据类型为整型数据,分别用顺序栈和链栈完成以下功能:

    1、编写取栈顶元素、入栈、出栈算法;

    2、通过进制转化验证上述是三个算法(原数据,拟转化的进制从键盘输入,输出转化后的结果);


    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    typedef struct Stack
    {
    int data;
    struct Stack * next;
    }Stack;
    Stack * InitStack()//初始化
    {
    Stack * p=(Stack*)malloc(sizeof(Stack));
    if(p!=NULL)
    {
    p->data=0;
    p->next=NULL;
    }
    return p;
    }
    Stack* push(Stack *head,int x)
    {
    Stack *p=(Stack *)malloc(sizeof(Stack));
    if(p==NULL)
    {
    return NULL;
    }
    p->data=x;
    p->next=head;
    head=p;

    return head;
    }
    Stack * pop(Stack *head)
    {
    if(head->next==NULL)
    {
    return NULL;
    }
    Stack *p;
    p=head;
    head=head->next;
    free(p);
    //printf("%d",head->data);
    return head;
    }
    int top(Stack * head,int *l)
    {
    if(head->next==NULL)
    return -1;
    *l=head->data;
    printf("%d",head->data);
    return 0;
    }
    int visit(Stack * head)
    {
    Stack *p=head;
    if(p->next==NULL)
    {
    printf("栈为空\n");
    return -1;
    }
    while(p->next!=NULL)
    {
    printf("%d ",p->data);
    p=p->next;
    }
    }
    int StackEmpty(stack *head)//判断栈是否为空
    {
    if(head==NULL)
    {
    printf("栈为空");
    }
    else
    return 1;

    }
    int main()
    {
    int a,b,x,l;
    scanf("%d",&b);
    Stack * s;
    s=InitStack();
    while(b!=0)
    {
    a=b%2;
    b=b/2;
    s=push(s,a);
    }
    visit(s);

    }


  • 相关阅读:
    单例模式
    json 格式
    axios 获取不到数据错误
    sprint test 添加事务回滚机制
    springboot An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    spring boot 启动之后404
    废了
    tomcat 部署项目到服务器
    Druid 介绍及配置
    jq 全选
  • 原文地址:https://www.cnblogs.com/accept/p/8175867.html
Copyright © 2011-2022 走看看