数据结构算法 Visual C++6.0

《数据结构算法:Visual C++6.0程序集》
///
//
//第一章 顺序存储结构的表、堆栈和队列
//
///

linelist1.h

#define MaxListSize 20
#define EQUAL 1
typedef struct STU{
        char name[10];
        char stuno[10];
        int age;
        int score;
        }ElemType;

class List
{
  private:
       ElemType elem[MaxListSize];
       int length;
       int MaxSize;
  public:
       void init(List **L,int ms);
       void DestroyList(List &L) {free(&L);}
       void ClearList() {length = 0;}
       bool ListEmpty() {return length == 0;}
       bool ListFull() {return length == MaxSize;}
       ElemType PriorElem(ElemType cur_e,ElemType &pre_e);
       ElemType NextElem(ElemType cur_e,ElemType &next_e);
       bool ListDelete(int,ElemType &e);
       void ListTraverse();
       int ListLength();
       void GetElem(int,ElemType *);
       bool EqualList(ElemType *.ElemType *);
       bool Less_EqualList(ElemType *,ElemType *);
       bool LocateElem(ElemType,int);
       bool UpdateList(ElemType& e,ElemType);
       void MergeList(List *,List *);
       bool ListInsert(int,ElemType &);
       void UnionList(List *,List *);
       void printlist(int);
}

 

linelist1.cpp

#include "linelist1.h"
void List::init(List **L,int ms)
{
 *L = (List *)malloc(sizeof(List));
 (*L)->length = 0;
 (*L)->MaxSize = ms;
}

int List::ListLength()
{
  return length;
}

ElemType List::PriorElem(ElemType cur_e,ElemType &pre_e)
{
  for(int i=0;i<length;i++)
   {
     if(i != 0 && strcmp(cur_e.name,elem[i].name) == 0)
      {
         pre_e = elem[i-1];
         return pre_e;
      }
   }
  return cur_e;
}

ElemTpye List::NextElem(ElemType cur_e,ElemType &next_e)
{
   for(int i=0;i<length;i++)
   {
       if(i != length-1 && strcmp(cur_e.name,elem[i].name) == 0)
       {
         next_e = elem[i+1];
         return next_e;
       }
   }
   return cur_e;
}

bool List::ListDelete(int mark,ElemType &e)
{
  int i,j;
  if(ListEmpty()) return false;
  if(mark > 0)
  {
     e = elem[0];
     for(i=1;i<length;i++)
         elem[i-1] = elem[i];
  }
  else if(mark < 0) e = elem[length-1];
  else
  {
     for(i=0;i<length;i++)
     {
         if(strcmp(elem[i].name,e.name) == 0) break;
      }
         if(i >= length) return false;
         else e = elem[i];
         for(j=i+1;j<length;j++)
              elem[j-1] = elem[j];
   }
   length--;
   return true;
}

void List::ListTraverse()
{
   for(int i=0;i<length;i++)
   {
      cout<<setw(8)<<elem[i].name;
      cout<<setw(10)<<elem[i].stuno;
      cout<<setw(9)<<elem[i].age;
      cout<<setw(8)<<elem[i].score<<endl;
    }
}

void List::GetElem(int i,ElemType *e)
{
  *e = elem[i];
}

bool List::EqualList(ElemType *el,ElemType *e2)
{
   if(strcmp(e1->name,e2->name))
       return false
   if(strcmp(e1->stuno,e2->stuno))
       return false;
   if(e1->age != e2->age)
       return false;
   if(e1->score != e2->score)
       return false;
   return true;
}

bool List::Less_EqualList(ElemType *e1,ElemType *e2)
{
  if(strcmp(e1->name,e2->name) == 0) return false;
  else return true;
}

bool List::LocateElem(ElemType e,int type)
{
  int i;
  switch(type)
  {
     case EQUAL:
          for(i=0;i<length;i++)
              if(EqualList(&elem[i],&e))  return true;
          break;
     default:
          break;
  }
  return false;
}

bool List::UpdateList(ElemType &e,ElemType e1)
{
  for(int i=0;i<length;i++)
     if(strcmp(elem[i].name,e.name) == 0) {elem[i] = e1;return true;}
  return false;
}

bool List::ListInsert(int i,Elemtype &e)
{
   ElemType *p,*q;
   if(i<1 || i>length+1) return false;
   q = &elem[i-1];
   for(p=&elem[length-1];p>=q;--p)
         *(p+1) = *p;
   *q = e;
   ++length;
   return true;
}

void List::MergeList(List *La,List *Lb)
{
  int i,j,La_len,Lb_len;
  La_len = La->ListLength();
  Lb_len = Lb->ListLength();
  for(i=0;i<La_len;i++)
      elem[i] = La->elem[i];
  for(j=0;j<Lb_len;j++)
      elem[i+j] = Lb->elem[j];
  length = La_len + Lb_len;
}

 

 

/
//
//1.2 线性表的动态分配顺序表示和实现
//

#define EQUAL 1
#define OVERFLOW -1
#define LIST_INIT_SIZE 30
#define LISTINCREMENT 10
typedef struct STU{
          char name[10];
          char stuno[8];
          int age;
          int score;
        }ElemType;

class List{
     private:
          ElemType *elem;
          int length;
          int listsize;
     pubilc:
          void init(List *);
          void DestroyList(List &L) {free(&L);}
          void ClearList() {length = 0;}
          bool ListEmpty()
          {
             if(length == 0)  return true;
             else  return false;
          }
          bool ListFull();
          {return length == listsize;}
          ElemType PriorElem(ElemType cur_e,ElemType &pre_e);
          ElemType NextElem(ElemType cur_e,ElemType &next_e);
          bool ListDelete(int,ElemType &e);
          void ListTraverse();
          int ListLength();
          void GetElem(); 
}

 

//
//
//3.2 建立一维,二维数组的类定义和实现
//

在程序中将  2类声明为  1类的友元类,使得在 类中的构造函数能够访问  类的成员.在 两个类中同时
重载了下标访问操作符[]

//intarray.h
class IntArray1{
      int size;
      int *data;
      void init(int n);
      InitArray1():size(0),data(0){}
      friend class IntArray2;
public:
      IntArray1(int n){init(n);}
      ~IntArray1(){delete []data;}
      int &operator[](int i);
      int size1()const{return size;}
      void ReArray(int si);
};

class IntArray2{
      int size;
      IntArray1 *data;
public:
      IntArray2(int,int);
      ~IntArray2() {delete []data;}
      IntArray1 &operator[](int i);
      int size1()const{return size;}
      int size2()const{return data->size1();}
};

//intarray.cpp
#include "intarray.h"

void IntArray1::init(int n)
{
   if(n<1)
   {
       cout<<"Error dimesion description";
       exit(1);
   }
   size = n;
   data = new int[size];
}

int &IntArray1::operator[](int i)
{
  if(i<1 || i>size)
  {
     cout<<endl<<"subscript out of range";
     delete []data;
     exit(2);
  }
  return data[i-1];
}

void IntArray1::ReArray(int si)
{
   if(si < 1)
   {
     cout<<"the length is invalid/n";
     exit(1);
   }
   if(si == size) return;
   int *newArray = new int[si];
   if(!mewArray)
   {
     cout<<"the buffer failed!/n";
     exit(1);
   }
   int n = (si <= size)?si::size;
   int *sourceP = data;
   int *destP = newArray;
   while(n--)
       *destP++ = *sourceP++;
   delete []data;
   data = newArray;
   size = si;
}

IntArray2::IntArray2(int m,int n)
{
 if(m<1 || n<1)
   {
    cout<<"Error dimension description";
    exit(1);
   }
  size = m;
  data = new IntArray1[size];
  for(int i=0;i<size;i++) data[i].init(n);
}

IntArray1 &IntArray2::operator[](int i)
{
   if(i<1 || i>size)
   {
      cout<<endl<<"subscript out of range";
      delete []data;
      exit(1);
   }
   return data[i-1];
}


///
//
//3.3 稀疏矩阵的类定义与操作
//
///

//xishu.h
#define MAXSIZE 100
class TSMatrix;

class Triple
{
  public:
     int ii,jj;
     ElemType e;
    friend class TSMatrix;
};

class TSMatrix
{
  public:
     TSMatrix() {}
     TSMatrix(int Mrow,int Mcol,int t);
     void TrMatrix(TSMatrix &);
     void FastTrMatrix(TSMatrix &);
     void mulmatrix(TSMatrix &,TSMatrix &);
     Triple data[MAXSIZE];
     int mu,nu,tu;
};

TSMatrix::TSMatrix(int Mrow,int Mcol,int t)
{
   int m,n,i,j,f0 = 0;
   if(t <= 0) exit(0);
   ElemYype (*A)[Mcol] = new ElemType[Mrow][Mcol];
   if(!A) {cout<<"the buffer failed!/n";exit(-1);}
   for(i=0;i<Mrow;i++)
     for(j=0;j<Mcol;j++)
        A[i][j] = 0;
  srand(150);
  while(f0 <= t)
  {
    m = rand() % 100;
    n = rand() % 10;
    if(m>=0 && m<Mrow && n>=0 && n<Mcol)
    {
       A[m][n]
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值