#include<iostream>
#include<algorithm>
#include<list>
#include<queue>
using namespace std;
typedef struct node{
int data;
struct node *next;
}node, *pnode;
class myQueue
{
private:
pnode base;
pnode top;
public:
myQueue()
{
this -> base = NULL;
this -> top = NULL;
}
~myQueue()
{
if ( this -> base != NULL )
{
pnode pTemp = base -> next;
delete pTemp;
base = pTemp;
}
}
void initialqueue();
void addValue( int value );
int deleteValue();
void travel();
int getNodeNumber();
};
#include<iostream> #include<algorithm> #include<list> #include<queue> #include"queuepractice.h" using namespace std; void myQueue::initialqueue() { int data; char answer; pnode pTail,pNew; do { cout << "please input your data:"; cin >> data; if ( base == NULL ) { base = new node; top = new node; base -> data = data; base -> next = NULL; top -> next = NULL; top = base; pTail = base; pTail -> next = NULL; } else { pTail = top; pNew = new node; pNew -> data = data; pNew -> next = NULL; pTail -> next = pNew; pTail = pTail -> next; top = pTail; } cout << "need continue?"; cin >> answer; } while ( answer == 'y' || answer == 'Y' ); } void myQueue::addValue( int value ) { if ( base == NULL ) { base = new node; top = new node; base -> data = value; base -> next = NULL; top -> next = NULL; top = base; } else { pnode pTemp = top; pnode pNew = new node; pNew -> next = NULL; pNew -> data = value; pTemp -> next = pNew; pTemp = pTemp -> next; top = pTemp; } } int myQueue::getNodeNumber() //计算有多少个数字在队列 { pnode pTemp = base; int number = 0; while ( pTemp -> next != NULL ) { number++; pTemp = pTemp -> next; } return number; } int myQueue::deleteValue() { if ( base == top ) { base = NULL; top = NULL; cout << "it is empty"; return 0; } else { pnode pTemp; pTemp = base; base = pTemp -> next; delete pTemp; return 0; } } void myQueue::travel() { pnode pTemp = this -> base; while ( pTemp != NULL ) { cout << pTemp -> data << " "; pTemp = pTemp -> next; } cout << endl; } int main() { myQueue queue; //queue.initialqueue(); for (int i = 0; i < 10; i++) { queue.addValue(i); } queue.travel(); queue.deleteValue(); queue.travel(); queue.addValue( 8 ); queue.travel(); queue.deleteValue(); queue.travel(); }
新人写代码,还望大牛指出错误