Integer Stack Copy Function

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

Join Date: Jul 2006
Posts: 73
Reputation: SHWOO is an unknown quantity at this point 
Solved Threads: 0
SHWOO SHWOO is offline Offline
Junior Poster in Training

Integer Stack Copy Function

 
0
  #1
Jun 26th, 2007
I'm trying to implement a function to copy one instance of a stack into another instance of a stack.

The result should be two identical stacks. It only outputs blank lines. I

Implementation

  1.  
  2. #include <cassert>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #include "linkedStack.h"
  7.  
  8. using namespace std;
  9.  
  10. void linkedStack::initialize()
  11. {
  12. destroyStack();
  13. } //end initialize
  14.  
  15. void linkedStack::push(int newNumber)
  16. {
  17. node *newNode; //pointer to create the new node
  18.  
  19. newNode = new node; //creats a new node
  20. assert( newNode != NULL);
  21.  
  22. newNode->number = newNumber; //new integer stored in newNode
  23. newNode->link = stackTop; //isert newNode before stackTop
  24. stackTop = newNode; //set stackTop to point to the top node
  25. }
  26.  
  27. void linkedStack::pop()
  28. {
  29. node *temp;
  30.  
  31. if(stackTop != NULL) //if stack is not empty
  32. {
  33. temp = stackTop; //set temp to stackTop
  34. stackTop = stackTop->link; //advance stackTop to the next node
  35. delete temp; //delete temp node
  36. }
  37. else
  38. cerr << "Cannot remove from an empty stack." << endl;
  39. } //end pop
  40.  
  41. void linkedStack::printStack()
  42. {
  43. node *current;
  44. node *temp;
  45.  
  46. temp = stackTop;
  47. while(stackTop != NULL)
  48. {
  49. cout << stackTop->number << " "; //print letter on top of stack
  50.  
  51. current = stackTop->link; //set current to stackTops link
  52. stackTop = current; //set stackTop to current
  53.  
  54. }
  55.  
  56. stackTop = temp;
  57. cout << endl << endl;
  58. }
  59.  
  60. void linkedStack::destroyStack()
  61. {
  62. node *temp; //pointer to delete a node
  63.  
  64. while (stackTop != NULL)
  65. {
  66. temp = stackTop;
  67.  
  68. stackTop = stackTop->link;
  69.  
  70. delete temp; // delet temp pointer
  71. }
  72. } // end destroy
  73.  
  74. void linkedStack::displayMenu()
  75. {
  76. cout << "***************************************" << endl << endl;
  77. cout << "1 Add an integer to the stack" << endl << endl;
  78. cout << "2 Delete the top integer on the stack" << endl << endl;
  79. cout << "3 Print the stack" << endl << endl;
  80. cout << "4 Copy the first stack into a second stack" << endl << endl;
  81. cout << "5 Print both stacks" << endl << endl;
  82. cout << "6 Destroy both stacks" << endl << endl << "9 to EXIT";
  83. }
  84.  
  85. linkedStack::linkedStack()
  86. {
  87. stackTop = NULL;
  88. } //end constructor
  89.  
  90. linkedStack::linkedStack(linkedStack &otherStack)
  91. {
  92. stackTop = NULL;
  93. copyStack(otherStack);
  94. } // end constructor
  95.  
  96. linkedStack::~linkedStack()
  97. {
  98. destroyStack();
  99. } //end destructor
  100.  
  101. void linkedStack::copyStack(linkedStack& otherStack)
  102. {
  103. node *newNode; //pointer to create a node
  104. node *current; //pointer to travel stack
  105.  
  106.  
  107. current = otherStack.stackTop; //current points to the list to be copied
  108.  
  109. assert(stackTop != NULL);
  110.  
  111. stackTop->number = current->number; //copy the letter
  112. stackTop->link = NULL;
  113.  
  114. current = current->link; //make current point to the next node
  115.  
  116. while(current != NULL)
  117. {
  118. newNode = new node;
  119.  
  120. assert(newNode != NULL);
  121.  
  122. newNode->number = current->number; //copy the letter
  123. newNode->link = NULL; //set the link of newNode to NULL
  124.  
  125. stackTop->link = newNode; //attach newNode after stackTop
  126. stackTop = newNode; //make the the top the last node
  127.  
  128. current = current->link; //make current point to next node
  129.  
  130. } //end while
  131. } //end copyStack


