#include<iostream>
#include "stdlib.h"
#define _AFXDLL
#include "windows.h"
#include "process.h"
#define SelTest 2
using namespace std;
template<typename T>
class mylist
{
private:
struct node
{
T data;
node* m_next;
node()
{
m_next=NULL;
}
};
public:
mylist()
{
m_head =NULL;
m_isize =0;
}
private:
node* m_head;
int m_isize;
public:
void show()
{
int i=0;
node * ptemp= m_head;
if(ptemp==NULL)
return;
cout<<"链表显示开始";
while(ptemp->m_next !=NULL)
{
if(i%20==0)
cout<<endl;
i++;
std::cout<<ptemp->data<<" ";
ptemp=ptemp->m_next;
}
std::cout<<ptemp->data<<std::endl;
cout<<"链表显示结束"<<endl;
}
void push_back(T val )
{
node* pnode = new node;
pnode->data = val;
m_isize++;
if(!m_head)
{
m_head =pnode;
m_head->m_next=NULL;
}
else
{
node* pnodetemp = m_head ;
while(pnodetemp->m_next !=NULL)
{
pnodetemp= pnodetemp->m_next;
}
pnodetemp->m_next = pnode;
}
}
bool insertNode(int index ,T val)
{
node * ptemp= m_head;
if(index<=m_isize-1)
{
int ix=0;
node* pnode = new node;
pnode->data = val;
node* poldnode=ptemp;
while (index != ix++ && ptemp && ptemp->m_next)
{
poldnode=ptemp;
ptemp = ptemp->m_next;
}
if(index != 0)
{
poldnode->m_next =pnode;
pnode->m_next= ptemp;
}
else
{
pnode->m_next =m_head;
m_head = pnode;
}
m_isize++;
return true;
}
else
return false;
}
void ReversalList()
{
if(m_head==NULL || m_head->m_next==NULL )
return;
node* firistnode =m_head;
node* p= m_head;
node* q=p ? p->m_next:NULL;
node* m=q ? q->m_next:NULL;
while (q->m_next!=NULL)
{
q->m_next=p;
p=q;
q=m;
m=q->m_next;
}
if(q)
{
q->m_next=p;
m_head =q;
}
firistnode->m_next=NULL;
}
bool check()
{
if (m_head==NULL)
return false;
node * pfast = m_head->m_next;
node * pslow = m_head;
while ( pfast!=pslow && pfast->m_next!=NULL )
{
pfast=pfast->m_next;
pslow=pslow->m_next;
}
if(pfast->m_next!=NULL)
return true;
else
return false;
}
void pop()
{
if(m_head!=NULL)
{
node *ptem = m_head->m_next;
delete m_head;
m_head=ptem;
}
};
void foront(T& Val)
{
if(m_head!=NULL)
{
Val=m_head->data;
pop();
}
}
};
template<typename T>
class myCirclelist
{
private:
struct node
{
T data;
node* m_next;
node()
{
m_next=NULL;
}
};
public:
myCirclelist()
{
m_head =NULL;
m_isize =0;
}
private:
node* m_head;
int m_isize;
public:
void show()
{
int i=0;
node * ptemp= m_head;
if(ptemp==NULL)
return;
cout<<"环形链表显示开始";
while(ptemp->m_next !=m_head)
{
if(i%20==0)
cout<<endl;
i++;
std::cout<<ptemp->data<<" ";
ptemp=ptemp->m_next;
}
std::cout<<ptemp->data<<std::endl;
cout<<"环形链表显示结束"<<endl;
}
void push_back(T val )
{
node* pnode = new node;
pnode->data = val;
m_isize++;
if(!m_head)
{
m_head =pnode;
m_head->m_next=m_head;
}
else
{
node* pnodetemp = m_head ;
while(pnodetemp->m_next !=m_head)
{
pnodetemp= pnodetemp->m_next;
}
pnodetemp->m_next = pnode;
pnode->m_next=m_head;
}
}
bool insertNode(int index ,T val)
{
node * ptemp= m_head;
if(index<=m_isize-1)
{
int ix=0;
node* pnode = new node;
pnode->data = val;
node* poldnode=ptemp;
while (index != ix++ && ptemp && ptemp->m_next)
{
poldnode=ptemp;
ptemp = ptemp->m_next;
}
if(index != 0)
{
poldnode->m_next =pnode;
pnode->m_next= ptemp;
}
else
{
if(m_head==NULL)
return false;
node* ptem =m_head;
while(ptem->m_next != m_head)
{
ptem=ptem->m_next;
}
pnode->m_next =m_head;
m_head = pnode;
ptem->m_next= m_head;
}
m_isize++;
return true;
}
else
return false;
}
bool check()
{
if (m_head==NULL)
return false;
node * pfast = m_head->m_next;
node * pslow = m_head;
while ( pfast!=pslow && pfast->m_next!=NULL )
{
pfast=pfast->m_next;
pslow=pslow->m_next;
}
if(pfast->m_next!=NULL)
return true;
else
return false;
}
void pop()
{
if(m_head!=NULL)
{
node *ptem = m_head->m_next;
delete m_head;
if(ptem != m_head)
{
m_head=ptem;
}
else
m_head=NULL;
}
};
void foront(T& Val)
{
if(m_head!=NULL)
{
Val=m_head->data;
pop();
}
}
};
template<typename T>
class myCircleQueueByList
{
private:
struct node
{
T data;
node* m_next;
node()
{
m_next=NULL;
}
};
public:
myCircleQueueByList()
{
m_pTail=m_head =NULL;
m_isize =0;
}
private:
node* m_head;
node* m_pTail;
int m_isize;
public:
void show()
{
int i=0;
node * ptemp= m_head;
if(ptemp==NULL)
return;
cout<<"环形链式队列显示开始";
while(ptemp->m_next !=m_head)
{
if(i%20==0)
cout<<endl;
i++;
std::cout<<ptemp->data<<" ";
ptemp=ptemp->m_next;
}
std::cout<<ptemp->data<<std::endl;
cout<<"环形链式队列显示结束"<<endl;
}
void EnterQueue(T val )
{
push_back( val );
}
void dequeue(T& val)
{
//if(m_head != m_pTail)
foront(val);
}
private:
void push_back(T val )
{
node* pnode = new node;
pnode->data = val;
m_isize++;
if(!m_head)
{
m_head =pnode;
m_head->m_next=m_head;
m_pTail=m_head;
}
else
{
node* pnodetemp = m_head ;
while(pnodetemp->m_next !=m_head)
{
pnodetemp= pnodetemp->m_next;
}
pnodetemp->m_next = pnode;
pnode->m_next=m_head;
m_pTail=pnode;
}
}
bool insertNode(int index ,T val)
{
node * ptemp= m_head;
if(index<=m_isize-1)
{
int ix=0;
node* pnode = new node;
pnode->data = val;
node* poldnode=ptemp;
while (index != ix++ && ptemp && ptemp->m_next)
{
poldnode=ptemp;
ptemp = ptemp->m_next;
}
if(index != 0)
{
poldnode->m_next =pnode;
pnode->m_next= ptemp;
}
else
{
if(m_head==NULL)
return false;
node* ptem =m_head;
while(ptem->m_next != m_head)
{
ptem=ptem->m_next;
}
pnode->m_next =m_head;
m_head = pnode;
ptem->m_next= m_head;
}
m_isize++;
return true;
}
else
return false;
}
bool check()
{
if (m_head==NULL)
return false;
node * pfast = m_head->m_next;
node * pslow = m_head;
while ( pfast!=pslow && pfast->m_next!=NULL )
{
pfast=pfast->m_next;
pslow=pslow->m_next;
}
if(pfast->m_next!=NULL)
return true;
else
return false;
}
void pop()
{
if(m_head!=NULL)
{
node *ptem = m_head->m_next;
delete m_head;
if(ptem != m_head)
{
m_head=ptem;
}
else
{
m_head=NULL;
m_pTail=NULL;
}
if(m_pTail !=NULL)
m_pTail->m_next=m_head;
}
};
void foront(T& Val)
{
if(m_head!=NULL)
{
Val=m_head->data;
pop();
}
}
};
#define N 5
template<typename T>
class MyStack
{
public:
MyStack()
{
m_iCout=0;
}
void pushStack(T val)
{
if(m_iCout>=N)
return ;
m_arr[m_iCout++]=val;
}
bool popFromStack(T& val)
{
if(m_iCout>N-1)
return false;
val=m_arr[m_iCout--];
}
protected:
private:
int m_iCout;
T m_arr[N];
};
template<typename T>
class myQueueByList
{
private:
mylist<T> m_CircleQueueByList;
public:
void enqueue(T Val)
{
m_CircleQueueByList.push_back(Val);
}
void dequeue(T& Val)
{
m_CircleQueueByList.foront(Val);
}
void show()
{
m_CircleQueueByList.show();
}
};
template<typename T>
class myCircleQueue
{
public:
myCircleQueue()
{
m_itail=0;
m_ihead=0;
}
public:
bool enqueue(T val)
{
if((m_itail+1)%N == m_ihead)//queue over
return false;
m_arr[m_itail++]=val;
m_itail=m_itail%N;
return true;
}
bool outqueue(T& val)
{
if(m_itail==m_ihead)
return false;
val= m_arr[m_ihead++];
m_ihead=m_ihead%N;
return true;
}
void ShowQueue()
{
cout <<"显示队列开始"<<endl;
for (int i=0 ;i<abs(m_itail-m_ihead);i++ )
{
cout<<m_arr[(m_ihead+i)%N] ;
}
cout<<endl;
cout<<"显示队列结束"<<endl;
}
private:
int m_itail;
int m_ihead;
T m_arr[N];
};
class CCriticalSection
{
public:
CCriticalSection()
{
::InitializeCriticalSection(&_cs);
}
void Lock()
{
::EnterCriticalSection(&_cs);
}
void Unlock()
{
::LeaveCriticalSection(&_cs);
}
protected:
private:
CRITICAL_SECTION _cs;
};
#define NZ 100
template<typename T>
class myCircleQueueZ
{
public:
myCircleQueueZ()
{
m_itail=0;
m_ihead=0;
}
public:
bool enqueue(T val)
{
m_enterCritical.Lock();
if((m_itail+1)%NZ == m_ihead)//queue over
{
m_enterCritical.Unlock();
return false;
}
m_arr[m_itail++]=val;
m_enterCritical.Unlock();
return true;
}
bool outqueue(T& val)
{
m_outCritical.Lock();
if(m_itail==m_ihead)
{
m_outCritical.Unlock();
return false;
}
val= m_arr[m_ihead++];
m_outCritical.Unlock();
return true;
}
void ShowQueue()
{
cout <<"显示队列开始";
for (int i=0 ;i<abs(m_itail-m_ihead);i++ )
{
if(i%20 ==0) cout<<endl;
cout<<m_arr[(m_ihead+i)%NZ]<<" ";
}
cout<<endl;
cout<<"显示队列结束"<<endl;
}
private:
int m_itail;
int m_ihead;
T m_arr[NZ];
CCriticalSection m_enterCritical;
CCriticalSection m_outCritical;
};
myCircleQueueZ<int> cirZ;
void ProductThread1(void* p)
{
int istart=0;
int i=istart;
while(i<NZ/2)
{
cirZ.enqueue(i++);
for (int k=0;k<1900;k++)
{
;
}
}
int k=0;
}
void ProductThread2(void* p)
{
int istart=100;
int i=istart;
while(i-NZ<NZ/2)
{
cirZ.enqueue(i++);
for (int k=0;k<1900;k++)
{
;
}
}
}
void ProductThread3(void* p)
{
int istart=400;
int i=istart;
while(i-istart<NZ/2)
{
cirZ.enqueue(i++);
for (int k=0;k<1900;k++)
{
;
}
}
}
void main()
{
#pragma region 单链表
#if SelTest==1
mylist<int> mlist;
int index =1;
mlist.push_back(1);
mlist.push_back(2);
mlist.push_back(3);
mlist.push_back(4);
mlist.push_back(5);
mlist.show();
cout<<"****"<<endl;
mlist.insertNode(0,6);
mlist.show();
cout<<"****"<<endl;
mlist.insertNode(1,7);
mlist.show();
cout<<"****"<<endl;
mlist.insertNode(6,8);
mlist.show();
cout<<"****"<<endl;
mlist.push_back(1);
mlist.push_back(2);
mlist.push_back(3);
mlist.push_back(4);
mlist.push_back(5);
mlist.show();
cout<<"****"<<endl;
mlist.ReversalList();
mlist.show();
cout<<"****"<<endl;
cout<<"****"<<endl;
mylist<int> SL;
SL.push_back(0);
SL.push_back(3);
SL.push_back(5);
SL.show();
cout<<"****"<<endl;
SL.ReversalList();
SL.show();
#endif
#pragma endregion 单链表
#pragma region 环形队列
#if SelTest==2
myCircleQueue<int> cir;
cir.enqueue(1);
cir.enqueue(2);
cir.enqueue(3);
cir.enqueue(4);
cir.enqueue(5);
cir.ShowQueue();
int a;
cir.outqueue(a);
cout<<"a:"<<a<<endl;
cir.ShowQueue();
cir.outqueue(a);
cout<<"a:"<<a<<endl;
cir.ShowQueue();
cir.outqueue(a);
cout<<"a:"<<a<<endl;
cir.ShowQueue();
cir.outqueue(a);
cout<<"a:"<<a<<endl;
cir.ShowQueue();
#endif
#pragma endregion 环形队列
#pragma region 并发队列
#if SelTest==3
HANDLE H[3];
H[0]=(HANDLE)_beginthread(ProductThread1,0,NULL);
H[1]=(HANDLE)_beginthread(ProductThread2,0,NULL);
H[2]=(HANDLE)_beginthread(ProductThread3,0,NULL);
WaitForMultipleObjects(3,H,true,10000);
cirZ.ShowQueue();
#endif
#pragma endregion 并发队列
#pragma region 链式队列
#if SelTest==4
myQueueByList<int> m_s;
for (int k=0;k<100;k++)
{
m_s.enqueue(k);
}
m_s.show();
int af;
for (int j=0;j<200;j++)
{
m_s.dequeue(af);
cout<< "af:"<<af <<" ";
}
m_s.show();
#endif
#pragma endregion 链式队列
#pragma region 环形链表
#if SelTest==5
myCirclelist<int> Circlelist;
for (int i=0;i< 4;i++)
{
Circlelist.push_back(i);
}
Circlelist.show();
Circlelist.insertNode(0,100);
Circlelist.insertNode(4,101);
Circlelist.show();
#endif
#pragma endregion 环形链表
#pragma region 链式环形队列
#if SelTest==6
myCircleQueueByList<int> qlist;
for (int i=0; i<4 ;i++)
{
qlist.EnterQueue(i);
}
qlist.show();
int as;
qlist.dequeue(as);
cout<<"as:" << as<< " ";
qlist.dequeue(as);
cout<<"as:" << as<< " ";
qlist.dequeue(as);
cout<<"as:" << as<< " ";
qlist.dequeue(as);
cout<<"as:" << as<< " ";
qlist.dequeue(as);
cout<<"as:" << as<< " "<<endl;
qlist.show();
for (int i=0; i<40 ;i++)
{
qlist.EnterQueue(i);
}
qlist.show();
#endif
#pragma endregion 链式环形队列
system("pause");
}
链表 环形链表 队列 环形队列 栈
猜你喜欢
转载自blog.csdn.net/wuan584974722/article/details/83005285
今日推荐
周排行