⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex12_3.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

#include "seqlist2.h"

// collection class implementing insertion and
// deletion at the middle of a sequential list
template <class T>
class MidList: public SeqList<T>
{
	public:
		// constructor
		MidList(void);

		// override the virtual Insert method
		// of the base class
		virtual void Insert(const T& item);
};
		
// constructor. initialize the base class
template <class T>
MidList<T>::MidList(void): SeqList<T>()
{}

// insert item at the middle of the list
template <class T>
void MidList<T>::Insert(const T& item)
{
	int i;
	
	// start at front of the list
	llist.Reset();
	// move to position size/2
	for(i=0;i < size/2;i++)
		llist.Next();
	// insert at position size/2
	llist.InsertAt(item);
	// increment list size
	size++;
}

// print the list L
template <class T>
void PrintList(MidList<T>& L)
{
	for(int i=0;i < L.ListSize();i++)
		cout << L.GetData(i) << "  ";
	cout << endl;
}

void main(void)
{
	// declare an MidList object
	MidList<int> L;
	int n;
	
	// read and insert 5 integers
	for(int i=0;i < 5;i++)
	{
		cin >> n;
		L.Insert(n);
	}
	
	// print the list
	PrintList(L);

	// delete two elements
	L.Delete(L.GetData(1));
	L.Delete(L.GetData(3));
	
	// print the new list and its size
	PrintList(L);
	cout << "List size = " << L.ListSize() << endl;	
}

/*
<Run>

3 8 7 2 1
8  2  1  7  3  
8  1  7  
List size = 3
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -