队列的enqueue, dequeue操作...

#include #include using namespace std; typedef struct _NODE { int value; struct _NODE *next; _NODE(int value) : value(value), next(NULL) {}; }NODE, *PTRNODE; void enqueue(PTRNODE &head, PTRNODE &tail, int value) { if(head == NULL) { head = new NODE(value); tail = head; } else { PTRNODE newNode = new NODE(value); tail->next = newNode; tail = newNode; } } int dequeue(PTRNODE &head, PTRNODE &tail) { if(head == NULL) { tail = NULL; return -1; } PTRNODE temp = head; int res = temp->value; head = head->next; delete temp; return res; } void clear(PTRNODE &head, PTRNODE &tail) { if(head == NULL) { tail = NULL; return; } while(head != NULL) dequeue(head, tail); } void display(PTRNODE head) { if(head == NULL) { cout << "empty" << endl; return; } PTRNODE temp = head; while(temp != NULL) { cout << temp->value << "->"; temp = temp->next; } cout << endl; } int main(int argc, char *argv[]) { PTRNODE head = NULL, tail = NULL; for(int i=0; i<10; i++) enqueue(head, tail, i); cout << "init queue is: " << endl; display(head); cout << "please input a number to enqueue: "; int num = 0; cin >> num; enqueue(head, tail, num); cout <<"after enqueue: " << endl; display(head); cout << "after dequeue: "<< endl; dequeue(head, tail); display(head); clear(head, tail); cout << "after clear: " << endl; display(head); system("PAUSE"); return EXIT_SUCCESS; }  

你可能感兴趣的:(数据结构)