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

📄 operation.cpp

📁 模块化的链表的示例
💻 CPP
字号:
#include "class.h"
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <iomanip.h>

clsList_c::clsList_c( )
{
	cout << "*******************************************************" << endl;
	psthead = NULL;
}

clsList_c::clsList_c( int count )
{
	uidata = count;
	cout << "Constructor working..." << endl;
	clsNode_c * pstp = NULL, * pstq = NULL;
	psthead = new clsNode_c;
	psthead->pstnext = NULL;
	pstp = psthead;
	int uii = 0;
	for( ; uii < count; ++uii )
	{
		pstq = new clsNode_c;
		if( NULL == pstq )
		{
			cout << "Error!!" << endl;
			exit(1);
		}
		else
		{
			++uidata;
			pstq->uinodeuidata = rand() % 100;
			pstp->pstnext = pstq;
			pstp = pstq;
		}
		pstq->pstnext = NULL;
	}
}

clsList_c::clsList_c( clsList_c & cllistcopy )
{
	clsNode_c * pstp = NULL, * pstnew = NULL, * psttemp = NULL;
	cout << "Now CopyConstrutor is working..." << endl;
	uidata = cllistcopy.uidata;
	psthead = new clsNode_c;
	psthead->pstnext = NULL;
	pstp = cllistcopy.psthead->pstnext;
	psttemp = psthead;
	while ( NULL != pstp )
	{
		pstnew = new clsNode_c;
		pstnew->pstnext = NULL;
		if( NULL == pstnew )
		{
			cout << "Error!!" << endl;
			exit(1);
		}
		//cout<<pstp->uinodeuidata<<endl;
		pstnew->uinodeuidata = pstp->uinodeuidata;
		psttemp->pstnext = pstnew;
		psttemp = pstnew;
		pstp = pstp->pstnext;
	}
	pstnew->pstnext = NULL;
}

clsList_c & clsList_c::operator = ( const clsList_c & cllistdemo )
{
	this->ClearList( );
	
	clsNode_c * pstp = NULL, * pstnew = NULL, * psttemp = NULL;
	cout << "Now Operator = working..." << endl;
	uidata = cllistdemo.uidata;
	psthead = new clsNode_c;
	psthead->pstnext = NULL;
	pstp = cllistdemo.psthead->pstnext;
	psttemp = psthead;
	while ( NULL != pstp )
	{
		pstnew = new clsNode_c;
		pstnew->pstnext = NULL;
		if( NULL == pstnew )
		{
			cout << "Error!!" << endl;
			exit(1);
		}
		pstnew->uinodeuidata = pstp->uinodeuidata;
		psttemp->pstnext = pstnew;
		psttemp = pstnew;
		pstp = pstp->pstnext;
	}
	pstnew->pstnext = NULL;
	cout << "= OverLoad Complete..." << endl;
	return * this;
}

clsList_c & clsList_c::operator + ( const clsList_c & cllistdemo )
{
	cout << "Now Operator + working..." << endl;
	clsNode_c * pstp = NULL, * pstq = NULL, * pstnew = NULL;
	pstp = psthead;
	while ( NULL != pstp->pstnext )
	{
		pstp = pstp->pstnext;
	}
	pstq = cllistdemo.psthead->pstnext;
	while ( NULL != pstq )
	{
		pstnew = new clsNode_c;
		if( NULL == pstnew )
		{
			cout << "Error!!" << endl;
			exit(1);
		}
		pstnew->uinodeuidata = pstq->uinodeuidata;
		pstp->pstnext = pstnew;
		pstp = pstnew;
		pstq = pstq->pstnext;
	}
	pstnew->pstnext = NULL;
	cout << "+ OverLoad Complete..." << endl;
	return * this;
}

void clsList_c::Insert( int position, int elem )
{
	clsNode_c * pstp = NULL, * pstq = NULL;
	int uicounter = 0, uii = 0, uij = 0;
	pstp = psthead;
	if ( uidata >= MAXSIZE )
	{
		cout << "The list has full,delete some nodes." << endl;
		int uitemp = 0;
		cout << "Input a postion to remove this node" << endl;
		cin >> uitemp;
		Delete( uitemp );
		cout << "Please input the node's value and it's position:   " << endl;
		cin >> uii >> uij;
		while ( ( uii < 0 ) || ( uii > MAXSIZE ) )
		{
			cout << "Bad input, try again" << endl;
			cin >> uij >> uii;
		}
	}
	while ( ( NULL != pstp->pstnext ) && ( uicounter < position ) )
	{
		pstp = pstp->pstnext;
		++uicounter;
	}
	pstq = new clsNode_c;
	if( NULL == pstq )
	{
		cout << "Error!!" << endl;
		exit(1);
	}
	else
	{
		pstq->uinodeuidata = elem;
		pstq->pstnext = pstp->pstnext;
		pstp->pstnext = pstq;
		++uidata;
	}
}

void clsList_c::Delete( int position )
{
	clsNode_c * pstp = NULL, * pstq = NULL;
	int uicounter = 0;
	pstp = psthead;
	if ( position > uidata )
	{
		cout << "OverFlow..." << endl;
		exit(1);
	}
	else
	{
		while ( ( NULL != pstp->pstnext ) && ( uicounter < position - 1 ) )
		{
			pstp = pstp->pstnext;
			++uicounter;
		}
		pstq = pstp->pstnext;
		pstp->pstnext = pstq->pstnext;
		delete pstq;
		--uidata;
	}
}

void clsList_c::IsEmpty(  )
{
	if( NULL == psthead->pstnext )
	{
		cout << "List is empty,you can still insert elems." << endl;
	}
	else
	{
		cout << "There have many elems in the list,so it is not empty." << endl;
	}
}

void clsList_c::IsFull(  )
{
	if ( uidata == MAXSIZE )
	{
		cout << "List has full!!!" << endl;
	}
	else
	{
		cout << "List now is not full,you can still insert elems." << endl;
	}
}

void clsList_c::ClearList(  )
{
	clsNode_c * pstp = NULL;
	pstp = psthead;
	while ( NULL != psthead )
	{
		psthead = psthead->pstnext;
		delete pstp;
		pstp = psthead;
	}
	//delete psthead;
}

void clsList_c::PrintList(  )
{
	clsNode_c * pstp = psthead->pstnext;
	while ( NULL != pstp )
	{
		cout << setw(5) << pstp->uinodeuidata;
		pstp = pstp->pstnext;
	}
	cout << endl;
}

clsList_c::~clsList_c()
{
	cout << "Distructor working..." << endl;
	clsNode_c * pstp = NULL;
	pstp = psthead;
	while ( NULL != psthead )
	{
		psthead = psthead->pstnext;
		delete pstp;
		pstp = psthead;
	}
}

⌨️ 快捷键说明

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