队列的链式存储结构与操作

来源:互联网 发布:c语言编程序 编辑:程序博客网 时间:2024/04/29 18:52

 #include <iostream>
using namespace std;


struct Infor
{
 char temp;
 struct Infor *next;
};

template<class T>
class Test
{
public:
 Test();

 void InterFace();
 void GetIntoQueue();
 void GetTopStack();
 void Exit();
 void DeleteTopStack();
 void CleanQueue();
 
protected:
private:
 struct Infor *m_pTop;
 struct Infor *m_pTail;
 int m_nSum;
};

template<class T>
Test<T>::Test()
{
 m_nSum = 0;
 m_pTail = new struct Infor;
 m_pTail->next = NULL;
 m_pTop  = m_pTail;
}

template<class T>
void Test<T>::Exit()
{
 exit(1);
}

template<class T>
void Test<T>::CleanQueue()
{
 struct Infor *p = m_pTop->next, *p2 = NULL;


  while (NULL != p)
  {
   p2 = p;
   p = p->next;
   delete p2;
  }

 

 m_pTail = m_pTop;
 m_pTop->next = NULL;
 InterFace();
}

template<class T>
void Test<T>::GetTopStack()
{
 if (NULL != m_pTop->next)
 {
  cout << m_pTop->next->temp << endl;
 }
 else
 {
  cout << "None" << endl;
 }
 InterFace();
}

template<class T>
void Test<T>::DeleteTopStack()
{
 struct Infor *p = m_pTop->next;

 if (NULL != p)
 {
  if (NULL != p->next)
  {
   cout << p->temp << endl;
   m_pTop->next = p->next;
  }
  else
  {
   cout << p->temp << endl;
   m_pTop->next = NULL;
  }

  delete p;
  m_nSum--;
 }
 else
 {
  cout << "None" << endl;
 }

 InterFace();

}

template<class T>
void Test<T>::GetIntoQueue()
{
// cout << "please input information" << endl;
 struct Infor *p = NULL;
 T temp;
 cin >>temp;

 p = new struct Infor;
 p->next = NULL;

 p->temp = temp;
 m_pTail->next = p;
 m_pTail = p;

 m_nSum++;

 InterFace();

}

template<class T>
void Test<T>::InterFace()
{
/* cout << "please input your server" << endl;
 cout << "Exit----------Q" << endl;
 cout << "Delete---------D" << endl;
 cout << "GetTopStack---G" << endl;
 cout << "clean all------C" << endl;
 cout << "getintoStack--E" << endl;*/
 char szTemp;
 cin >> szTemp;

 switch (szTemp)
 {
 case'C' | 'c':
  CleanQueue();
  break;
 case 'D' | 'd':
  DeleteTopStack();
  break;
 case 'Q' | 'q':
  Exit();
  break;
 case'E' | 'e':
  GetIntoQueue();
  break;
 case'G'|'g':
  GetTopStack();
  break;
 }
}

int main()
{
 Test<char> t1;
 t1.InterFace();
 return 0;
}

原创粉丝点击