zoukankan      html  css  js  c++  java
  • 数组仿真堆栈与链表仿真堆栈(内含数组与链表的差别)

    数组和链表其实都能很好的完成这项任务,唯一不同就是,仿真入栈操作时,数组必须提前给出栈的大小,而链表可以使栈的大小实现动态变化,节省内存...

     其实也就是数组和链表本身的区别了:

    数组: 优点:查找速度快
            缺点:大小固定,不适合动态存储,不方便动态添加。 
        链表:与数组相反。优缺点互换。

    再详细点可以从下面两个方面说: 

    1、从逻辑结构来看
      数组必须事先定义元素个数,不能适应数据动态地增减的情况,当数据增加时,可能超出原先定义的元素个数;当数据减少时,造成内存浪费。
      
       链表动态地进行存储分配,可以适应数据动态地增减的情况,且可以方便地插入、删除数据项,而数组比较麻烦的...  
      
    2、 从内存存储来看  
      (静态)数组从栈中分配空间, 对于程序员方便快速,但是自由度小
      
      链表从堆中分配空间, 自由度大但是申请管理比较麻烦.
    数组中的数据在内存中的按顺序存储的,而链表是随机存储的!
      要访问数组中的元素可以按下标索引来访问,速度比较快。  
        要访问链表中的某个元素的话,那就得从链表的头逐个遍历,直到找到所需要的元素为止,所以链表的随机访问的效率就比数组要低
      
      数组在内存中开辟连续的一块区域,如果一个数据要两个内存单元,一组5个数据10个单元就够了,无需标记其地址,因为数组定义时候标顶了第一个原许的地址,其他四个都知道了。
      链表可以是连续的,也可以是不连续的,但一般都是不连续的,尽管在内存中是连续的,我们把他当作是不连续的,因为如果把他当作是连续的,不如当作是数组了,在某些情况下。一链5个数据,如果每个数据本身用2个内存单元,那么10个单元是不够的,因为每个数据都要表示出下个数据在哪里,所以一个数据本身用2个单元,再用1个单元表示此链下一个数据在什么地址
     

    代码如下:

    数组仿真堆栈:

    #include <stdio.h>

    #define MaxSize 10
    int stack[MaxSize];
    int top = -1;

    void push(int value)
    {
     int i;
     if(top >= MaxSize)
     {
      printf("\nthe stack is full!!\n");
     }
     else
     {
      printf("\nthe stack content before(top->bottom):");
      for(i = top; i >= 0; i--)
      {
       printf("[%d]",stack[i]);
      }
      top++;
      stack[top] = value;
      
      printf("\nthe stack content after push(top->bottom):");
      for(i = top; i >= 0; i--)
      {
       printf("[%d]",stack[i]);
      }
      printf("\n");
     }
    }

    int pop()
    {
     int temp;
     int i;
     
     if(top < 0)
     {
      printf("\nthe stack is empty!!!\n");
      return -1;
     }
     
     printf("\nthe stack content before(top->bottom):");
     for(i = top; i >= 0; i--)
     {
      printf("[%d]",stack[i]);
     }
     temp = stack[top];
     top--;
     printf("\nthe pop value is [%d]",temp);
     
     printf("\nthe stack content after push(top->bottom):");
     for(i = top; i >= 0; i--)
     {
      printf("[%d]",stack[i]);
     }
     printf("\n");
     
     return temp;
    }

    int main()
    {
     int select;
     int value;
     
     printf("\n(1)Input a stack data");
     printf("\n(2)Onput a stack data");
     printf("\n(3)Exit");
     
     printf("\nplease select one =>");
     scanf("%d",&select);
     
     do
     {
      switch(select)
      {
      case 1:
       printf("please input the data =>");
       scanf("%d",&value);
       push(value);
       break;
      case 2:
       value = pop();
       break;
      }
      
      printf("\n(1)Input a stack data");
      printf("\n(2)Onput a stack data");
      printf("\n(3)Exit");
      
      printf("\nplease select one =>");
      scanf("%d",&select);
      printf("\n");
     }while(select!=3);
     
     return 0;
    }

    链表仿真堆栈:

    #include <stdio.h>

    struct node
    {
     int data;
     struct node *next;
    };

    typedef struct node list;
    typedef list *link;
    link stack = NULL;

    void print_stack()
    {
     link temp = NULL;
     temp = stack;
     if(temp == NULL)
     {
      printf("the stack is empty!!\n");
     }
     else
     {
      while(temp != NULL)
      {
       printf("[%d]", temp-> data);
       temp = temp -> next;
      }
      printf("\n");
     }
    }

    void push(int value)
    {
     link newnode;
     printf("\nthe stack content before(top -> bottom):");
     print_stack();
     newnode = (link)malloc(sizeof(list));
     newnode -> data = value;
     newnode -> next = stack;
     stack = newnode;
    }

    int pop()
    {
     link top;
     int temp;
     printf("\nthe stack content before(top -> bottom):");
     print_stack();
     if(stack != NULL)
     {
      top = stack;
      stack = stack -> next;
      temp = top -> data;
      free(top);
      return temp;
     }
     else
      return -1;
    }

    int main()
    {
     link point;
     int select;
     int i, value;
     
     printf("\n(1)Input a stack data");
     printf("\n(2)Onput a stack data");
     printf("\n(3)Exit");
     
     printf("\nplease select one =>");
     scanf("%d",&select);
     
     do
     {
      switch(select)
      {
      case 1:
       printf("please input the data =>");
       scanf("%d",&value);
       push(value);
       printf("\nthe stack content current(top -> bottom):");
       print_stack();
       break;
      case 2:
       value = pop();
       printf("\nthe output value is (%d)", value);
       printf("\n");
       printf("\nthe stack content current(top -> bottom):");
       print_stack();
       break;
      } 
      printf("\n(1)Input a stack data");
      printf("\n(2)Onput a stack data");
      printf("\n(3)Exit");
      
      printf("\nplease select one =>");
      scanf("%d",&select);
     // printf("\n");
     }while(select!=3); 
     return 0;
    }

    嵌入式软件设计
  • 相关阅读:
    yaml文件执行后常见错误解决
    动态存储管理实战:GlusterFS
    Kubernetes角色访问控制RBAC和权限规则(Role+ClusterRole)
    Kubernetes 存储系统 Storage 介绍:PV,PVC,SC
    在容器中管理数据的两种方式
    k8s 如何关联pvc到特定的pv
    Mysqldump 的 的 6 大使用场景的导出命令
    Docker 容器日志管理
    k8s中节点级别的日志
    k8s中pod的容器日志查看命令
  • 原文地址:https://www.cnblogs.com/hao02171990/p/3032967.html
Copyright © 2011-2022 走看看