944,020 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2224
  • C++ RSS
Mar 29th, 2005
0

at the moment this is a stack of queue, i am trying to make it into first in first ou

Expand Post »
at the moment this is a stack of queue, i am trying to make it into first in first out, (FIFO) , can anyone help. thank you very much
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. const int STACK_SIZE = 6;
  5. const int NIL = -1;
  6. const int LAST_ELEMENT = STACK_SIZE - 1;
  7.  
  8. struct stackType
  9. {
  10. int tos;
  11. char data[STACK_SIZE];
  12. };
  13.  
  14.  
  15.  
  16. void create(stackType&);
  17. bool full(stackType);
  18. bool empty(stackType);
  19. void Push(char, stackType& );
  20. void Pop(char&,stackType& );
  21. void Top( char&, stackType);
  22.  
  23. void display(stackType);
  24.  
  25. void create(stackType& s)
  26. {
  27. s.tos = NIL;
  28. }
  29.  
  30. bool full(stackType s)
  31. {
  32. if(s.tos == LAST_ELEMENT)
  33. return true;
  34. else
  35. return false;
  36. }
  37.  
  38. bool empty(stackType s)
  39. {
  40. if (s.tos == NIL)
  41. return true;
  42. else
  43. return false;
  44. }
  45.  
  46. void Push(stackType& s, char achar)
  47. {
  48. s.tos = s.tos + 1;
  49. s.data[s.tos] = achar;
  50. }
  51.  
  52. void Pop(stackType& s, char& achar)
  53.  
  54. {
  55. achar = s.data[s.tos];
  56. s.tos = s.tos + 1;
  57. }
  58.  
  59. void display(stackType s)
  60. {
  61. int i;
  62. for (i = s.tos; i != NIL; i++)
  63. cout << s.data[i] << endl;
  64. }
  65.  
  66. void Top(stackType s, char& achar)
  67. {
  68. achar = s.data[s.tos];
  69. }
  70.  
  71. void main()
  72. {
  73. stackType aStack[3];
  74.  
  75. char command, letter;
  76. int use_stack = 0;
  77. system("cls");
  78.  
  79. for (int i = 0 ; i<3 ; i++) create(aStack[i]);
  80.  
  81. cout << "This program uses a stack implemented as an array\n";
  82. cout << "Maximum number of items = " << STACK_SIZE << endl;
  83.  
  84. do
  85. {
  86. cout << "\nMAKING USE OF STACK " << use_stack;
  87. cout << "\n[P]ush data, P[O]p data, [T]op show top data";
  88. cout << "\n[D]isplay stack, [S]witch stack or [Q]uit -> ";
  89. cin >> command;
  90. switch(command)
  91. {
  92. case 'p' :
  93. case 'P' : if(!full(aStack[use_stack]))
  94. {
  95. cout << "Enter data -> ";
  96. cin >> letter;
  97. Push(aStack[use_stack],letter);
  98. }
  99. else
  100. cout << "The stack is full data not pushed\n";
  101. break;
  102. case 'o' :
  103. case 'O' : if (!empty(aStack[use_stack]))
  104. {
  105. Pop(aStack[use_stack], letter);
  106. cout << letter << " has been popped\n";
  107. }
  108. else
  109. cout << "Stack empty cannot pop\n";
  110. break;
  111. case 't' :
  112. case 'T' : if (!empty(aStack[use_stack]))
  113. {
  114. Top(aStack[use_stack], letter);
  115. cout << letter << " is on top of the stack\n";
  116. cout << "\n";
  117. }
  118. else
  119. cout << "Stack empty cannot pop\n";
  120. break;
  121. case 's' :
  122. case 'S' : do
  123. {
  124. cout << "Which stack do you wish to use (0-2) ";
  125. cin >> use_stack;
  126. } while (use_stack < 0 || use_stack > 2);
  127. break;
  128. case 'd' :
  129. case 'D' : if (!empty(aStack[use_stack]))
  130. display(aStack[use_stack]);
  131. else
  132. cout << "stack empty no data to display\n";
  133. break;
  134. case 'q' :
  135. case 'Q' : cout << "Program terminated\n";
  136. break;
  137. default : cout << "Unknown command [" <<command << "] try again!\n";
  138. break;
  139. }
  140. } while (!(command == 'q' || command == 'Q'));
  141. }
Code tags added. -Narue
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mel2005 is offline Offline
24 posts
since Mar 2005
Mar 29th, 2005
0

Re: at the moment this is a stack of queue, i am trying to make it into first in first ou

i have read your post of c and c++ . this post increase my c and c++ foundamental knowledge and i am thankful to your resoruces and organisation.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aman_yadav11 is offline Offline
1 posts
since Feb 2005
Mar 29th, 2005
0

Re: at the moment this is a stack of queue, i am trying to make it into first in first ou

So you have a stack of queues and you want to convert it to a queue of queues?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 29th, 2005
0

Re: at the moment this is a stack of queue, i am trying to make it into first in first ou

yes, the right, please help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mel2005 is offline Offline
24 posts
since Mar 2005
Mar 30th, 2005
0

Re: at the moment this is a stack of queue, i am trying to make it into first in first ou

hello everyone, just to let you know, i don't need help on this thread, i've done it, took me all night. anywhere thanks, have you got any idea how i can remove this thread
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mel2005 is offline Offline
24 posts
since Mar 2005
Mar 30th, 2005
0

Re: at the moment this is a stack of queue, i am trying to make it into first in first ou

can you not post up the correct code? It may be of some help to others?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 31st, 2005
0

Re: at the moment this is a stack of queue, i am trying to make it into first in first ou

yes, so i can, i will most probably post it by tommorrow, and i will also post my final piece soon. all i need to do now it switch to different queues, and that the hard bit
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mel2005 is offline Offline
24 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: parsing date strings mm/dd/yy??
Next Thread in C++ Forum Timeline: how to test equality of 2 objects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC