广工Anyview数据结构习题

欢迎借鉴讨论 (doge)
建议直接访问这里👉数据结构习题永久地址
GitHub仓库地址:gdut_DataStructure_Anyview_exercise

目录

第一章

DC01PE06 将三个整数按升序重新排列

试写一算法,如果三个整数a,b和c的值不是依次非递增的,则通过交换,令其为非递增。
要求实现下列函数:
void Descend(int &a, int &b, int &c);
/* 通过交换,令 a >= b >= c */

#include "allinclude.h"  //DO NOT edit this line
void Descend(int &a, int &b, int &c) {
    // 通过交换,令 a >= b >= c
    // Add your code here
    int temp;
    if(a < c){
   
      temp = c;
      c = a;
      a = temp;
    }
    if(b < c){
   
      temp = b;
      b = c;
      c= temp;
    }
    if(b > a){
   
      temp = a;
      a = b;
      b = temp;
    }
}

DC01PE08 求一元多项式的值

试编写算法求一元多项式
P(x) = a0 + a1x + a2x^2 + … + anx^n
的值P(x0),并确定算法中每一语句的执行次数和整个算法的时间复杂度。
要求实现下列函数:
float Polynomial(int n, int a[], float x0);
/* 求一元多项式的值P(x0)。 /
/
数组a的元素a[i]为i次项的系数,i=0,1,…,n */

#include "allinclude.h"  //DO NOT edit this line
float Polynomial(int n, int a[], float x0){
    
    // Add your code here
    float ans = a[0],t = 1.0;
    int i = 1;
    while(i <= n){
   
      t *= x0;
      ans += (t * a[i]);
      i++;
    }
    return ans;
}

DC01PE11 求k阶裴波那契序列的第m项的值

已知k阶裴波那契序列的定义为
f(0)=0, f(1)=0, …, f(k-2)=0, f(k-1)=1;
f(n)=f(n-1)+f(n-2)+…+f(n-k), n=k,k+1,…
试编写求k阶裴波那契序列的第m项值的函数算法,k和m均以值调用的形式在函数参数表中出现。
要求实现下列函数:
Status Fibonacci(int k, int m, int &f);
/* 如果能求得k阶斐波那契序列的第m项的值f,则返回OK;/
/
否则(比如,参数k和m不合理)返回ERROR */

#include "allinclude.h"  //DO NOT edit this line
Status Fibonacci(int k, int m, int &f) {
    
    // Add your code here
    int *a;     
    int i=1; 
    if(k < 2 || m < 0) return ERROR;     
    if(m < k) {
   
      if(m == k-1) f = 1;
      else f = 0;
      return OK;
    }
    a = (int*)malloc((m + 1)*sizeof(int));
    for (int i = 0; i < k - 1; i++) a[i] = 0;
      /* code */
      a[i] = 0;
      i = k + 1;
      a[k - 1] = 1;
      a[k] = 1;
      while(i <= m){
   
        a[i] = 2*a[i - 1] - a[i - k -1];
        i++;
      }
    f = a[m];
    return OK;
}

DC01PE18 计算i!×2^i的值

试编写算法,计算i!×2^i的值并存入数组a[0…n-1]的第i-1个分量中 (i=1,2,…,n)。假设计算机中允许的整数最大值为MAXINT,则当对某个k(1≤k≤n)使k!×2^k>MAXINT时,应按出错处理。注意选择你认为较好的出错处理方法。
要求实现下列函数:
Status Series(int a[], int n);
/* 求i!*2^i序列的值并依次存入长度为n的数组a;若所有 /
/
值均不超过MAXINT,则返回OK,否则返回EOVERFLOW */

#include "allinclude.h"  //DO NOT edit this line
Status Series(int a[], int n) {
    
    // Add your code here
 int t;
 int s;
 if(0 == n){
   
  return ERROR;
 }
 for(int i=1; i<=n; ++i){
   
  t = 1; 
  s = 1;
  for(int j=1; j<=i; ++j){
   
   t*=j;
   s*=2;
  }
  a[i-1] = t * s;
  if(a[i-1] > MAXINT){
   
   return EOVERFLOW;
  }
 }
 return OK;
}

DC01PE49 由一维数组构建一个序列

