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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章