Link List with an embedded stack C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Link List with an embedded stack C++

 
0
  #1
Sep 26th, 2008
I was trying to figure this out but for some reason it is illuding me

Ministack header provided
  1.  
  2. #ifndef MINISTACK_H
  3.  
  4. #define MINISTACK_H
  5.  
  6. const int MINI_STACK_SIZE = 5; // Fixed number of element in stack
  7.  
  8. class MiniStack
  9. {
  10. private:
  11. int num; // Number of values stored in MiniStack
  12. char* stackPtr; // Pointer to array representing stack
  13.  
  14. public:
  15. MiniStack(); // Default constructor
  16. void Push(char ch); // Adds element to top of stack assuming stack not full
  17. void Pop(); // Removes element from top of stack
  18. void MakeEmpty(); // Empties ministack
  19. char Top(); // Returns copy of value stored at top of stack
  20. bool IsFull() const; // Returns true if ministack is full; false otherwise
  21. bool IsEmpty() const; // Returns true if ministack empty; false otherwise
  22. void Print() const; // Prints stack contents, top to bottom
  23. ~MiniStack(); // Destructor
  24. };
  25.  
  26. #endif


Big stack header

  1.  
  2. #ifndef BIGSTACK_H
  3.  
  4. #define BIGSTACK_H
  5.  
  6. #include "ministack.h"
  7.  
  8. struct ListNode // Description of a ListNode struct
  9. {
  10. MiniStack* stackPtr; // Pointer to a MiniStack object
  11. ListNode* nextPtr; // Pointer to next ListNode
  12. };
  13.  
  14.  
  15. class BigStack // Description of BigStack class
  16. {
  17. private:
  18. int num; // Total number of values stored in MiniStack
  19. ListNode* headPtr; // Pointer to head of list of nodes representing bigstack
  20.  
  21. public:
  22. BigStack(); // Default constructor
  23. void Push(char ch); // Adds element to top of stack assuming stack not full
  24. void Pop(); // Removes element from top of stack
  25. char Top(); // Returns copy of top value assuming stack not empty
  26. bool IsFull() const; // Returns true if ministack is full; false otherwise
  27. bool IsEmpty() const; // Returns true if ministack empty; false otherwise
  28. void Print() const; // Prints stack contents, top to bottom
  29. ~BigStack(); // Destructor
  30. };
  31.  
  32. #endif

ministack implemintation I wrote this code here.

  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6. #include <cmath>
  7. #include <new>
  8. #include <cstddef>
  9. #include "ministack.h"
  10. #include "bigstack.h"
  11.  
  12. using namespace std;
  13.  
  14. MiniStack::MiniStack() // Default constructor
  15. {
  16. stackPtr = new char[MINI_STACK_SIZE];
  17. num = 0;
  18. }
  19.  
  20. void MiniStack::Push(char ch) // Adds element to top of stack assuming stack not full
  21. {
  22.  
  23. if (num >=0 && num <=4)
  24. {
  25. stackPtr[num] = ch;
  26. num++;
  27. }
  28. else if(num < 0)
  29. {
  30. num = 0;
  31. stackPtr[num] = ch;
  32. num++;
  33. }
  34. else
  35. num = 5;
  36. }
  37.  
  38. void MiniStack::Pop() // Removes element from top of stack
  39. {
  40. num--;
  41. }
  42.  
  43. void MiniStack::MakeEmpty() // Empties ministack
  44. {
  45. num = -1;
  46. }
  47.  
  48. char MiniStack::Top() // Returns copy of value stored at top of stack
  49. {
  50. return stackPtr[num-1];
  51. }
  52.  
  53. bool MiniStack::IsFull() const // Returns true if ministack is full; false otherwise
  54. {
  55. if(num == MINI_STACK_SIZE-1)
  56. return true;
  57. else
  58. return false;
  59. }
  60.  
  61. bool MiniStack::IsEmpty() const // Returns true if ministack empty; false otherwise
  62. {
  63. if (num == 0)
  64. return true;
  65. else
  66. return false;
  67. }
  68.  
  69. void MiniStack::Print() const // Prints stack contents, top to bottom
  70. {
  71. int n;
  72. n = num - 1;
  73.  
  74. do
  75. {
  76. if (n <= -1)
  77. break;
  78. else
  79. {
  80. cout << stackPtr[n] << " ";
  81. n--;
  82. }
  83.  
  84. }while (n != -1);
  85. cout << endl;
  86. }
  87.  
  88. MiniStack::~MiniStack() // Destructor
  89. {
  90. do
  91. {
  92. num--;
  93. }while (num >= 0);
  94. }

  1. bigstack implemintation
  2. Once again I am having a problem with the push function.
  3. how in the world do I get these two to work together.
  4. I know that the headPtr points to the top of the link list and nextPtr points to the next node in the list and that the stackPtr points to the stack. But how would I put it all together. Do I do it in my main or in the implementation of the big stack. Please Please help I have been trying but I can not get it.

  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6. #include <cmath>
  7. #include <new>
  8. #include <cstddef>
  9. #include "ministack.h"
  10. #include "bigstack.h"
  11.  
  12. using namespace std;
  13.  
  14. BigStack::BigStack() // Default constructor
  15. {
  16. headPtr = NULL;
  17. num = 0;
  18. }
  19.  
  20. [COLOR="Red"]void BigStack::Push(char ch) // Adds element to top of stack assuming stack not full
  21. {
  22. if (num == 0)
  23. {
  24. ListNode* nextPtr = new ListNode;
  25. nextPtr->nextPtr = headPtr;
  26. headPtr = nextPtr;
  27. num++;
  28. }
  29. nextPtr->ch = ch;
  30. nextPtr->stackPtr = ch;
  31.  
  32. }[/COLOR]
  33. void BigStack::Pop() // Removes element from top of stack
  34. {
  35.  
  36. }
  37.  
  38. char BigStack::Top() // Returns copy of top value assuming stack not empty
  39. {
  40. }
  41.  
  42. bool BigStack::IsFull() const // Returns true if ministack is full; false otherwise
  43. {
  44. }
  45.  
  46. bool BigStack::IsEmpty() const // Returns true if ministack empty; false otherwise
  47. {
  48. }
  49.  
  50. void BigStack::Print() const // Prints stack contents, top to bottom
  51. {
  52.  
  53. }
  54.  
  55. BigStack::~BigStack() // Destructor
  56. {
  57. }
Last edited by JustLearning; Sep 26th, 2008 at 5:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Link List with an embedded stack C++

 
0
  #2
Sep 26th, 2008
>>I was trying to figure this out but for some reason it is illuding me

you will have to be a lot more specific than that.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC