About the data structure of the stack:
Header file definition for class
// stack.h -- class definition for the stack /******************************************************************/#ifndef _STACK_#define _STACK_typedef unsigned long Item ;class Stack {private: enum { MAX = 10 } ; Item items[MAX] ; int top ;public: Stack(); bool isempty() const ; bool isfull() const ; bool push( Item & item ); bool pop( Item & item );};#endif
About the specific implementation of the section:
// implement for the stack.h ///**********************************************never let the implement file has the cin and cout opeoration , just let class func opers its own private data !*********************************************/#include "stack.h"Stack::Stack(){ top = 0 ;}bool Stack::isempty() const{ return top == 0 ;}// the const is needed to be added to the implement part bool Stack::isfull() const{ return top == 10 ; }bool Stack::push( Item & item){if( top < MAX ){ items[top++] = item ; return true ; } else return false ;}bool Stack::pop(Item & item){if( top == 0 ) return false ;else{ item = items[--top]; return true ; }}
Test section:
#include "stack.h"#include<iostream>int main(){int i ;Item item = 8729 ;Item get ;Stack stack;for ( i = 0 ; i < 15 ; i++ ) stack.push(item);for( i = 0 ; i < 15 ; i++ ) { if(stack.pop(get)) std::cout<<get<<std::endl; }}
For the implementation of the list, you can add a list of different data types
header file for declaration:
#ifndef _list_#define _list_#include <stdio.h>typedef struct elem{char type; Union {int Int_; Short Shrt_; Char Char_; void * PTR; Double Double_; float Float_; }value; Elem * NEXT; Elem * FORE;} Elem;class list{private:enum {INT, Shrt, CHR, PTR, DOU, FLT}; unsigned int count; Elem * HEAD; Elem * Tail;p ublic:list () {count = 0; head = tail = NULL;} BOOL Push (Elem & Item); BOOL Pop (Elem & Item); BOOL Insert (int pos, Elem & Item); BOOL Purge (int pos, Elem & Item); BOOL GetPos (int pos, Elem & Item); BOOL IsEmpty (); void reverse (); void show () const; ~list ();}; #endif具体实现的文件: #include "list.h" #include<iostream> #include <stdio.h>void innershow (Elem * ptr) {printf ("in function \ n"); std::cout<< "Type:"; Switch (ptr->type) {case (0): printf ("int value:%d\n", ptr->value); Break Case (1): printf ("Short value:%d\n", ptr->value); Break Case (2): printf ("Char value:%c\n", ptr->value); Break Case (3): printf ("Pointer value:%p\n", ptr->value); Break Case (4): printf ("Double value:%f\n", ptr->value); Break Case (5): printf ("Float value:%f\n", ptr->value); Break default:printf ("UNKNOWN value:%p\n", ptr->value); Break }}bool List::p ush (Elem & Item) {Elem * entity = new Elem; if (entity = = NULL) return 0; Entity->type = Item.type; Entity->value = Item.value; count++; if (head = = null) {Entity->fore = null; Head = tail = entity; } else{entity->fore = tail; Tail->next = entity; tail = entity; }entity->next = Null;return 1;} BOOL List::p OP (Elem & Item) {Elem * PTR; if (!count) return 0; Item.type = Tail->type; Item.value= tail->value; if (head! = tail) {ptr = Tail->fore; Tail->fore->next = NULL; Delete tail; tail = ptr; } else{Delete tail; Head = tail = NULL; } count--; return 1;} BOOL List::insert (int pos, Elem & Item) {Elem * ptr = NULL; if (pos > Count | | pos < 0) return 0; if (pos = = count) push (item); else{ptr = head; while (pos--) ptr = Ptr->next; Elem * entity = new Elem; if (entity = = NULL) return 0; Entity->type = Item.type; Entity->value = Item.value; if (ptr = = head) {entity->next = head; Head->fore = entity; Entity-&gT;fore = NULL; Head = entity; } else if (ptr = = tail) {entity->next = NULL; Entity->fore = tail; Tail->next = entity; tail = entity; } else{entity->next = ptr; Entity->fore = ptr->fore; Ptr->fore->next = entity; Ptr->fore = entity; } count++; } return 1; }bool List::p urge (int pos, Elem & Item) {Elem * ptr = NULL; if (pos >= count | | pos < 0) return 0; else{ ptr = head; while (pos--) ptr = Ptr->next; if (ptr = = head) {ptr->next->fore = NULL; ptr = Ptr->next; Delete head; head = PTR; } else if (ptr = = tail) {ptr->fore->next = NULL; ptr = Ptr->fore; Delete tail; tail = ptr; } else{ptr->fore->next = Ptr->next; Ptr->next->fore = Ptr->fore; Delete ptr; } count--;} return 1;} BOOL List::getpos (int pos, Elem & Item) {int count; Elem * ptr = NULL; if (pos >= count | | pos < 0) return 0; else{ptr = head; while (pos--) ptr = Ptr->next; Item.type = Ptr->type; Item.value = ptr->value; } return 1;} BOOL List::isempty () {return count==0;} void List::reverse () {Elem * ptr = head; Elem * Swap, * next; while (PTR) {next = Ptr->next; swap = Ptr->fore; Ptr->fore = Ptr->next; Ptr->next = swap; ptr = next; } swap = head; head = tail; tail = swap;} void List::show () const{Elem * ptr = head; Elem * BACK; std::cout<< "List items......\n"; while (PTR) {innershow (PTR); ptr = Ptr->next; } std::cout<< "***************************\n";} List::~list () {count = 0; Elem * ptr = head; Elem * BACK; while (PTR) {BAck = ptr; ptr = Ptr->next; Delete back; }} Specific Test files: #include <iostream> #include <stdio.h> #include "list.h" int main () {Elem Elem = {0, 6526}; List List; List.push (Elem); Elem.type = 1; List.push (Elem); List.show (); Elem.type = 2; List.push (Elem); List.show (); Elem.type = 3; List.push (Elem); List.show (); Elem.type = 4; List.push (Elem); List.show (); printf ("*************\n"); List.show (); printf ("*****swap******\n"); List.reverse (); List.show (); return 0;} I believe these two examples are very good explanations of the idea of class programming.
Two simple examples of C + + class programming