数据结构基础(代码)


tree.cpp
01#include <iostream>
02using namespace std;
03 
04typedef struct Node {           //二叉树的结构
05    intdata;                  //存储的数据
06    structNode *lchild;       //左孩子
07    structNode *rchild;       //右孩子
08}*BiTree;
09 
10void CreateTree(BiTree &T)      //创建一棵二叉树
11{
12    inti;
13    T=NULL;                    //输入数据之前先让该节点置空
14    cin>>i;
15    if(i!=-1)              //如果输入的数据不是结束符,则申请内存,创建二叉树或者子二叉树
16    {
17        T=newNode;
18        T->data=i;              //数据存储
19 
20        cout<<"请输入左孩子数据:"<<endl;
21        CreateTree(T->lchild);  //创建左孩子
22 
23        cout<<"请输入右孩子数据:"<<endl;
24        CreateTree(T->rchild);  //创建右孩子
25    }
26}
27 
28int Visit(BiTree &T)            //访问某一节点指针指向该节点的数据并输出
29{
30    cout<<T->data<<"   ";
31    returnT->data;
32}
33 
34void PreOrder(BiTree &T)        //先序遍历
35{
36    if(T==NULL)return;        //如果该节点为空,则停止继续访问
37    Visit(T);                  //访问并输出根节点的值
38    PreOrder(T->lchild);    //再访问左孩子
39    PreOrder(T->rchild);    //再访问右孩子
40}
41 
42void MidOrder(BiTree &T)        //中序遍历
43{
44    if(T==NULL)return;        //如果该节点为空,则停止继续访问  
45    MidOrder(T->lchild);    //再访问左孩子
46    Visit(T);                  //访问并输出根节点的值
47    MidOrder(T->rchild);    //再访问右孩子
48}
49 
50void PostOrder(BiTree &T)       //后序遍历
51{
52    if(T==NULL)return;        //如果该节点为空,则停止继续访问
53     
54    PostOrder(T->lchild);       //再访问左孩子
55    PostOrder(T->rchild);       //再访问右孩子
56    Visit(T);                  //访问并输出根节点的值
57}
58 
59void main()
60{
61    BiTree T;
62    T=NULL;
63    CreateTree(T);
64    cout<<"先序遍历的结果是:"<<endl;
65    PreOrder(T);
66    cout<<"中序遍历的结果是:"<<endl;
67    MidOrder(T);
68    cout<<"后序遍历的结果是:"<<endl;
69    PostOrder(T);
70}


vector.cpp

01#include <iostream>
02#include <vector>
03#include <bitset>
04#include <string>
05using namespace std;
06using std::vector;
07using std::bitset;
08/*void fun1()
09{
10    vector <string> str;
11    vector <string>::iterator ite;
12    string s;
13    string s1="\00";
14    while(true)
15    {
16        cin>>s;
17        if(strcmp(s,s1)==0)break;
18        str.push_back(s);
19    }
20     
21    for(ite=str.begin(); ite!=str.end();++ite)
22    {
23        cout<<*ite<<"   ";
24    }
25    cout<<endl;
26}*/
27 
28void fun2()
29{
30    vector<string> str(10);
31    vector<string>::iterator ite;
32    for(vector<string>::size_type id=0; id!=10; ++id)
33    {
34        cin>>str[id];
35    }
36    for(ite=str.begin(); ite!=str.end(); ++ite)
37    {
38        cout<<*ite<<"   ";
39    }
40    cout<<endl;
41}
42 
43void fun3()
44{
45    bitset<32> bt;
46    intid;
47    for(id=0;id!=32;++id)
48    {
49        cout<<bt[id]<<"   ";
50    }
51    cout<<endl;
52    for(id=0;id!=32;id+=2)
53    {
54        bt[id]=1;
55    }
56    for(id=0;id!=32;++id)
57    {
58        cout<<bt[id]<<"   ";
59    }
60    cout<<endl;
61    for(id=1; id!=33; id+=2)
62    {
63        bt[id]=1;
64    }
65    for(id=0;id!=32;++id)
66    {
67        cout<<bt[id]<<"   ";
68    }
69    cout<<endl;
70    unsignedlongp=bt.to_ulong();
71    cout<<"p = "<<p<<endl;
72    bt.flip();
73    for(id=0;id!=32;++id)
74    {
75        cout<<bt[id]<<"   ";
76    }
77    cout<<endl;
78}
79void main()
80{
81//  fun1();
82    fun2();
83    fun3();
84}


Thread.cpp

01#include <windows.h>
02#include <iostream.h>
03 
04DWORD WINAPI ThProc1(
05  LPVOIDlpParameter                  // thread data
06);
07 
08DWORD WINAPI ThProc2(
09  LPVOIDlpParameter                  // thread data
10);
11 
12int index = 0;
13int ticket = 10;
14HANDLE hMutex;
15 
16void main()
17{
18    HANDLEthread;
19    HANDLEthread2;
20 
21    thread= CreateThread(NULL,0,ThProc1,NULL,0,NULL);
22    thread2 = CreateThread(NULL,0,ThProc2,NULL,0,NULL);
23 
24    CloseHandle(thread);
25    CloseHandle(thread2);
26     
27    hMutex = CreateMutex(NULL,false,NULL);
28    Sleep(6000);
29/*  while(index++<1000)
30    cout<<"main thread is running"<<endl;*/
31//  while(ticket>0);
32 
33/*  WaitForSingleObject(hMutex,INFINITE);
34    ReleaseMutex(hMutex);
35    ReleaseMutex(hMutex);*/
36}
37DWORD WINAPI ThProc1(LPVOID lpParameter)
38{
39/*  while(index++<1000)
40        cout<<"ThProc1 thread is running"<<endl;*/
41    while(true) {
42        WaitForSingleObject(hMutex,INFINITE);
43        if(ticket>0)
44        {
45            Sleep(500);
46            cout<<"thread1 sell ticket:"<<ticket--<<endl;
47        }
48        else{
49            break;
50        }
51        ReleaseMutex(hMutex);
52    }
53    return0;                          //must have the return value
54}
55 
56DWORD WINAPI ThProc2(LPVOID lpParameter)
57{
58/*  while(index++<1000)
59        cout<<"ThProc1 thread is running"<<endl;*/
60 
61    while(true) {
62        WaitForSingleObject(hMutex,INFINITE);
63        if(ticket>0)
64        {
65            Sleep(500);
66            cout<<"thread2 sell ticket:"<<ticket--<<endl;
67        }
68        else{
69            break;
70        }
71        ReleaseMutex(hMutex);
72    }
73    return0;                          //must have the return value
74}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值