C++ Queue program, enqueue and dequeue must be in given order

Write a class that implements queue. Queue should store string value. The value to be put on the queue are:
I
like
orange
an
better
than
an
sauce
apple.
Output must be: I like an orange better than an apple sauce.
I know how to get output in same order, can anybody help me to write code in required sequence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  #include <iostream>
#include <stdint.h>
#include <string>
#include <sstream>
using namespace std;

class node
{
	friend class list;
	private:
		string value;
		node *pnext;
	public:
		node() { pnext = NULL; };  // default constructor
		node( string v ) { value = v ; pnext = NULL; }; // constructor with value
		node( string v, node* next ) { value =v ; pnext = next; }; 
		string getValue() { return value; };
		node* getNext() { return pnext; };
		void setNext( node *ptr ) { pnext = ptr; };
};

class queue
{
	private:
		node *front;
		node *back;
	public:
		queue() { front = back = NULL;} ; 
		queue( string v ) { front = back = new node(v) ; } ; 
		~queue() {} ; 
		string dequeue()
		{
		    string v;
				if ( front == NULL ) // empty list
				{
					v = -1;
					return v;
				}
				v=back->getValue();// first in first out
				node *tmp = front;
				if ( front != back )
				{
while ( tmp != NULL && tmp->getNext() != NULL && tmp->getNext() != back )
{
		tmp = tmp->getNext();
                     
}
			  }
				else
                {
					tmp = NULL;
					front = NULL;
				}
				delete back;
				back = tmp;
				return v;
				};

				void enqueue(string v)
                {
                if (front==NULL)
                back= new node(v,front);
                front=new node (v, front);
                return;
                };

		node * getFront() { return front; };
		node * getBack() { return back; };
};

int main()
{
   //int i;
   string num;
   queue *que;
   que = new queue;


    que->enqueue("I");
    que->enqueue("like");
    que->enqueue("orange");
    que->enqueue("an");
    que->enqueue("better");
    que->enqueue("than");
    que->enqueue("an");
    que->enqueue("sauce");
    que->enqueue("apple");


   


   for(int i=0;i<10;i++)
    {
       cout << que->dequeue()  << endl; 

    }


   cout << endl;

   return( 0 );
}

Topic archived. No new replies allowed.