| | |
Link List with an embedded stack C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
I was trying to figure this out but for some reason it is illuding me
Ministack header provided
Big stack header
ministack implemintation I wrote this code here.
Ministack header provided
C++ Syntax (Toggle Plain Text)
#ifndef MINISTACK_H #define MINISTACK_H const int MINI_STACK_SIZE = 5; // Fixed number of element in stack class MiniStack { private: int num; // Number of values stored in MiniStack char* stackPtr; // Pointer to array representing stack public: MiniStack(); // Default constructor void Push(char ch); // Adds element to top of stack assuming stack not full void Pop(); // Removes element from top of stack void MakeEmpty(); // Empties ministack char Top(); // Returns copy of value stored at top of stack bool IsFull() const; // Returns true if ministack is full; false otherwise bool IsEmpty() const; // Returns true if ministack empty; false otherwise void Print() const; // Prints stack contents, top to bottom ~MiniStack(); // Destructor }; #endif
Big stack header
C++ Syntax (Toggle Plain Text)
#ifndef BIGSTACK_H #define BIGSTACK_H #include "ministack.h" struct ListNode // Description of a ListNode struct { MiniStack* stackPtr; // Pointer to a MiniStack object ListNode* nextPtr; // Pointer to next ListNode }; class BigStack // Description of BigStack class { private: int num; // Total number of values stored in MiniStack ListNode* headPtr; // Pointer to head of list of nodes representing bigstack public: BigStack(); // Default constructor void Push(char ch); // Adds element to top of stack assuming stack not full void Pop(); // Removes element from top of stack char Top(); // Returns copy of top value assuming stack not empty bool IsFull() const; // Returns true if ministack is full; false otherwise bool IsEmpty() const; // Returns true if ministack empty; false otherwise void Print() const; // Prints stack contents, top to bottom ~BigStack(); // Destructor }; #endif
ministack implemintation I wrote this code here.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <fstream> #include <string> #include <cmath> #include <new> #include <cstddef> #include "ministack.h" #include "bigstack.h" using namespace std; MiniStack::MiniStack() // Default constructor { stackPtr = new char[MINI_STACK_SIZE]; num = 0; } void MiniStack::Push(char ch) // Adds element to top of stack assuming stack not full { if (num >=0 && num <=4) { stackPtr[num] = ch; num++; } else if(num < 0) { num = 0; stackPtr[num] = ch; num++; } else num = 5; } void MiniStack::Pop() // Removes element from top of stack { num--; } void MiniStack::MakeEmpty() // Empties ministack { num = -1; } char MiniStack::Top() // Returns copy of value stored at top of stack { return stackPtr[num-1]; } bool MiniStack::IsFull() const // Returns true if ministack is full; false otherwise { if(num == MINI_STACK_SIZE-1) return true; else return false; } bool MiniStack::IsEmpty() const // Returns true if ministack empty; false otherwise { if (num == 0) return true; else return false; } void MiniStack::Print() const // Prints stack contents, top to bottom { int n; n = num - 1; do { if (n <= -1) break; else { cout << stackPtr[n] << " "; n--; } }while (n != -1); cout << endl; } MiniStack::~MiniStack() // Destructor { do { num--; }while (num >= 0); }
C++ Syntax (Toggle Plain Text)
bigstack implemintation Once again I am having a problem with the push function. how in the world do I get these two to work together. 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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <fstream> #include <string> #include <cmath> #include <new> #include <cstddef> #include "ministack.h" #include "bigstack.h" using namespace std; BigStack::BigStack() // Default constructor { headPtr = NULL; num = 0; } [COLOR="Red"]void BigStack::Push(char ch) // Adds element to top of stack assuming stack not full { if (num == 0) { ListNode* nextPtr = new ListNode; nextPtr->nextPtr = headPtr; headPtr = nextPtr; num++; } nextPtr->ch = ch; nextPtr->stackPtr = ch; }[/COLOR] void BigStack::Pop() // Removes element from top of stack { } char BigStack::Top() // Returns copy of top value assuming stack not empty { } bool BigStack::IsFull() const // Returns true if ministack is full; false otherwise { } bool BigStack::IsEmpty() const // Returns true if ministack empty; false otherwise { } void BigStack::Print() const // Prints stack contents, top to bottom { } BigStack::~BigStack() // Destructor { }
Last edited by JustLearning; Sep 26th, 2008 at 5:45 pm.
![]() |
Similar Threads
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- Help Hijackthis (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Formatting output in c++
- Next Thread: Re: how to insert variables into string with % sign?
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






