用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

线性表基本操作(增删改查,判断是否空,求长度...)

2012-09-22 作者: 神马举报

[c++]代码库

#include<iostream.h>
#include<stdlib.h>
typedef int elemtype;
struct list
{
	elemtype *list;
	int len;
	int maxsize;
};

void initlist ( list &l,int ms )
{
	l.list=new elemtype[ms];
	if ( !l.list )
	{
		cerr<<"memory allocation failure!"<<endl;
		exit ( 1 );
	}
	l.len=0;
	l.maxsize=ms;
}

void clearlist ( list &l )
{ l.len=0;}

int length ( list &l )
{  return l.len; }

int listempty ( list &l )
{  return l.len==0;}

int listfull ( list &l )
{ return l.len==l.maxsize;}

void looklist ( list &l )
{
	for ( int i=0; i<l.len; i++ )
		cout<<l.list[i]<<' ';
	cout<<endl;
}

int findlist ( list &l,elemtype x )
{
	for ( int i=0; i<l.len; i++ )
		if ( l.list[i]==x )
		{
			x=l.list[i];
			return 1;
		}
	return 0;
}

int insertlist ( list &l,elemtype x,int k )
{
	if ( listfull ( l ) ) return 0;
	if ( k>0 )
	{
		for ( int i=l.len-1; i>=0; i-- )
			l.list[i+1]=l.list[i];
		l.list[0]=x;
	}
	else if ( k<0 ) l.list[l.len]=x;
	else
	{
		for ( int i=0; i<l.len; i++ )
			if ( x<l.list[i] )  break;
		for ( int j=l.len-1; j>=i; j-- )
			l.list[j+1]=l.list[j];
		l.list[i]=x;
	}
	l.len++;
	return 1;
}
int deletelist ( list &l,elemtype &x,int k )
{
	if ( listempty ( l ) ) return 0;
	if ( k>0 )
	{
		x=l.list[0];
		for ( int i=1; i<l.len; i++ )
			l.list[i-1]=l.list[i];
	}
	else if ( k<0 ) x=l.list[l.len-1];
	else
	{
		for ( int i=0; i<l.len; i++ )
			if ( l.list[i]==x ) break;
		if ( i>=l.len )
			return 0;
		else x=l.list[i];
		for ( int j=i+1; j<l.len; j++ )
			l.list[j-1]=l.list[j];
	}
	l.len--;
	return 1;
}

void outputlist ( list &l,int m )
{
	int *b=new int[l.len];
	int i,k;
	for ( i=0; i<l.len; i++ ) b[i]=i;
	for ( i=1; i<l.len; i++ )
	{
		k=i-1;
		for ( int j=i; j<l.len; j++ )
		{
			if ( m==1&&l.list[b[j]]<l.list[b[k]] ) k=j;
			if ( m!=1&&l.list[b[j]]>l.list[b[k]] ) k=j;
		}
		if ( k!=i-1 )
		{
			int x=b[i-1];
			b[i-1]=b[k];
			b[k]=x;
		}
	}
	for ( i=0; i<l.len; i++ )
		cout<<l.list[b[i]]<<' ';
	cout<<endl;
}

void main()
{
	const int ml=20;
	list a;
	initlist ( a,ml );
	int i;
	elemtype x;
	cout<<"input 5 int num:";
	for ( i=0; i<5; i++ )
	{
		cin>>x;
		insertlist ( a,x,-1 );
	}
	cout<<"input 2 int num:";
	cin>>x;
	insertlist ( a,x,1 );
	cin>>x;
	insertlist ( a,x,1 );
	looklist ( a );
	cout<<"uporder sort:";
	outputlist ( a,1 );
	cout<<"downorder sort:";
	outputlist ( a,0 );
	list b;

	initlist ( b,ml );
	for ( i=0; i<a.len; i++ )
		insertlist ( b,a.list[i],0 );
	looklist ( b );
	if ( deletelist ( a,x,1 ) )
		cout<<"delete front!"<<endl;
	else
		cout<<"delete fale!"<<endl;
	looklist ( a );
	if ( deletelist ( a,x,-1 ) )
		cout<<"delete rear!"<<endl;
	else
		cout<<"delete fale!"<<endl;
	looklist ( a );
	cout<<"input a del num:";
	cin>>x;
	if ( deletelist ( a,x,0 ) )
		cout<<"delete success!"<<endl;
	else
		cout<<"delete fale!"<<endl;
	looklist ( a );
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...