湖南大学CG题库(数据结构与算法)题解

本文分享了湖南大学2022年学生在数据结构与算法课程中的经验,包括链表和二叉树的实现过程中遇到的问题、错误修正和深刻反思。作者通过实际操作演示了如何修复链表的print函数,并揭示了二叉树理解误区,强调了理论与实践结合的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:湖南大学2022数据结构与算法课程答案

实验一:预测线性表的实现

1、基于链表的线性表实现(题解)

题目描述:

在这里插入图片描述
在这里插入图片描述

题解:

#include<bits/stdc++.h>
using  namespace  std;

//断言工具函数:如果val为假则输出s然后中断程序
void  Assert(bool  val,string  s){
   
        if(!val){
   
                cout<<"断言失败:"<<s<<endl;
        }
}

template  <typename  E>  class  List  {
     //  List  ADT
private:
        void  operator  =(const  List&)  {
   }            //  Protect  assignment
        List(const  List&)  {
   }                      //  Protect  copy  constructor
public:
        List()  {
   }                    //  Default  constructor
        virtual  ~List()  {
   }  //  Base  destructor

        //  Clear  contents  from  the  list,  to  make  it  empty.
        virtual  void  clear()  =  0;

        //  Insert  an  element  at  the  current  location.
        //  item:  The  element  to  be  inserted
        virtual  void  insert(const  E&  item)  =  0;

        //  Append  an  element  at  the  end  of  the  list.
        //  item:  The  element  to  be  appended.
        virtual  void  append(const  E&  item)  =  0;

        //  Remove  and  return  the  current  element.
        //  Return:  the  element  that  was  removed.
        virtual  E  remove()  =  0;

        //  Set  the  current  position  to  the  start  of  the  list
        virtual  void  moveToStart()  =  0;

        //  Set  the  current  position  to  the  end  of  the  list
        virtual  void  moveToEnd()  =  0;

        //  Move  the  current  position  one  step  left.  No  change
        //  if  already  at  beginning.
        virtual  void  prev()  =  0;

        //  Move  the  current  position  one  step  right.  No  change
        //  if  already  at  end.
        virtual  void  next()  =  0;

        //  Return:  The  number  of  elements  in  the  list.
        virtual  int  length()  const  =  0;

        //  Return:  The  position  of  the  current  element.
        virtual  int  currPos()  const  =  0;

        //  Set  current  position.
        //  pos:  The  position  to  make  current.
        virtual  void  moveToPos(int  pos)  =  0;

        //  Return:  The  current  element.
        virtual  const  E&  getValue()  const  =  0;
};

//  Singly  linked  list  node
template  <typename  E>  class  Link  {
   
public:
        E  element;            //  Value  for  this  node
        Link  *next;                //  Pointer  to  next  node  in  list
        //  Constructors
        Link(const  E&  elemval,  Link*  nextval  =NULL)  {
   

element=elemval;
next=nextval;

        }
        Link(Link*  nextval  =NULL)  {
   

next=nextval;

        }
};

template  <typename  E>  class  LList:  public  List<E>  {
   
private:
        Link<E>*  head;              //  Pointer  to  list  header
        Link<E>*  tail;              //  Pointer  to  last  element
        Link<E>*  curr;              //  Access  to  current  element
        int  cnt;                              //  Size  of  list

        void  init()  {
                   //  Intialization  helper  method
        curr=tail=head=new Link<E>;
        cnt=0;
        }

        void  removeall()  {
         //  Return  link  nodes  to  free  store

while(head!=NULL){
   
curr=head;
head=head->next;
delete curr;
}

        }

public:
        LList(int  size=100)  {
   
                init();        //  Constructor
        }
        ~LList()  {
   
                removeall();        //  Destructor
        }

        //  使用空格分隔输出线性表中的所有数据,并最终换行
        //无数据时直接输出空行
void  print(){
   
        Link<E>* tmp=head->next;
        while(tmp!=NULL){
   
        cout<<tmp->element<<' ';
        tmp=tmp->next;
        }
        cout<<endl;
        }

        void  clear()  {
   
                removeall();        //  Clear  list
                init();
        }

        //  Insert  "it"  at  current  position
void  insert(const  E&  it)  {
   
        curr->next=new Link<E>(it,curr->next);
        if(tail==curr)tail=curr->next;

        cnt++
### 关于 HNU 数据结构课程中的凸包算法 在 HNU 的数据结构算法分析课程中,虽然未直接提及凸包算法的具体讲义名称或章节编号[^1],但从相关实验和作业的内容来看,可以推测凸包算法可能作为几何算法的一部分被讨论。通常情况下,凸包算法会涉及到计算平面上一组点的最小凸多边形,使得所有的点都在这个多边形内部或者边界上。 #### 凸包算法的基础概念 凸包是一种经典的几何算法问题,常见的解决方法有 Graham 扫描法、Jarvis 步进法以及分治法等。这些算法的核心在于通过特定策略逐步构建包围给定点集的凸多边形。以下是几种常见算法及其复杂度: - **Graham 扫描法**:时间复杂度为 \(O(n \log n)\),适用于大多数情况下的二维平面点集。 - **Jarvis 步进法(Gift Wrapping Algorithm)**:最坏情况下时间复杂度为 \(O(nh)\),其中 \(h\) 是最终凸包上的顶点数。 - **分治法**:时间复杂度同样为 \(O(n \log n)\),适合用于理解递归思想的应用场景。 #### 可能的教学资源位置 根据已知引用内容,HNU 的数据结构课程注重理论联系实际,提供了丰富的实验材料来帮助学生掌握核心知识点。尽管当前提供的引用并未明确提到凸包算法相关内容,但可以从以下几个方面寻找线索: - 如果存在针对几何问题的专题实验,则极有可能包含凸包算法的学习任务。 - 在某些综合性的算法设计题目中,可能会隐含对凸包求解的需求。 #### 示例代码展示 (Python 实现 Graham Scan 方法) 下面是一个简单的 Python 版本实现 Graham 扫描法的例子: ```python import sys from math import atan2 def polar_angle(p0, p1=None): if p1 is None: p1 = anchor y_span = p0[1]-p1[1] x_span = p0[0]-p1[0] return atan2(y_span,x_span) def distance(p0,p1=None): if p1==None: p1=anchor y_span=p0[1]-p1[1] x_span=p0[0]-p1[0] return y_span**2+x_span**2 def quicksort(a): if len(a)<=1: return a smaller,equal,larger=[],[],[] pivot_ang=polar_angle(a[randint(0,len(a)-1)]) for pt in a: ang=polar_angle(pt) if ang<pivot_ang: smaller.append(pt) elif ang==pivot_ang: equal.append(pt) else: larger.append(pt) return quicksort(smaller)+sorted(equal,key=distance)+quicksort(larger) points=[(random.randint(-50,50), random.randint(-50,50))for _ in range(random.randint(8,16))] anchor=min(points, key=lambda p:(p[1],p[0])) sorted_pts=quicksort(points) prev_point=anchor hull=[anchor] for s in sorted_pts: while len(hull)>=2 and cross_product_2D(hull[-2], hull[-1],s)<0: del hull[-1] hull.append(s) print("Convex Hull:", hull) ``` 此段代码展示了如何利用角度排序配合栈操作筛选出构成凸壳的关键节点集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

0<Solving)1

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值