实验八泛型程序设计

实验八泛型程序设计
实验八泛型程序设计

实验八泛型程序设计

软件1502 杨成进 0

一、实验目的

1.了解链表类的定义与实现,学习其使用方法。

2.了解栈类的定义与实现,学习其使用方法。

3.了解队列类的定义与实现,学习其使用方法。

4.了解C++标准模板库STL的使用方法。

二、实验任务

1.编写程序link.h,实现教材中例9—6的链表类。在测试程序lab9—1.cpp中定义两个整型链表A和B,分别插入5个元素,然后把B中的元素加入A的尾部。

2.编写程序queue.h,用链表实现队列(或栈)类。在测试程序lab9—2.cpp中定义一个整型队列(或栈)对象,插入5个整数,压人队列(或栈),再依次取出并显示出来。

3.使用C++标准模板库(STL)中的双向队列类(deque)重新实现上一小题。

三、实验步骤

1.参照教材《C++语言程序设计》中链表类LinkeclI。ist的定义(教材中的例程9—6.h),给出其实现,注意合理使用NodIe类(教材中的例程9—3.h)的成员函数。在测试程序中定义整型链表A和B,分别插入5个元素,使用循环语句显示链表中的元素,然后把B中的元素加入A的尾部,再显示出来。

2.队列类的特点就是其元素的操作顺序为先入先出(FIFO),用上题中的链表类实现队列类,用链表类的成员函数实现队列的成员函数,在测试程序中定义一个整型队列对象,观察队列类中的元素先入先出的特点。

3.在程序中包含语句#include ,使用deque类的方法push_back()、empty()、pop_front()完成上一小题的要求。程序名:。、

四、实验程序

1、

#include ""

int main()

{

L inkedList A, B;

f or(int i=0;i<5;i++)

{

(2*i+1);

(2*i+2);

}

();

c out << "链表A的元素为:" ;

w hile(!())

{

cout << () << " ";

();

}

c out << endl;

();

c out << "链表B的元素为:" ;

w hile(!())

{

cout << () << " ";

();

}

c out << endl;

c out << "把B中的元素插入A中..." << endl; ();

{

());

();

}

();

c out << "此时,链表A的元素为:" ;

w hile(!())

{

cout << () << " ";

();

}

c out << endl;

}

#ifndef LINKEDLIST_CLASS

#define LINKEDLIST_CLASS

#include

#include

using namespace std;

#ifndef NULL

const int NULL = 0;

#endif 接插入排序 2.直接选择排序 3.冒泡排序:" ;

c in >> SortType;

s witch(SortType)

{

();

break;

c ase 2:

();

break;

c ase 3:

();

break;

d efault:

cout << "输入错误,程序退出!";

exit(0);

}

c out << "排序后的数组为:" << endl;

f or(i=0;i<10;i++)

cout << A[i] << " ";

c out << endl;

c out << "请输入待查找的数字:";

c in >> SearchNum;

k= (SearchNum);

i f (k<0)

cout << "没有找到数字" << SearchNum << endl;

e lse

cout << SearchNum << "是第" << k+1 << "个数字" << endl; }

#ifndef ARRAY1_CLASS

#define ARRAY1_CLASS

#include

#include

using namespace std;

#ifndef NULL

const int NULL = 0;

#endif

enum ErrorType

{invalidArraySize, memoryAllocationError, indexOutOfRange}; char *errorMsg[] =

{

"Invalid array size", "Memory allocation error",

"Invalid index: "

};

template

class Array

{

private:

T* alist;

int size;

void Error(ErrorType error,int badIndex=0) const;

public:

Array(int sz = 50);

Array(const Array& A);

~Array(void);

Array& operator= (const Array& rhs);

T& operator[](int i);

operator T* (void) const;

int ListSize(void) const;

void Resize(int sz);

void InsertionSort();

void SelectionSort();

void BubbleSort();

int SeqSearch(T key);

};

template

void Array::Error(ErrorType error, int badIndex) const {

cerr << errorMsg[error];

if (error == indexOutOfRange)

cerr << badIndex;

cerr << endl;

exit(1);

}

template

Array::Array(int sz)

{

if (sz <= 0)

Error(invalidArraySize);

size = sz;

alist = new T[size];

if (alist == NULL)

Error(memoryAllocationError);

}

template

Array::~Array(void)

{

delete [] alist;

}

template

Array::Array(const Array& X)

{

int n = ;

size = n;

alist = new T[n];

if (alist == NULL)

Error(memoryAllocationError);

T* srcptr = ;

T* destptr = alist;

while (n--)

*destptr++ = *srcptr++;

}

template

Array& Array::operator= (const Array& rhs)

int n = ;

if (size != n)

{

delete [] alist;

alist = new T[n];

if (alist == NULL)

Error(memoryAllocationError);

size = n;

}

T* destptr = alist;

T* srcptr = ;

while (n--)

*destptr++ = *srcptr++;

return *this;

}

template

T& Array::operator[] (int n)

{

if (n < 0 || n > size-1)

Error(indexOutOfRange,n);

return alist[n];

}

template

Array::operator T* (void) const

return alist;

}

template

int Array::ListSize(void) const

{

return size;

}

template

void Array::Resize(int sz)

{

if (sz <= 0)

Error(invalidArraySize);

if (sz == size)

return;

T* newlist = new T[sz];

if (newlist == NULL)

Error(memoryAllocationError); int n = (sz <= size) sz : size; T* srcptr = alist;

T* destptr = newlist;

while (n--)

*destptr++ = *srcptr++;

delete[] alist;

alist = newlist;

size = sz;

}

template

void Array::InsertionSort()

{

i nt i, j;

T temp;

f or (i = 1; i < size; i++)

{

j = i;

temp = alist[i];

while (j > 0 && temp < alist[j-1])

{

alist[j] = alist[j-1];

j--;

}

alist[j] = temp;

}

}

template

void Array::SelectionSort()

{

i nt smallIndex;

i nt i, j;

f or (i = 0; i < size-1; i++)

{

smallIndex = i;

for (j = i+1; j < size; j++)

if (alist[j] < alist[smallIndex])

smallIndex = j;

Swap(alist[i], alist[smallIndex]); }

}

template

void Swap (T &x, T &y)

{

T temp;

t emp = x;

x = y;

y = temp;

}

template

void Array::BubbleSort()

{

i nt i,j;

i nt lastExchangeIndex;

i = size-1;

w hile (i > 0)

{

lastExchangeIndex = 0;

for (j = 0; j < i; j++)

if (alist[j+1] < alist[j])

{

Swap(alist[j],alist[j+1]);

lastExchangeIndex = j;

}

i = lastExchangeIndex;

}

}

template

int Array::SeqSearch(T key)

{

f or(int i=0;i < size;i++)

if (alist[i] == key)

return i;

r eturn -1;

}

#endif

3、

#include

#include

using namespace std;

typedef deque

INTDEQUE;

int main()

{

INTDEQUE A;

for(int i=0;i<5;i++)

{

(2*i+1);

}

cout << "队列A的元素为:" while(!())

{

cout << () << " "; ();

}

cout << endl;

}

相关主题
相关文档
最新文档