data structure test
1、设计算法,对带头结点的单链表实现就地逆置。并给出单链表的存储结构(数据类型)的定义。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std; typedef char ElemType; typedef struct Node{
ElemType data;
struct Node *next;
}Node, *LinkList; LinkList CreateList()
{
LinkList L;
ElemType c;
L = (LinkList)malloc(sizeof(Node));
L->next = NULL;
Node *p , *tail;
tail = L;
c = getchar();
while(c != '#')
{
p = (Node *)malloc(sizeof(Node));
p->data = c;
tail->next = p;
tail = p;
c = getchar();
}
tail->next = NULL;
return L;
} void ShowList(LinkList L)
{
Node *p;
p = L->next;
while(p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
} void ReverseList(LinkList L)
{
Node *p, *q;
p = L->next;
L->next = NULL;
while(p != NULL)
{
q = p->next;
p->next = L->next;
L->next = p;
p = q;
} } int main()
{
LinkList L;
L = CreateList();
ShowList(L); ReverseList(L);
ShowList(L);
return 0;
}
2、编写递归算法,将二叉树中所有结点的左、右子树相互交换。并给出算法中使用的二叉树的存储结构(数据类型)的定义。
typedef struct BiNode
{
char data;
struct BiNode *left;
struct BiNode *right;
}BiNode, *BiTree;
BiNode* Exchange(BiNode* T)
{
BiNode* p;
if(NULL==T || (NULL==T->lchild && NULL==T->rchild))
return T;
p = T->lchild;
T->lchild = T->rchild;
T->rchild = p;
if(T->lchild)
{
T->lchild = Exchange(T->lchild);
}
if(T->rchild)
{
T->rchild = Exchange(T->rchild);
}
return T;
}
3、折半查找算法。
#include<iostream>
#include<cstdio>
using namespace std;
int search(int *array, int n, int target)
{
int low = 0;
int high = n - 1;
if( low > high ) return -1;
while( low <= high )
{
int mid = low + ( high - low ) / 2;
if( array[mid] < target ) low = mid + 1;
else if( array[mid] > target ) high = mid - 1;
else return mid;
}
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9};
cout<<search(a,9,4)<<endl;
return 0;
}
4、数据结构课本P74,习题2、13
void DeleteNode( ListNode *s)
{//删除单循环链表中指定结点的直接前趋结点
ListNode *p, *q;
p=s;
while( p->next->next!=s)
p=p->next;
//删除结点
q=p->next;
p->next=q->next;
free(p); //释放空间
}
5、统计叶子结点数。P168
#include<iostream>
#include<queue>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef struct BiNode
{
char data;
struct BiNode *left;
struct BiNode *right;
}BiNode, *BiTree;
int sum = 0;
void CreateBinaryTree(BiTree &T)//二叉树建立 abc,,de,g,,f,,,
{
//T = (BiNode*) malloc (sizeof(BiNode));
T = new BiNode;
cin >> T->data;
if(T->data == ',')
{
T = NULL;
}
if(T != NULL)
{
CreateBinaryTree(T->left);
CreateBinaryTree(T->right);
}
}
void PreOrder(BiTree T)//前序遍历
{
if(T != NULL)
{
cout << T->data;
PreOrder(T->left);
PreOrder(T->right);
}
}
void InOrder(BiTree T)//中序遍历
{
if(T != NULL)
{
InOrder(T->left);
cout << T->data;
InOrder(T->right);
}
}
void PostOrder(BiTree T)//后序遍历
{
if(T != NULL)
{
PostOrder(T->left);
PostOrder(T->right);
cout << T->data;
}
}
void LevOrder(BiTree T)//层次遍历
{
if(T != NULL)
{
BiTree p = T;
queue<BiTree>que;
que.push(p);
while(!que.empty())
{
p = que.front();
cout << p->data;
que.pop();
if(p->left != NULL)
{
que.push(p->left);
}
if(p->right != NULL)
{
que.push(p->right);
}
}
}
}
int Size(BiTree T)//计算二叉树节点数
{
if(T != NULL)
{
if(T->left == NULL && T->right == NULL)
{
sum++;
}
Size(T->left);
Size(T->right);
}
return sum;
}
int Deep(BiTree T)//计算二叉树深度
{
int m, n;
if(T == NULL) return 0;
m = Deep(T->left);
n = Deep(T->right);
if(m > n) return m + 1;
else return n + 1;
}
int main(void)
{
BiTree T;
CreateBinaryTree(T); cout << "前序遍历结果为:" << endl;
PreOrder(T);
cout << endl << endl; cout << "中序遍历结果为:" << endl;
InOrder(T);
cout << endl << endl; cout << "后序遍历结果为:" << endl;
PostOrder(T);
cout << endl << endl; cout<<"层次遍历结果为:"<<endl;
LevOrder(T);
cout << endl << endl; cout << "二叉树叶节点个数为:" << Size(T)<<endl;
cout << "二叉树深度数为:" << Deep(T) << endl;
system("pause");
return 0;
}
6、顺序表的合并。
#define MAXSIZE 100
typedef int ElemType; typedef struct SeqList
{
ElemType elem[MAXSIZE];
int last;
}SeqList;
void mergeList(SeqList *LA, SeqList * LB, SeqList *LC)
{
int i, j, k;
i = j = k = 0;
while (i <= LA->last && j <= LB->last)
{
if (LA->elem[i] <= LB->elem[j])
{
LC->elem[k++] = LA->elem[i++];
}
else
{
LC->elem[k++] = LB->elem[i++];
}
}
while (i <= LA->last)
{
LC->elem[k++] = LA->elem[i++];
}
while (j <= LB->last)
{
LC->elem[k++] = LB->elem[j++];
}
LC->last = LA->last + LB->last + 1;
}
data structure test的更多相关文章
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- Finger Trees: A Simple General-purpose Data Structure
http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
随机推荐
- 【Python3爬虫】反反爬之解决前端反调试问题
一.前言 在我们爬取某些网站的时候,会想要打开 DevTools 查看元素或者抓包分析,但按下 F12 的时候,却出现了下面这一幕: 此时网页暂停加载,也就没法运行代码了,直接中断掉了,难道这就能阻止 ...
- 啊哈!C语言课后参考答案下
最近看到一本好评量很高的的C语言入门书,课本真的很好,入门的话.专业性没有那么强,但入门足够了!!好评!看着看着就想把这本书的题课后习题都写出来,最后就有了这个小结.可能有的不是最好,不那么专业,但主 ...
- Notepad++中安装json格式化插件
在线工具固然好,一旦没网就凉凉 Notepad++编辑器中提供了 json 数据格式化显示的插件 安装插件过程如下: 注意: 安装过程需要联网状态 插件安装过程会自动退出程序,等待几秒钟后插件安装完成 ...
- iOS - 点击背景视图收起系统键盘
我们在 IOS 开发中经常会需要在输入框输入数据后,需要收起系统键盘,比如由于手机屏幕不是很大,可能由于输入信息后,系统键盘就会遮挡住下一步的按钮,而系统键盘有没有收起键,所以我们可以实现点击背景视图 ...
- 到底如何配置 maven 编译插件的 JDK 版本
千言万语不及官方文档,详情请阅读 compiler:compile 文档 配置 maven 编译插件的 JDK 版本 maven 编译插件(maven-compiler-plugin)有默认编译 JD ...
- 【Java基础总结】泛型
泛型实现了参数化类型的概念,使代码可以应用于多种类型. 1. 泛型类 声明的泛型类型静态方法不能使用 class Tools<T>{ private T t; public void se ...
- 【转】推荐给初级Java程序员的3本进阶书
ImportNew 注: 原作者在这篇文章中介绍3本不错的技术书籍.作者认为这些书籍对新手或者学生而言尤其有帮助.通过一些基础性的教程入门后,我们可以使用Java做基础性的编程.然而,当我们需要从初级 ...
- 引用dll出现的问题:发生一个或多个错误,引用无效或不支持该引用
获取到新的项目后,然后FineUI就出现黄色的标志,肯定是不可以用的,需要重新引用下. 然后我就开始重新引用下,就出现下面的问题: 因为是购买的UI,一开始我怀疑是引用的版本不一样呢,其实都不是 只需 ...
- Iaas/paas/saas 三种模式分别都是做什么?
任何一个在互联网上提供其服务的公司都可以叫做云计算公司.其实云计算分几层的,分别是Infrastructure(基础设施)-as-a- Service,Platform(平台)-as-a-Servic ...
- Pycharm 中的翻译工具
对于开发来说,大多数哥们英文欠缺,比如在下,我们大多数使用的开发工具是IDEA,IDEA 很强大,开发起来顺手. 废话不多说,让我们看一下如何使用翻译器. 打开Pycharm 的setting 设置, ...