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的更多相关文章

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. 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 ...

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  6. ✡ 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 - ...

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  8. 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 ...

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  10. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

随机推荐

  1. 毕业两年半,入手人生第一款macbook pro

    当程序员入手第一款macbook 大家好,我是灰大狼,你们可以叫我灰狼.大狼.甚至是小灰灰. 接下来我主要跟大家分享下作为程序员的我,刚入手一款mac的使用心得. 背景 做程序员三年了,一直用的都是w ...

  2. [Linux实践] macOS平台Homebrew更新brew update卡死,完美解决

    [Linux实践] macOS 平台 Homebrew 更新 brew update 卡死,完美解决 版本2020.01.05 摘要: 使用brew install [软件包]安装软件包时,卡在Upd ...

  3. 公子奇带你进入Java8流的世界(一)

    在说流之前,我们先来看看集合,为什么呢?作为Java8中的新成员,它和集合有很多相似之处,同时它们也是可以互相转化的.集合不仅仅是Java语言,任何一门高级开发语言都有集合的概念,集合顾名思义,就是很 ...

  4. 一次 kafka 消息堆积问题排查

    收到某业务组的小伙伴发来的反馈,具体问题如下: 项目中某 kafka 消息组消费特别慢,有时候在 kafka-manager 控制台看到有些消费者已被踢出消费组. 从服务端日志看到如下信息: 该消费组 ...

  5. Fabric1.4:Go 链码开发与编写

    1 链码结构 1.1 链码接口 链码启动必须通过调用 shim 包中的 Start 函数,传递一个类型为 Chaincode 的参数,该参数是一个接口类型,有两个重要的函数 Init 与 Invoke ...

  6. C#调用Matlab生成的Dll

    问题描述:最近开发需要调用matlab生成的DLL,在New MWNumericArray 对象的时候报错,提示未将对象引用到对象的实例. 问题分析:因为MWArray.dll是Matlab提供的DL ...

  7. Java爬虫一键爬取结果并保存为Excel

    Java爬虫一键爬取结果并保存为Excel 将爬取结果保存为一个Excel表格 官方没有给出导出Excel 的教程 这里我就发一个导出为Excel的教程 导包 因为个人爱好 我喜欢用Gradle所以这 ...

  8. 深入理解协程(三):async/await实现异步协程

    原创不易,转载请联系作者 深入理解协程分为三部分进行讲解: 协程的引入 yield from实现异步协程 async/await实现异步协程 本篇为深入理解协程系列文章的最后一篇. 从本篇你将了解到: ...

  9. Promise里的代码为什么比setTimeout先执行

    当浏览器或者Node拿到一段代码时首先做的就是传递给JavaScript引擎,并且要求它去执行. 然而,执行 JavaScript 并非一锤子买卖,宿主环境当遇到一些事件时,会继续把一段代码传递给 J ...

  10. Akka Java 文档 -- 容错

    [转自: http://blog.csdn.net/zjw10wei321/article/details/46911825] 容错 实际中的故障处理 容错案例图解 容错案例所有源码 创建新的监管策略 ...