• 实验三 栈的实现及应用


    先确定一个小目标:

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

    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);

    }


  • 相关阅读:
    AutoMapperHelper
    EmitMapper的使用
    hdu5396 Expression 区间dp +排列组合
    Eclipse 4.2 安装Java反编译插件
    hdu 1728 逃离迷宫 bfs记步数
    阿里云部署Docker(2)
    程序猿面试宝典(第四版)——读书笔记-1、第五章:程序设计基本概念
    IOS
    hdu 5078 2014鞍山现场赛 水题
    资源文件
  • 原文地址:https://www.cnblogs.com/accept/p/8175867.html
Copyright © 2020-2023  润新知