#include
#include
#include
using namespace std;
struct TreeNode_t
{
int val;
struct TreeNode_t *left;
struct TreeNode_t *right;
TreeNode_t(int val):val(val),left(nullptr),
right(nullptr){
}
};
TreeNode_t *Reconstruct(vector<int> preOrder,vector<int> inOrder)
{
//寻找头结点
if(preOrder.size() == 0)
return nullptr;
int index = 0;
int gen = preOrder[0];
TreeNode_t *root = new TreeNode_t(gen);
for (int i = 0; i < preOrder.size(); i++)
{
if (gen == inOrder[i]){
index = i;
break;
}
}
vector<int> left_inOrder;
vector<int> left_preOrder;
vector<int> right_inOrder;
vector<int> right_preOrder;
for (int i = 0; i < index; i++)
{
left_preOrder.push_back(preOrder[i+1]);
left_inOrder.push_back(inOrder[i]);
}
for (int i = index + 1; i < preOrder.size(); i++)
{
right_preOrder.push_back(preOrder[i]);
right_inOrder.push_back(inOrder[i]);
}
root->left = Reconstruct(left_preOrder,left_inOrder);
root->right = Reconstruct(right_preOrder,right_inOrder);
return root;
}
void preOrderPrintf(TreeNode_t *root)
{
if (root == nullptr)
return;
printf("%d ",root->val);
if (root->left != nullptr)
preOrderPrintf(root->left);
if (root->right != nullptr)
preOrderPrintf(root->right);
}
void inOrderPrintf(TreeNode_t *root)
{
if (root == nullptr)
return;
if (root->left != nullptr)
inOrderPrintf(root->left);
printf("%d ",root->val);
if (root->right != nullptr)
inOrderPrintf(root->right);
}
//重建二叉树
int main(int argc,char **argv)
{
vector<int> pre = {
1,2,4,7,3,5,6,8};
vector<int> in = {
4,7,2,1,5,3,8,6};
TreeNode_t *root = Reconstruct(pre,in);
preOrderPrintf(root);
printf("\n");
inOrderPrintf(root);
printf("\n");
}
编译命令行g++ -o Reconstruct Reconstruct.cpp -std=c++11。
bool HasCycle(List*head)
{
if(!head)
return false;
List *fast = head;
List *slow = head;
while (fast != nullptr && slow != nullptr)
{
if(slow == fast)
return true;
slow = slow->next;
if(fast->next != nullptr)
fast = fast->next->next;
}
return false;
}
#include
#include
#include
#include
typedef struct msg_s
{
int num;
struct msg_s *next;
}msg_t;
msg_t *head = NULL;
msg_t *mp = NULL;
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *thread_producer(void *arg)
{
printf("producer start\n");
while (1)
{
printf("start produce\n");
mp = (msg_t*)malloc(sizeof(msg_t));
mp->num = rand()%1000;
pthread_mutex_lock(&mutex);
mp->next = head;
head = mp;
pthread_cond_signal(&has_product);
pthread_mutex_unlock(&mutex);
printf("finish produce\n");
sleep(5);
}
}
void *thread_comsumer(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (head == NULL)
{
printf("comsumer wait....\n");
pthread_cond_wait(&has_product,&mutex);
}
printf("comsumer start\n");
mp = head;
head = head->next;
pthread_mutex_unlock(&mutex);
free(mp);
mp = NULL;
sleep(1);
}
return NULL;
}
int main()
{
pthread_t pid,cid;
srand(time(NULL));
pthread_create(&pid,NULL,thread_producer,NULL);
pthread_create(&cid,NULL,thread_comsumer,NULL);
pthread_join(pid,NULL);
pthread_join(pid,NULL);
return 0;
}
参考链接:
10. https://www.cnblogs.com/ztteng/p/5147156.html。
11. https://blog.csdn.net/kongxian2007/article/details/49153801。
12. https://www.cnblogs.com/panchanggui/p/9288389.html。
13. https://blog.csdn.net/qq_38410730/article/details/81036768
14. https://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601204.html。