求数据结构(C++版)上机实验题:将一条循环单链表逆转的程序

求数据结构(C++版)上机实验题:将一条循环单链表逆转的程序,第1张

#include <iostream>

#include <ctime>

//历樱扮定义链表节点结构

struct Node

{

public:

Node():_val(0),_next(NULL)

{

}

Node(int val):_val(val),_next(NULL)

{

}

Node(int val,Node* next):_val(val),_next(next)

{

}

~Node()

{

if (_next)

delete _next

}

public:

int _val

Node* _next

}

typedef Node* LinkNode

//创建一个链表

Node* CreateLink(int len,int MAX_BOUND = 100)

{

srand((unsigned int)time(NULL))

LinkNode head = new Node(-1)

LinkNode tmp = head

for (int i = 0i <len++i)

{

tmp = tmp->_next = new Node(rand() % MAX_BOUND)

}

tmp->_next = NULL

return head

}

//链表逆转

Node* LinkListReserve(LinkNode head)

{

LinkNode p = NULL//记录逆转节点的前一个节点;

LinkNode r = head//记录当前节点

LinkNode q = NULL//记录逆转节点的下一个节点;

while (r != NULL)

{

q = r->_next //保存下一个节点

r->_next = p //逆转

p = r//下一次遍历

r = q

}

return p

}

//打印链表颂缓数据

void Print(LinkNode head)

{

using namespace std

cout<<".......begin......"<<endl

while (head != NULL)

{

cout<<head->_val<<" "

head = head->_next

}

cout<<endl<<"肢灶.......end........"<<endl

}

int main(int argc,char* argv[])

{

Node* head = CreateLink(4)

Print(head)

Print(LinkListReserve(head))

return 0

}

}

Status MidTraverse(BiTree T)/)此处Elem Type 根据数据类型实际情况而定

struct BiTNode *lchild

printf("

printf("

T->\

PreTraverse(T->

printf("!(T=(BiTNode *)malloc(sizeof(BiTNode))))

exit(overflow),count)/lchild)data)

printf(".h>\

printf("lchild)&&(,depthright=0lchild)stdio

printf(":"n中序输出二叉树,count)?depthleft)

PreTraverse(T)按先序输入二叉树中节点的值

{

char ch):depthright)

CreateTree(T->,int &count)

{

if(T)

{

if((

}

Status PreTraverse(BiTree T)/告脊rchild)

printf("

}

/

}

Status PostTraverse(BiTree T)

{

if(T)

{

PostTraverse(T->

int count=0stdlib

CountLeaf(T

typedef struct BiTNode

{

ElemType data)lchild)先序输出差友辩

{

if(T)

{

printf("

}

return OK)!T->rchild))

count++%c"depthright

printf("rchild

Status CountLeaf(BiTree T

}

return OK.h>lchild)%c"n")):"

printf(",*BiTree统计叶子数

printf("

# include <#'n\\,depthleft=0n"

PostTraverse(T->**************统计叶子数虚缺********************/\n"

PreTraverse(T->\

depthright=Depth(T->

}

return OK以下为程序代码/%c"n\rchild)data)

printf("rchild),T->/

# define OK 1

# define ERROR -1

# define overflow -1

typedef int ElemType

}

return depthvaln先序输出二叉树!T->)

}

int main()

{

BiTree T)

PostTraverse(T)/此二叉树的深度:"\

}

return OK

return OKlchild),count)

printf("\,T->

typedef int Status

else

{

depthleft=Depth(T->

ch=getchar()

{

if(T)

{

MidTraverse(T->

CountLeaf(T->data))%d"lchild创建二叉树

printf(",count)/n"/:"

}

/,*rchild

else

{

if(此二叉树的叶子数为data=ch左右孩子指针

}BiTNoden"rchild)):"

CountLeaf(T->/

Status CreateTree(BiTree &T)/

printf("!T)

depthval=0n后序输出二叉树)

T=NULL

MidTraverse(T),T->

CreateTree(T)/

depthval=1+(depthleft>

CreateTree(T->rchild)

if(/

MidTraverse(T->

int Depth(BiTree T)

{

int depthval=0*************输出二叉树的深度*************/

}

return OK

if(ch=='

printf("\%d"n\,Depth(T)):",请楼主参考

# include <


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12511003.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存