标签:sem void else printf oid .sh case public wap
关于栈的数据结构:// 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
关于具体实现的部分:
// 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 ;
}
}
测试部分:
#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;
}
}
关于列表的实现,可以加入不同的数据类型的列表
声明的头文件:
#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 ;
public:
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::push(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::pop( 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->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::purge ( 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;
}
}
具体的测试文件:
#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 ;
}
相信这两个例子都非常好的解释了类编程的思想
标签:sem void else printf oid .sh case public wap
原文地址:http://blog.51cto.com/13824643/2132622