试写一算法,由长度为n的一维数组a构建一个序列S。
要求实现下列函数:
Status CreateSequence(Sequence &S, int n, ElemType a);
/
由长度为n的一维数组a构建一个序列S,并返回OK。 /
/
若构建失败,则返回ERROR */
序列的定义为:
typedef struct {
ElemType *elem;
int length;
} Sequence;

#include "allinclude.h"  //DO NOT edit this line
Status CreateSequence(Sequence &S, int n, ElemType *a) {
    
   if(n<1){
   
     return ERROR;
   }else{
   
     S.elem = (ElemType*)malloc(n*sizeof(ElemType));
     S.elem[0] = a[0];
     for (int i = 0; i < n; i++)
       /* code */
       S.elem[i] = a[i];
       S.length = n;
     }
     return OK;
}

DC01PE61 构建一个值为x的结点

链表的结点和指针类型定义如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, LinkList;
试写一函数,构建一个值为x的结点。
要求实现下列函数:
LinkList MakeNode(ElemType x);
/
构建一个值为x的结点,并返回其指针。 /
/
若构建失败,则返回NULL。 */

#include "allinclude.h"  //DO NOT edit this line
LinkList MakeNode(ElemType x) {
    
    // Add your code here
  LNode*p;
  p=(LNode*)malloc(sizeof(LNode));
  if(p == NULL) return NULL;
  else 
  p->data = x;
  return p;
}

DC01PE63 构建长度为2且两个结点的值依次为x和y的链表

链表的结点和指针类型定义如下
typedef struct LNode {
ElemType data;
struct LNode next;
} LNode, LinkList;
试写一函数,构建长度为2且两个结点的值依次为x和y的链表。
要求实现下列函数:
LinkList CreateLinkList(ElemType x, ElemType y);
/
构建其两个结点的值依次为x和y的链表。
/
/* 若构建失败,则返回NULL。 */

#include "allinclude.h"  //DO NOT edit this line
LinkList CreateLinkList(ElemType x, ElemType y) {
    
    // Add your code here
  LNode*p;
  p = (LNode*)malloc(sizeof(LNode));
  if(NULL == p) return NULL;
  else{
   
    p->next = (LNode*)malloc(sizeof(LNode));
    if(NULL == p->next) return NULL;
    p->data = x;
    p->next->data = y;
    p->next->next = NULL;
  }
  return p;
}

DC01PE65 构建长度为2的升序链表

链表的结点和指针类型定义如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, LinkList;
试写一函数,构建长度为2的升序链表,两个结点的值
分别为x和y,但应小的在前,大的在后。
要求实现下列函数:
LinkList CreateOrdLList(ElemType x, ElemType y);
/
构建长度为2的升序链表。 /
/
若构建失败,则返回NULL。 */

#include "allinclude.h"  //DO NOT edit this line
LinkList CreateOrdLList(ElemType x, ElemType y) {
    
    // Add your code here
    LNode*p;
    p=(LNode*)malloc(sizeof(LNode));
    if(NULL == p) return NULL;
    else{
   
      p->next=(LNode*)malloc(sizeof(LNode));
      if(NULL == p->next) {
   
        return NULL;
      }else{
   
        p->data = (x < y)? x : y;
        p->next->data = (x > y)? x : y;
        p->next->next = NULL;
      }
      return p;
    }
}

第二章

DC02PE03 实现顺序栈的判空操作

