标签:c++
/*********************** WZ ASUST 2016 分页实现:注运用了c语言里的回调函数来实现 ***********************/ #ifndef _SEQLIST_H_ #define _SEQLIST_H_ #define MAX 10 #define CAP 5 typedef int elem_type; class SEQLIST { private: elem_type *data; int length; int TotalSize; public: bool InitSeqList(SEQLIST *p); bool ClearSeqList(SEQLIST *p); bool IsFull(SEQLIST *p); bool IsEmpty(SEQLIST *p); int Getlength(SEQLIST *p); bool InsertHead(SEQLIST *p,elem_type e); bool InsertTail(SEQLIST *p,elem_type e); bool DeleteHead(SEQLIST *p); bool DeleteTail(SEQLIST *p); bool Destroy(SEQLIST *p); bool IncSize(SEQLIST *p); void Show(SEQLIST *p,void (*print)(elem_type)); }; void print_int(elem_type e); void print_char(elem_type e); #endif #include<iostream> #include "SEQLIST.h" using namespace std; #include<assert.h> bool SEQLIST :: InitSeqList(SEQLIST *p) { if(p == NULL) { return false; } p->data = new elem_type[MAX]; assert(p->data != NULL); p->length = 0; p->TotalSize = MAX; return true; } bool SEQLIST :: ClearSeqList(SEQLIST *p) { if(p == NULL) { return false; } p->length = 0; return true; } int SEQLIST :: Getlength(SEQLIST *p) { assert(p != NULL); return p->length; } bool SEQLIST :: IsFull(SEQLIST *p) { assert(p != NULL && p->data != NULL); return p->length == p->TotalSize; } bool SEQLIST :: IsEmpty(SEQLIST *p) { assert(p != NULL && p->data != NULL); return p->length == 0; } bool SEQLIST :: InsertHead(SEQLIST *p,elem_type e) { if(p == NULL) { return false; } assert(p->data != NULL); if(IsFull(p)) { IncSize(p); } int n = p->length; for(int i=n;i>0;i--) { p->data[n] = p->data[n-1]; } p->data[0] = e; p->length++; return true; } bool SEQLIST :: InsertTail(SEQLIST *p,elem_type e) { if(p == NULL) { return false; } assert(p->data != NULL); if(IsFull(p)) { IncSize(p); } p->data[p->length] = e; p->length++; return true; } bool SEQLIST :: DeleteHead(SEQLIST *p) { if(p ==NULL) { return false; } int n = p->length; for(int i=0;i<n;i++) { p->data[i] = p->data[i+1]; } p->length--; return true; } bool SEQLIST :: DeleteTail(SEQLIST *p) { if(p == NULL) { return false; } assert(p->data != NULL); p->length--; return true; } bool SEQLIST :: Destroy(SEQLIST *p) { assert(p != NULL); if(p->data == NULL) { return false; } delete[]p->data; return true; } bool SEQLIST :: IncSize(SEQLIST *p) { if(p == NULL) { return false; } assert(p->data != NULL); p->data = (elem_type *)realloc(p,sizeof(elem_type)*p->TotalSize*2); assert(p->data != NULL); return true; } void SEQLIST ::Show(SEQLIST *p,void (*print)(elem_type)) { assert(p != NULL && p->data != NULL); int n = p->length; for(int i=0;i<n;i++) { print(p->data[i]); } printf("\n"); } #include "SEQLIST.h" #include <iostream> using namespace std; #include<vld.h> int main() { SEQLIST p; p.InitSeqList(&p); p.InsertTail(&p,10); p.InsertTail(&p,20); p.InsertTail(&p,30); p.Show(&p,print_int); p.Destroy(&p); return 0; } void print_int(elem_type e) { printf("%d ",e); } void print_char(elem_type e) { printf("%c ",e); }
标签:c++
原文地址:http://sts609.blog.51cto.com/11227442/1758678