Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- //step 1: always create node
- struct queuenode {
- string StudentID;
- queuenode * next;
- queuenode * back;
- } *front, *rear,*temp;
- //step 2: always declare the function header
- void enqueue(string sID);
- void dequeue();
- void display_from_front();
- void display_from_rear();
- //step 3: add the operations into the main()
- int main() {
- front = rear = NULL;
- enqueue("TP01");
- enqueue("TP02");
- enqueue("TP03");
- dequeue();
- enqueue("TP04");
- enqueue("TP05");
- dequeue();
- enqueue("TP02");
- cout << "From front to rear : \n"; display_from_front();
- cout << "From rear to front : \n"; display_from_rear();
- cout << endl;
- }
- //step 4: create the enqueue algorithm in the function
- void enqueue(string sID) {
- queuenode *n = new queuenode;
- n->StudentID = sID;
- n->next = NULL;
- n->back = NULL;
- if (front == NULL)
- {
- front = rear = n;
- }
- else {
- temp = rear;
- temp->next = n;
- n->back = temp;
- rear = n;
- }
- }
- //step 5: create the dequeue algorithm in the function
- void dequeue() {
- if (front == NULL)
- {
- return;
- }
- temp = front->next;
- if (temp == NULL) {
- front = rear = NULL;
- }
- front = temp;
- front->back = NULL;
- }
- //step 6: add the display from front algorithm in the function
- void display_from_front() {
- temp = front;
- while (temp != NULL) {
- cout << "student id: " << temp->StudentID << endl;
- temp = temp->next;
- }
- }
- //step 7: add the display from rear algorithm in the function
- void display_from_rear() {
- temp = rear;
- while (temp != NULL) {
- cout << "student id: " << temp->StudentID << endl;
- temp = temp->back;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement