当前位置:文档之家› 双向循环链表-课程设计

双向循环链表-课程设计


10
txt
数据结构
doc
删除叶子节点
11
txt
数据结构
doc
结论
在本次课程设计学会了vector和 list的使用,设计题目通过老师课堂讲 解的图的vector和list来写书的邻接表 存储树的结构,其中,树中孩子节点 数直接运用图结构的出度计算,由于 首次运用 list结构,书写代码途中出现 错误理解在先序遍历用多重循环嵌套 导致一系列错误。后,经过大家一致 努力克服重重困难最终,得以完成题 目。
6
txt
数据结构
doc
先序遍历
7
txt
数据结构
doc
删除叶子节点
//判断节点是不是叶子节点
bool tree::Leaf(int node){
for(int p=1;p<=n;p++){
if(al[p].empty())
return true;
}
return false;
}
8
txt
数据结构
doc
12
txt
数据结构
doc
谢谢!
list<int>::iterator p;
cout<<root<<" ";
for(int i=0;i<=n;i++){
if(i == root){ for(p=al[i].begin();p!=al[i].end(); p++){
5
txt
数据结构
doc
先序遍历
PreOrder(*p); }
} } }
//正向输出链表
Node * p = head->next ;
while(p!= head){
cout<<p->data<<" " ;
p=p->next ;
}
}
2
txt
数据结构
doc
双向循环链表
void Add(int data){
//添加一个结点
Node * p = new Node(data) ;
删除叶子节点
tree& tree::DDelete(int node1,int node2){
if(Leaf(node1)){ if(node1 < 1 || node1 > n ||
node2 < 1 || node2 > n) return *this; list<int>::iterator p; p = find(al[node1].begin(),
al[node1].end(), node2);
9
ห้องสมุดไป่ตู้
txt
数据结构
doc
删除叶子节点
if (p==al[node1].end()) return *this;//没有找到跳出函数
if(al[node2].begin()==al[node2].end ())//是否为叶子节点
al[node1].erase(p); } return *this; }
课程设计题目
双向循环链表
采用邻接表存储结构,完成树的增加、删 除一条边,增加、删除一个结点,求树中 结点的个数、求树的高度、求结点的孩子 个数、实现树的先根、后根和层次遍历。
邻接表要使用STL提供的List和Vector等实现。
1
txt
数据结构
doc
双向循环链表
void PrintDList1(){
p->next = head ;
p->pre = head->pre ;
head->pre->next = p ;
head->pre = p ;
}
3
txt
数据结构
doc
原树及存储结构
存储结构
4
txt
数据结构
doc
先根遍历
//树的先根遍历
void tree::PreOrder(int root){
相关主题