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 ...
随机推荐
- jenkins+ant+jmeter自动化环境搭建
jmeter:测试接口的工具,支持java语言: ant:Apache Ant是一个Java库和命令行工具,其任务是将构建文件中描述的进程作为相互依赖的目标和扩展点.只要使用过Linux系统的读者,应 ...
- 1076 Wifi密码 (15 分)C语言
下面是微博上流传的一张照片:"各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答 ...
- C Primer Plus(二)
重读C Primer Plus ,查漏补缺 重读C Primer Plus,记录遗漏的.未掌握的.不清楚的知识点 分支和跳转 1.ctype.h头文件里包含了一些列用于字符判断的函数,包括判断数字.大 ...
- IOS系统唤醒微信内置地图
针对前一篇文章 唤醒微信内置地图 后来发现在IOS系统中运行 唤醒地图会无效的问题.因为在IOS上无法解析这俩个字符串的问题! 需要对经纬度 使用 “parseFloat()”进行转换 返回一个浮点数 ...
- 微信公众号 唤醒手机导航APP 一看就懂 复制即用
公司自研发框架,基本上没啥看不懂的 基本都是直接复制用就好了!希望能帮助到需要的朋友! 新建俩个同级文件用来保存 jsapi_ticket 和 access_token的文件 命名:jsapi_tic ...
- MongoDB 上手开发实践(入门上手开发这一篇就够了)
前言 MongoDB是一个介于 关系数据库 和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.它支持的数据结构非常松散,是类似 json 的 bson 格式,因此可以存储比较复 ...
- bash的默认组合键
组合键 组合按键 执行结果 Ctrl+C 终止目前的命令 Ctrl+D 输入结束(EOF),例如邮件结束的时候 Ctrl+M 就是Enter啦! Ctrl+S 暂停屏幕输出 Ctrl+Q 恢复屏幕输出 ...
- spring boot学习笔记(2)
Spring boot集成mybatis的三种方式 一.XML文件 在pom文件里面引入mybatis和数据库的依赖 在application.properties中加入数据源配置 其他和ssm配置完 ...
- js实现类选择器和name属性选择器
jQuery的出现,大大的提升了我们操作dom的效率,使得我们的开发更上一层楼,如jQuery的选择器就是一个很强大的功能,它包含了类选择器.id选择器.属性选择器.元素选择器.层级选择器.内容筛选选 ...
- 原生javascript实现二级延时菜单
一.实现原理: 使用定时器和排他思想完成 二.代码: <!DOCTYPE html> <html> <head> <title></title&g ...