试写一算法,实现顺序栈的判空操作
StackEmpty_Sq(SqStack S)。
要求实现下列函数:
Status StackEmpty_Sq(SqStack S);
/* 对顺序栈S判空。 /
/
若S是空栈,则返回TRUE;否则返回FALSE */
顺序栈的类型定义为:
typedef struct {
ElemType *elem; // 存储空间的基址
int top; // 栈顶元素的下一个位置,简称栈顶位标
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack; // 顺序栈

#include "allinclude.h"  //DO NOT edit this line
Status StackEmpty_Sq(SqStack S) {
    
    // Add your code here
  if(S.top == 0){
   
    return TRUE;
  }else{
   
    return FALSE;
  }
}

DC02PE05 实现顺序栈的取栈顶元素操作

试写一算法,实现顺序栈的取栈顶元素操作
GetTop_Sq(SqStack S, ElemType &e)。
要求实现下列函数:
Status GetTop_Sq(SqStack S, ElemType &e);
/* 取顺序栈S的栈顶元素到e,并返回OK; /
/
若失败,则返回ERROR。 */
顺序栈的类型定义为:
typedef struct {
ElemType *elem; // 存储空间的基址
int top; // 栈顶元素的下一个位置,简称栈顶位标
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack; // 顺序栈

#include "allinclude.h"  //DO NOT edit this line
Status GetTop_Sq(SqStack S, ElemType &e) {
    
    // Add your code here
    if(S.top == 0){
   
      return ERROR;
    }else{
   
      e = S.elem[S.top-1];
      return OK;
    }
}

DC02PE07 实现顺序栈的出栈操作

试写一算法,实现顺序栈的出栈操作
Pop_Sq(SqStack &S, ElemType &e)。
要求实现下列函数:
Status Pop_Sq(SqStack &S, ElemType &e);
/* 顺序栈S的栈顶元素出栈到e,并返回OK;/
/
若失败,则返回ERROR。 */
顺序栈的类型定义为:
typedef struct {
ElemType *elem; // 存储空间的基址
int top; // 栈顶元素的下一个位置,简称栈顶位标
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack; // 顺序栈

#include "allinclude.h"  //DO NOT edit this line
Status Pop_Sq(SqStack &S, ElemType &e) {
    
    // Add your code here
  if(S.top == 0){
   
      return ERROR;
    }else{
   
      e = S.elem[--S.top];
      return OK;
    }
}

DC02PE11 构建初始容量和扩容增量分别为size和inc的空顺序栈S

若顺序栈的类型重新定义如下。试编写算法,构建初始容量和扩容增量分别为size和inc的空顺序栈S。
typedef struct {
ElemType elem; // 存储空间的基址
ElemType top; // 栈顶元素的下一个位置
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack2;
要求实现下列函数:
Status InitStack_Sq2(SqStack2 &S, int size, int inc);
/
构建初始容量和扩容增量分别为size和inc的空顺序栈S。
/
/* 若成功,则返回OK;否则返回ERROR。 */

#include "allinclude.h"  //DO NOT edit this line
Status InitStack_Sq2(SqStack2 &S, int size, int inc) {
    
    // Add your code here
  S.elem = (ElemType*)malloc(size*sizeof(ElemType));
  if(NULL == S.elem||size <= 0 || inc <=0) return ERROR;
    else{
   
      S.top = S.elem;
      S.size = size;
      S.increment = inc;
       return OK;
    }
}

DC02PE13 实现顺序栈的判空操作

若顺序栈的类型重新定义如下。试编写算法,
实现顺序栈的判空操作。
typedef struct {
ElemType *elem; // 存储空间的基址
ElemType top; // 栈顶元素的下一个位置
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack2;
要求实现下列函数:
Status StackEmpty_Sq2(SqStack2 S);
/
对顺序栈S判空。 /
/
若S是空栈,则返回TRUE;否则返回FALSE */

#include "allinclude.h"  //DO NOT edit this line
Status StackEmpty_Sq2(SqStack2 S) {
    
    // Add your code here
    if(S.top == S.elem){
   
      return TRUE;
    }else{
   
      return FALSE;
    }
}

DC02PE15 实现顺序栈的入栈操作

若顺序栈的类型重新定义如下。试编写算法,实现顺序栈的入栈操作。
typedef struct {
ElemType elem; // 存储空间的基址
ElemType top; // 栈顶元素的下一个位置
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack2;
要求实现下列函数:
Status Push_Sq2(SqStack2 &S, ElemType e);
/
若顺序栈S是满的,则扩容,若失败则返回ERROR。
/
/* 将e压入S,返回OK。 */

#include "allinclude.h"  //DO NOT edit this line
Status Push_Sq2(SqStack2 &S, ElemType e) {
    
    // Add your code here
    ElemType*newbase;
    if(*(S.top) >= S.size){
   
      newbase = (ElemType*)realloc(S.elem,(S.size+S.increment)*sizeof(ElemType));
      if(NULL == newbase) return ERROR;
      S.elem = newbase;
      S.size += S.increment;
    }
    *(S.top++) = e;
    return OK;
}

DC02PE17 实现顺序栈的出栈操作

若顺序栈的类型重新定义如下。试编写算法,实现顺序栈的出栈操作。
typedef struct {
ElemType elem; // 存储空间的基址
ElemType top; // 栈顶元素的下一个位置
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack2;
要求实现下列函数:
Status Pop_Sq2(SqStack2 &S, ElemType &e);
/
若顺序栈S是空的,则返回ERROR; /
/
否则将S的栈顶元素出栈到e,返回OK。
/

#include "allinclude.h"  //DO NOT edit this line
Status Pop_Sq2(SqStack2 &S, ElemType &e) {
    
    // Add your code here
  if(S.top == S.elem){
   
    return ERROR;
  }else{
   
    e = *(--S.top);
  }
  return OK;
}

DC02PE19 借助辅助栈,复制顺序栈S1得到S2

试写一算法,借助辅助栈,复制顺序栈S1得到S2。
Status CopyStack_Sq(SqStack S1, SqStack &S2)。
要求实现下列函数:
Status CopyStack_Sq(SqStack S1, SqStack &S2);
/* 借助辅助栈,复制顺序栈S1得到S2。 /
/
若复制成功,则返回TRUE;否则FALSE。 */
顺序栈的类型定义为:
typedef struct {
ElemType *elem; // 存储空间的基址
int top; // 栈顶元素的下一个位置,简称栈顶位标
int size; // 当前分配的存储容量
int increment; // 扩容时,增加的存储容量
} SqStack; // 顺序栈
可调用顺序栈接口中下列函数:
Status InitStack_Sq(SqStack &S, int size, int inc); // 初始化顺序栈S
Status DestroyStack_Sq(SqStack &S); // 销毁顺序栈S
Status StackEmpty_Sq(SqStack S); // 栈S判空,若空则返回TRUE,否则FALSE
Status Push_Sq(SqStack &S, ElemType e); // 将元素e压入栈S
Status Pop_Sq(SqStack &S, ElemType &e); // 栈S的栈顶元素出栈到e

#include "allinclude.h"  //DO NOT edit this line
Status CopyStack_Sq(SqStack S1, SqStack &S2) {
    
    // Add your code here
    if(StackEmpty_Sq(S1)){
   
      S2.top =0;
      return TRUE;
    }else{
   
      S2.elem = S1.elem;
      S2.top = S1.top;
      return TRUE;
    }
}

DC02PE23 求循环队列的长度

试编写算法,求循环队列的长度。
循环队列的类型定义为:
typedef struct {
ElemType base; // 存储空间的基址
int front; // 队头位标
int rear; // 队尾位标,指示队尾元素的下一位置
int maxSize; // 最大长度
} SqQueue;
要求实现下列函数:
int QueueLength_Sq(SqQueue Q);
/
返回队列Q中元素个数,即队列的长度。*/

#include "allinclude.h"  //DO NOT edit this line
int QueueLength_Sq(SqQueue Q) {
    
    // Add your code here
    return (Q.rear-Q.front+Q.maxSize)%Q.maxSize;
}

DC02PE25 编写入队列和出队列的算法

如果希望循环队列中的元素都能得到利用,
则可设置一个标志域tag,并以tag值为0或1来区分尾
指针和头指针值相同时的队列状态是"空"还是"满"。
试编写与此结构相应的入队列和出队列的算法。
本题的循环队列CTagQueue的类型定义如下:
typedef struct {
ElemType elem[MAXQSIZE];
int tag;
int front;
int rear;
} CTagQueue;
实现下列函数:
Status EnCQueue(CTagQueue &Q, ElemType x);
/* 将元素x加入队列Q,并返回OK;/
/
若失败,则返回ERROR。 /
Status DeCQueue(CTagQueue &Q, ElemType &x);
/
将队列Q的队头元素退队到x,并返回OK;/
/
若失败,则返回ERROR。 */

#include "allinclude.h"  //DO NOT edit this line
Status EnCQueue(CTagQueue &Q, ElemType x) {
    
    // Add your code here
    if(Q.front == Q.rear && Q.tag == 1)
      return ERROR;
      else{
   
        Q.elem[Q.rear] = x;
        Q.rear = (Q.rear+1)%MAXQSIZE;
      }
      if(Q.front==Q.rear)
      Q.tag=1;
      return OK;
}
Status DeCQueue(CTagQueue &Q, ElemType &x){
   
  if(Q.front == Q.rear && Q.tag == 0)
  return ERROR;
  else{
   
    x = Q.elem[Q.front];
    Q.front = (Q.front+1)%MAXQSIZE;
  }
  if(Q.front==Q.rear)
  Q.tag=0;
  return OK;
}

DC02PE27 写出循环队列的入队列和出队列的算法

假设将循环队列定义为:以域变量rear和length分别指示循环队列中队尾元素的位置和内含元素的个数。试给出此循环队列的队满条件,并写出相应的入队列和出队列的算法(在出队列的算法中要返回队头元素)。

本题的循环队列CLenQueue的类型定义如下:
typedef struct {
ElemType elem[MAXQSIZE];
int length;
int rear;
} CLenQueue;
实现下列函数:
Status EnCQueue(CLenQueue &Q, ElemType x);
/* 将元素x加入队列Q,并返回OK;/
/
若失败,则返回ERROR。 /
Status DeCQueue(CLenQueue &Q, ElemType &x);
/
将队列Q的队头元素退队到x,并返回OK;/
/
若失败,则返回ERROR。 */

#include "allinclude.h"  //DO NOT edit this line
Status EnCQueue(CLenQueue &Q, ElemType x) {
    
    // Add your code here
    if(Q.length==MAXQSIZE) return ERROR;
    Q.rear=(Q.rear+1)%MAXQSIZE;
    Q.elem[Q.rear]=x;
    Q.length++;
    return OK;
}

Status DeCQueue(CLenQueue &Q, ElemType &x){
   
    // Add your code here
    if(Q.length==0) return ERROR;
    x=Q.elem[(Q.rear+MAXQSIZE-Q.length+1)%MAXQSIZE];
    Q.length--;
    return OK;
}

DC02PE32 利用循环队列编写求k阶斐波那契序列中第n+1项fn的算法

已知k阶斐波那契序列的定义为:
f0=0, f1=0, …, fk-2=0, fk-1=1;
fn=fn-1+fn-2+…+fn-k, n=k,k+1,…
试利用循环队列编写求k阶斐波那契序列中第n+1项fn的算法。

本题的循环队列的类型定义如下:
typedef struct {
ElemType base; // 存储空间的基址
int front; // 队头位标
int rear; // 队尾位标,指示队尾元素的下一位置
int maxSize; // 最大长度
} SqQueue;
要求实现下列函数:
long Fib(int k, int n);
/
求k阶斐波那契序列的第n+1项fn */

#include "allinclude.h"  //DO NOT edit this line
long Fib(int k, int n) {
    
    // Add your code here
    if(k<2||n<0) return ERROR;
    if(n<k-1)  return 0;
    else if(n==k-1)  return 1;
    else{
   
      SqQueue S;
      S.base=(ElemType*)malloc((n+1)*sizeof(ElemType));
      S.base[k-1]=1;
      int i,j,sum;
      for(i = 0; i < k-1; i++){
   
        S.base[i]=0;
      }
      for (i = k; i < n+1; i++) {
   
        /* code */
        sum=0;
        for (j = i-k; j < i ; j++) {
   
          /* code */
          sum+=S.base[j];
        }
          S.base[i]=sum;
        }
        return S.base[n];
      }
}

DC02PE33 试写一个比较A和B大小的算法

设A=(a1,…,am)和B=(b1,…,bn)均为有序顺序表,
A’和B’分别为A和B中除去最大共同前缀后的子表(例如,
A=(x,y,y,z,x,z),B=(x,y,y,z,y,x,x,z),则两者中最大
的共同前缀为(x,y,y,z), 在两表中除去最大共同前缀后
的子表分别为A’=(x,z)和B’=(y,x,x,z))。若A’=B’=空表,
则A=B;若A’=空表,而B’≠ 空表,或者两者均不为空表,
且A’的首元小于B’的首元,则A<B;否则A>B。试写一个比
较A和B大小的算法。(注意:在算法中,不要破坏原表A
和B,也不一定先求得A’和B’才进行比较)。
顺序表类型定义如下:
typedef struct {
ElemType elem;
int length;
int size;
int increment;
} SqList;
要求实现下列函数:
char Compare(SqList A, SqList B);
/
比较顺序表A和B, /
/
返回’<', 若A<B; /
/
‘=’, 若A=B; /
/
‘>’, 若A>B */

#include "allinclude.h"  //DO NOT edit this line
char Compare(SqList A, SqList B) {
   </
  • 102
    点赞
  • 375
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KahooLee

你的鼓励与支持是我最大的动力啵

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值