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

    }


  • 相关阅读:
    常见的概念
    cas底层
    判断页面是否读取了缓存
    window.location.hash(hash应用)---跳转到hash值制定的具体页面
    * 输入框被第三方输入法遮挡问题
    Mui去掉滚动条:
    实用网址
    完美解决safari、微信浏览器下拉回弹效果。
    移动端兼容性问题解决方案
    监听ios自带返回功能
  • 原文地址:https://www.cnblogs.com/accept/p/8175867.html
Copyright © 2011-2022 走看看