בעיקרון
בעיקרון זה חלק מתוכנית יותר גדולה, אבל אני אנסה לעלות קודם רק את החלק שעושה את הבעיה... וקצת זכויות יוצרים, הקוד פה חלקית לקוח מתוך ספר של אופוס על מבנה נתונים. הנה הקובץ header
#ifndef __CSTACK_H_ #define __CSTACK_H_ 1 #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <malloc.h> typedef long lValue; struct ds { lValue value; ds *next; ds(lValue newVal, ds *newNext) : value(newVal),next(newNext) {}; } ; class CStack { private: ds *head; public: CStack(): head(NULL) {}; ~CStack() {make_null();}; void push(const lValue &new_value); lValue pop(void); lValue top() const; int isEmpty() const; void make_null(); } ; #endif
והקובץ cpp:
#include "CStack.h" void CStack:

ush(const lValue &new_value) { ds *new_link; if (!(new_link = new ds(new_value,head))) { printf("\nERROR:: Out Of Memory\n"); exit(1); } head = new_link; } lValue CStack:

op(void) { if (isEmpty()) { printf("\nERROR:: Underflow, no data in stack\n"); exit(1); } ds *temp = head; lValue temp_value = temp->value; head = temp->next; delete temp; return temp_value; } lValue CStack::top() const { if (isEmpty()) { printf("\nERROR:: no data in stack\n"); exit(1); } return head->value; } int CStack::isEmpty() const { if (!head) return 1; return 0; } void CStack::make_null() { ds *pre, *fore; if (!isEmpty()) { for (pre = head, fore = head->next; fore; pre = fore, fore = fore->next) delete pre; delete pre; } head = NULL; }