zoukankan      html  css  js  c++  java
  • 数据结构趣题——顺序表就地逆置

    利用原表的存储空间将顺序表(a1,a2,……,an)逆置为(an,an-1,………a1)。

       1: #include <stdio.h>
       2: #include <stdlib.h>
       3:  
       4: # define MAXSIZE 10
       5: typedef struct {
       6:     int * base;
       7:     int length;
       8: } sqlist ;
       9:  
      10: void reverseSQ(sqlist *l) {
      11:     int low = 0 , high = l->length - 1;
      12:     int buf , i;
      13:  
      14:     for(i = 0; i < l->length / 2; i++)
      15:     {
      16:         buf = l->base[low] ;
      17:         l->base[low] = l->base[high];
      18:         l->base[high] = buf;
      19:         low++;
      20:         high--;
      21:     }
      22: }
      23:  
      24: int main()
      25: {
      26:     sqlist l;
      27:     int a , i = 0;
      28:     /*创建一个顺序表*/
      29:     l.base = (int *)malloc(sizeof(int) * MAXSIZE);
      30:     l.length = 0;
      31:  
      32:     /*输入数据*/
      33:     printf("Please input below 10 integer into the sqlist\n") ;
      34:     printf("Type -1 for stopping input\n");
      35:     scanf("%d", &a);
      36:  
      37:     while(a != -1 && i <= 9)
      38:     {
      39:         l.base[i] = a;
      40:         l.length++;
      41:         i++;
      42:         scanf("%d", &a);
      43:     }
      44:  
      45:     /*输出原顺序表中的数据*/
      46:     printf("The contents of the sqlist are\n");
      47:  
      48:     for(i = 0; i < l.length; i++)
      49:         printf("%d ", l.base[i]);
      50:  
      51:     printf("\n");
      52:  
      53:     reverseSQ(&l);    /*就地逆置顺序表*/
      54:  
      55:  
      56:     /*输出逆置后的顺序表中的数据*/
      57:     printf("The contents of the reversed sqlist are\n");
      58:  
      59:     for(i = 0; i < l.length; i++)
      60:         printf("%d ", l.base[i]);
      61:  
      62:     return 0;
      63: }
  • 相关阅读:
    yocto/bitbake 学习资源
    QEMU/KVM学习资源
    ubuntu 中创建和删除用户
    git 重命名本地和远程分支
    Ubuntu 上搭建 FTP 服务器
    gdb 常见用法
    git log 显示与特定文件相关的 commit 信息
    基于 qemu system mode 运行 arm 程序
    基于 qemu user mode 运行 aarch64 程序
    checking in(airport)
  • 原文地址:https://www.cnblogs.com/steven_oyj/p/1745941.html
Copyright © 2011-2022 走看看