两道基础题
反转链表
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
//单链表的结构
struct ListNode
{
int val;
ListNode *next;
// 初始化方法
ListNode() : val(0), next(NULL) {}
ListNode(int x) : val(x), next(NULL) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
// 从数组中创建链表,a表示数组,n表示数组的长度
ListNode* CreateList(int const* a, int n)
{
ListNode* l = (ListNode*)malloc(sizeof(ListNode));
ListNode* temp = l;
l->val = a[0];
for(int i = 1; i < n; ++i) {
ListNode* t = (ListNode*)malloc(sizeof(ListNode));
t->val = a[i];
temp->next = t;
temp = t;
}
temp->next = NULL;
return l;
}
// 打印链表中的元素
void DisplayList(ListNode* head)
{
while(head) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
// 清空链表
void DeleteList(ListNode* &head)
{
if (head != NULL)
{
DeleteList(head->next);
delete(head); //用new创建,就用delete销毁,用malloc创建,就用free销毁
head = NULL;
}
}
// 反转链表
ListNode* Reverse(ListNode* head)
{
ListNode* s = NULL;
ListNode* cur = head;
while(cur) {
ListNode* t = cur->next;
cur->next = s;
s = cur;
cur = t;
}
return s;
}
int main()
{
int const a[] = {1,21,10000,0};
ListNode* head = CreateList(a, sizeof(a)/sizeof(a[0]));
cout<<"链表的初始状态为:";
DisplayList(head);
head = Reverse(head);
cout<<"链表的反转状态为:";
DisplayList(head);
DeleteList(head);
cout<<"链表已清空,head指针的值为"<<head<<endl;
return 0;
}
判断二叉树是否同构
#include <iostream>
using namespace std;
// 树的结构
typedef struct node
{
int data; //数据元素
struct node *lchild; //指向左孩子结点
struct node *rchild; //指向右孩子结点
// 初始化方法 .0000000000000000000000000000000000
node() : data(0), lchild(NULL), rchild(NULL) {}
node(int x) : data(x), lchild(NULL), rchild(NULL) {}
node(int x, node *lchild) : data(x), lchild(lchild), rchild(NULL) {}
node(int x, node *lchild, node *rchild) : data(x), lchild(lchild), rchild(rchild) {}
} BTNode; //二叉链结点类型
BTNode *CreateBTree(int a[],int b[],int n) //对应例2.8的算法
//由先序序列a[0..n-1]和中序序列b[0..n-1]建立二叉链
{
int k;
if (n<=0) return NULL;
int root=a[0]; //根结点值
BTNode* bt = new BTNode(root);
for (k=0;k<n;k++) //在b中查找b[k]=root的根结点
if (b[k]==root) break;
bt->lchild = CreateBTree(a + 1, b, k); //递归创建左子树
bt->rchild = CreateBTree(a + k + 1, b + k + 1, n - k - 1); //递归创建右子树
return bt;
}
void DisplayBTree(BTNode *bt) //采用括号表示输出二叉链bt
{
/*
样例:
1
/ \
2 3
/ \
4 5
输出:1(2(4),3(,5))
*/
if(bt!=NULL) {
cout << bt->data;
if(bt->lchild != NULL || bt->rchild != NULL) {
cout << "(";
DisplayBTree(bt->lchild);
if(bt->rchild != NULL) cout << ",";
DisplayBTree(bt->rchild);
cout << ")";
}
}
}
void DestroyBTree(BTNode *&bt) //对应例2.9的算法
//释放以bt为根结点的二叉树
{ if (bt != NULL)
{
DestroyBTree(bt->lchild);
DestroyBTree(bt->rchild);
delete(bt);
bt = NULL;
}
}
bool Isomorphism(BTNode* bt1, BTNode* bt2)
{
if((bt1 == NULL) && (bt2 == NULL)) {
return 1;
}
if(bt1 == NULL && bt2 != NULL || bt1 != NULL && bt2 == NULL) {
return 0;
}
return Isomorphism(bt1->lchild, bt2->lchild) && Isomorphism(bt1->rchild,bt2->rchild);
}
int main()
{
// 首先创建三棵树
BTNode *bt1, *bt2, *bt3;
int a[] = {5, 2, 3, 4, 1, 6};
int b[] = {2, 3, 5, 1, 4, 6};
bt1 = CreateBTree(a, b, sizeof(a)/sizeof(a[0]));
int c[] = {2, 5, 1, 4, 3, 6};
int d[] = {5, 1, 2, 3, 4, 6};
bt2 = CreateBTree(c, d, sizeof(c)/sizeof(c[0]));
int e[] = {4, 1, 2, 6, 3, 5};
int f[] = {2, 1, 4, 3, 6, 5};
bt3 = CreateBTree(e, f, sizeof(e)/sizeof(e[0]));
cout<<"二叉树bt1:"; DisplayBTree(bt1); cout<<endl;
cout<<"二叉树bt2:"; DisplayBTree(bt2); cout<<endl;
cout<<"二叉树bt3:"; DisplayBTree(bt3); cout<<endl;
cout<<"二叉树bt1和bt2"<<(Isomorphism(bt1, bt2)?"同构":"不同构")<<endl;
cout<<"二叉树bt1和bt3"<<(Isomorphism(bt1, bt3)?"同构":"不同构")<<endl;
DestroyBTree(bt1);
DestroyBTree(bt2);
DestroyBTree(bt3);
cout<<"二叉树已销毁,bt1,bt2,bt3的指针的值分别为"<<bt1<<' '<<bt2<<' '<<bt3<<endl;
return 0;
}
作者:LightAc
出处:https://www.cnblogs.com/lightac/
联系:
Email: dzz@stu.ouc.edu.cn
QQ: 1171613053
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。