リニアテーブル-シーケンステーブル、チェーンテーブルクラステンプレートの実装(データ構造ベース2週目)
26865 ワード
授業が終わった後、自分でC++で簡単な順序表とチェーン表を実現し、ジョセフ問題でテストを行い、完全に正しいことを保証しなかった.C++のクラステンプレート宣言ヘッダファイルと実装ヘッダファイルは分離できない点に注意してください.h和.cppでは、そうでないと正常にコンパイルできません.詳細は以下を参照してください.https://www.zhihu.com/question/20630104
ソース1.シーケンステーブル//seqlist.h
//source.cpp
2.チェーンテーブル//lnklist.h
//source.cpp
ソース1.シーケンステーブル//seqlist.h
#pragma once
#include <iostream>
using namespace std;
template <class T> class seqlist
{
private:
T* aList; //
int maxSize; //
int curLen; //
int position;//
public:
seqlist(const int size);
~seqlist();
void clear(); //
int length();
bool isEmpty(); // , true
bool append(const T value); // value, 1
bool insert(const int p, const T value); // p value, 1
bool delet(const int p); // p , 1
bool getPos(int& p, const T value); // value
bool getValue(const int p, T& value); // p value
bool setValue(const int p, const T value); // value p
};
template <class T> seqlist<T>::seqlist(const int size)
{
maxSize=size;
aList = new T[maxSize];
curLen=position=0;
}
template <class T> seqlist<T>::~seqlist()
{
delete []aList;
}
template <class T> void seqlist<T>::clear()
{
delete [] aList;
curLen=position=0;
aList=new T[maxSize];
}
template <class T> int seqlist<T>::length()
{
return curLen;
}
template <class T> bool seqlist<T>::isEmpty()
{
if (curLen==0) {
return true;
}
else {
return false;
}
}
template <class T> bool seqlist<T>::append(const T value)
{
if (curLen>=maxSize) //
{
cout << "The list is overflow" << endl;
return false;
}
aList[curLen]=value;
curLen++;
return true;
}
// T,aList ,maxSize ;
//p value , true, false
template <class T> bool seqlist<T>::insert(const int p, const T value)
{
if (curLen>=maxSize) //
{
cout << "The list is overflow" << endl;
return false;
}
if (p<0 || p>curLen) //
{
cout << "Insertion point is illegal" << endl;
return false;
}
for(int i=curLen; i>p; i--)
aList[i]=aList[i-1]; // curLen-1 p
aList[p]=value; // p
curLen++; // 1
return true;
}
// T;aList ;p
// true, false
template <class T> bool seqlist<T>::delet(const int p)
{
if (curLen<=0) //
{
cout << "No element to delete" << endl;
return false;
}
if (p<0 || p>curLen-1) //
{
cout << "deletion is illegal" << endl;
return false;
}
for(int i=p; i<curLen; i++)
aList[i]=aList[i+1];
curLen--;
return true;
}
template <class T> bool seqlist<T>::getPos(int& p, const T value)
{
for(int i=0; i<curLen; i++)
if (aList[i]==value) {
p=i;
return true;
}
cout << "can not find element: " << value << endl;
return false;
}
template <class T> bool seqlist<T>::getValue(const int p, T& value)
{
if (curLen<=0) //
{
cout << "No element" << endl;
return false;
}
if (p<0 || p>curLen-1) //
{
cout << "getvalue is illegal" << endl;
return false;
}
value = aList[p];
return true;
}
template <class T> bool seqlist<T>::setValue(const int p, const T value)
{
if (curLen<=0) //
{
cout << "No element" << endl;
return false;
}
if (p<0 || p>curLen-1) //
{
cout << "setvalue is illegal" << endl;
return false;
}
aList[p] = value;
return true;
}
//source.cpp
#include <iostream>
#include "seqlist.h"
#include <stdio.h>
using namespace std;
void josephus_seq(seqlist<int>& palist, int s, int m)
{
int del = s;
int w=0;
for(int i=palist.length(); i>0; i--) {
del=(del+m-1)%i; //
if (del==0) del = i;
palist.getValue(del-1, w); // del
printf("Out element %d
", w); //
palist.delet(del-1); //
}
}
int main() {
seqlist<int> jos_alist(100);
int n, s, m;
printf("
please input the values(<100) of n = "); //
scanf("%d", &n);
printf("please input the values of s = "); //
scanf("%d", &s);
printf("please input the values of m = "); ///
scanf("%d", &m);
for (int i=0; i<n; i++) {
jos_alist.append(i+1);
}
josephus_seq(jos_alist, s, m);
return 0;
}
2.チェーンテーブル//lnklist.h
#pragma once
template <class T>
class Link
{
public:
T data; //
Link<T> *next; //
Link(const T info, Link<T>* nextValue=NULL) {
data=info;
next= nextValue;
}
Link():next(NULL) {}
};
template <class T>
class lnkList:public Link<T> {
private:
Link<T> *head, *tail; //
Link<T>* setPos(const int i); // i
public:
lnkList(); //
~lnkList(); //
bool isEmpty(); //
void clear(); //
int length(); //
bool append(const T value); // value, 1
bool insert(const int i, const T value); // i value, 1
bool delet(const int i); // i , 1
bool getValue(const int i, T& value); // i value
bool getPos(int& i, const T value); // value
};
template <class T>
lnkList<T>::lnkList() {
head=NULL;
tail=NULL;
}
template <class T>
lnkList<T>::~lnkList() {
if (head==NULL)
{
return;
}
Link<T>* cur=head;
while(cur != NULL) {
Link<T>* del=cur;
cur = cur->next;
delete del;
}
delete cur;
head=NULL;
tail=NULL;
}
template <class T>
Link<T>* lnkList<T>::setPos(const int i) {
int count=0;
Link<T>* p=head; // , i 0
while(p != NULL && count<i) {
p=p->next;
count++;
}
return p; // i
}
template <class T>
bool lnkList<T>::insert(const int i, const T value) {
Link<T> *p, *q;
if ((p=setPos(i-1))==NULL) //p i
{
cout << "Insertion point is illegal" << endl;
return false;
}
q = new Link<T>(value, p->next);
p->next = q;
if (p==tail) // ,
{
tail = q;
}
return true;
}
template <class T>
bool lnkList<T>::delet(const int i) {
Link<T> *p, *q;
if (i==0){ //
q=head;
head=head->next;
delete q;
return true;
}
p=setPos(i-1);
if (p==NULL || p==tail) { //
cout << "deletion is illegal" << endl;
return false;
}
q=p->next; //q
if (q==tail) // ,
{
tail = p;
p->next=NULL;
}
else
p->next=q->next; // q
delete q;
return true;
}
template <class T>
bool lnkList<T>::isEmpty() {
if (head==NULL) {
return true;
}
else {
return false;
}
}
template <class T>
void lnkList<T>::clear() {
if (head==NULL)
{
return;
}
Link<T>* cur=head;
while(cur != NULL) {
Link<T>* del=cur;
cur = cur->next;
delete del;
}
delete cur;
head=NULL;
tail=NULL;
}
template <class T>
int lnkList<T>::length() {
int count=0;
Link<T>* cur=head;
while(cur) {
count++;
cur = cur->next;
}
return count;
}
template <class T>
bool lnkList<T>::append(const T value) {
Link<T>* newLink = new Link<T>(value);
if (head==NULL) {
head=newLink;
tail=head;
}
else {
tail->next = newLink;
tail=newLink;
}
return true;
}
template <class T>
bool lnkList<T>::getPos(int& i, const T value) {
Link<T>* cur=head;
int count=0;
while(cur) {
if (cur->data==value)
{
i=count;
return true;
}
cur = cur->next;
count++;
}
cout << "can not find element: " << value << endl;
return false;
}
template <class T>
bool lnkList<T>::getValue(const int i, T& value) {
int count=0;
Link<T>* cur=head; // , i 0
while(cur != NULL && count<i) {
cur=cur->next;
count++;
}
if (cur==NULL) {
return false;
}
else {
value = cur->data;
return true;
}
}
//source.cpp
#include <iostream>
#include "lnklist.h"
#include <stdio.h>
using namespace std;
void josephus_seq(lnkList<int>& palist, int s, int m)
{
int del = s;
int w=0;
for(int i=palist.length(); i>0; i--) {
del=(del+m-1)%i; //
if (del==0) del = i;
palist.getValue(del-1, w); // del
printf("Out element %d
", w); //
palist.delet(del-1); //
}
}
int main() {
lnkList<int> jos_alist;
int n, s, m;
printf("
please input the values(<100) of n = "); //
scanf("%d", &n);
printf("please input the values of s = "); //
scanf("%d", &s);
printf("please input the values of m = "); ///
scanf("%d", &m);
for (int i=0; i<n; i++) {
jos_alist.append(i+1);
}
josephus_seq(jos_alist, s, m);
return 0;
}