C++ 对多个类使用相同的 ADT 结构

标签 c++ list adt

好的,例如,我必须使用 List ADT 创建一个包含多个类的简单医院队列系统。所以我的问题是typedef。我该怎么做呢,因为 type def 只能有一种数据类型。

  #include <string>
  #include "Patients.h"
  #include "Records.h"
  #include "Services.h"

const int MAX_SIZE = 10000;
typedef Patients ItemType;
typedef Records ItemType;         //Error Here
typedef Services ItemType;        //Error Here

class List
{
  private:
    ItemType items[MAX_SIZE];
    int      size;

  public:

List::List();

void List::display();

void List::replace(int index, ItemType item);

bool List::add(ItemType newItem);

bool List::add(int index, ItemType newItem);

void List::remove(int index);

ItemType List::get(int index); 

bool List::isEmpty(); 

int List::getLength();

};




#include <iostream>
#include "List.h"  // header file
using namespace std;
// constructor
List::List()
{
    size = 0;
}  

// add a new item to the back of the list (append)
bool List::add(ItemType newItem)
{
   bool success = size < MAX_SIZE;
   if (success)
   {  
      items[size] = newItem; // add to the end of the list
      size++;                // increase the size of the list by one
   }  
   return success;
}  

// add a new item at a specified position in the list (insert)
bool List::add(int index, ItemType newItem)
{
   bool success = (index >= 1) && (index <= size + 1) && (size < MAX_SIZE);
   if (success)
   {  
      for (int pos = size; pos >= index; pos--)
         items[pos] = items[pos-1];

      items[index-1] = newItem;
      size++;  // increase the size of the list by one
   }  
   return success;
}  

 // remove an item at a specified position in the list
void List::remove(int index)
{
   bool success = (index >= 1) && (index <= size);
   if (success)
   { 
      for (int fromPosition = index + 1; fromPosition <= size; fromPosition++)
         items[fromPosition - 2] = items[fromPosition - 1];

      size--; 
   }  

}  

// get an item at a specified position of the list (retrieve)
ItemType List::get(int index)
{
   ItemType dataItem;// = 0;
   bool success = (index >= 1) && (index <= size);
   if (success)
      dataItem = items[index - 1];

   return dataItem;
}  

// check if the list is empty
bool List::isEmpty()
{
   return size == 0;
}  

// check the size of the list
int List::getLength()
{
   return size;
}  


void List::replace(int index, ItemType item)
{
bool success = index >= 1 && index <= getLength();
if (success)
    items[index] = item;
}

最佳答案

你应该使用模板:

#include <list>

typedef std::list<Patient> Patients;
typedef std::list<Record> Records;
typedef std::list<Service> Services;

关于C++ 对多个类使用相同的 ADT 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285337/

相关文章:

java - 如何在java中对列表对象(int)进行排序?

java - 将嵌套列表 List<List<Object>> 转换为 Set<Object>

c++ - 修改对数曲线上的值

c++ - Qt 子目录包含类

c++ - 函数调用中char[]和char*的区别

c++ - 在迭代 std::list 时删除

java - token "boolean"出现语法错误,表达式无效

c - 带有 Android NDK 和 C 文件的 Eclipse ADT "... could not be resolved"

java - Android 在内部与外部导入库的好处

android - 编程新手在 android 和 iOS 之间选择