main()

  1.  
  2. #include <iostream>
  3. #include "linkedStack.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. linkedStack stack1, stack2;
  10. int choice;
  11.  
  12. stack1.displayMenu();
  13. cout << endl << endl << "Please enter a choice: ";
  14. cin >> choice;
  15. cout << endl;
  16.  
  17. while (choice != 9)
  18. {
  19. switch(choice)
  20. {
  21. case 1: cout << "Enter an integer to add to the stack: ";
  22. cin >> choice;
  23. stack1.push(choice);
  24.  
  25. break;
  26. case 2: stack1.pop();
  27. break;
  28. case 3: stack1.printStack();
  29. break;
  30. case 4: stack1.copyStack(stack2);
  31. break;
  32. case 5: stack1.printStack();
  33. stack2.printStack();
  34. break;
  35. case 6: stack1.destroyStack();
  36. stack2.destroyStack();
  37. break;
  38. default: cout << "Bad Selection!";
  39. } //end switch
  40.  
  41. stack1.displayMenu();
  42. cout << endl << endl << "Enter your choice: ";
  43. cin >> choice;
  44. cout << endl;
  45. }// end while
  46.  
  47. };

HEADER

  1. #ifndef H_linkedStack
  2. #define H_linkedStack
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int number;
  11. node *link;
  12. };
  13.  
  14. class linkedStack
  15. {
  16. public:
  17.  
  18. void initialize();
  19. //Function to initialize the stack to empty.
  20. //Postcondition: stackTop = NULL
  21. bool isEmpty();
  22. //Function to see if stack is empty.
  23. //Postcondition: Returns true if the stack is empty; otherwise, returns false.
  24.  
  25. void push(int );
  26. //Funtion to add an integer to the stack.
  27. //Precondition: The stack exists and is not full.
  28. //Postcondition: The new integer is added to the stack.
  29.  
  30. void pop();
  31. //Function to remove the top element of the stack.
  32. //Precondition: The stack exists and is not empty.
  33. //Postcondition: The stack is changed and the top element
  34. // is removed from the stack.
  35.  
  36. void printStack();
  37. //Function to print the stack to the output device.
  38. //Postcondition: If the stack is empty, the program displays a proper
  39. // message, otherwise, the entire stack is printed.
  40.  
  41. void destroyStack();
  42. //Function to remove all the elements of the stack,
  43. //leaving the stack in an empty state.
  44. //Postcondition: stackTop = NULL
  45.  
  46. void copyStack(linkedStack& otherStack);
  47. //Function to make a copy of otherStack.
  48. //Postcondition: A copy of otherStack is created and
  49. // assigned to this stack.
  50.  
  51. void displayMenu();
  52. //Function to display a menu
  53.  
  54. linkedStack();
  55. //default constructor
  56. //Postcondition: stackTop = NULL
  57. linkedStack(linkedStack& otherStack);
  58. //copy constructor
  59. ~linkedStack();
  60. //destructor
  61. //All the elements of the stack are removed from the stack.
  62.  
  63. private:
  64. node *stackTop; //pointer to the stack
  65.  
  66. };
  67.  
  68. #endif
Climbing the learning curve of C++
Becoming an expert seems light years away!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Integer Stack Copy Function

 
0
  #2
Jun 26th, 2007
1. Inside copyStack() this line..
current = otherStack.stackTop;
..would ensure a crash as it would make current point to NULL.

2. Usually it's better to re-use the code. So in this case you can implement the copy like this:
  1. void linkedStack::copyStack(linkedStack& otherStack)
  2. {
  3. node* currNode = stackTop ;
  4. int* stack_contents = new int[size()] ;
  5.  
  6. for( int i = 0; i < size(); i++, currNode = currNode->link )
  7. {
  8. assert( currNode != NULL ) ;
  9. stack_contents[i] = currNode->number ;
  10. }
  11.  
  12. for( int i = size() - 1; i >= 0; i-- )
  13. otherStack.push( stack_contents[i] ) ;
  14.  
  15. delete stack_contents ;
  16. } //end copyStack
For this to work you have to keep track of the size of the stack. Just add a member size_t _size ; and initialize in c'tor, increment in push() and decrement in pop(). Also add this function size_t size() { return _size() ; }
Good thing abt this is you don't have to re-write the way push() works inside copyStack().
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 73
Reputation: SHWOO is an unknown quantity at this point 
Solved Threads: 0
SHWOO SHWOO is offline Offline
Junior Poster in Training

Re: Integer Stack Copy Function

 
0
  #3
Jun 27th, 2007
The first iterator isn't getting assigned the correct number. instead of size - 1, it is being assigned to -842150451. Then obviously that number is being copied to the new stack.

[code]
void linkedStack::copyStack(linkedStack& otherStack)
{
node* currNode = stackTop;
int* stackContents = new int[stackSize()];
otherStack.initialize();

for( int i = size - 1; i < 0; i--)
{
assert( currNode != NULL );
stackContents[i] = currNode->number;
currNode = currNode->link;
}

for( int j = size -1; j >= 0; j--)
otherStack.push( stackContents[j] );

} //end copyStack

[\code]
Climbing the learning curve of C++
Becoming an expert seems light years away!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC