Status ListDelete_SL( const int L , const int i , ElemType& e )
{
	if( L == 0 )
	{
		return ERROR;
	} 

	int c = 1;
	int prev_p = L;
	int curr_p = g_slink_list[L].next;
	while( curr_p )
	{ 
		if( c == i )
		{
			g_slink_list[prev_p].next = g_slink_list[curr_p].next;
			Free_SL(g_slink_list , curr_p); 
			return OK;
		} 

		prev_p = curr_p;
		curr_p = g_slink_list[curr_p].next;
		c++;
	}

	return ERROR;
}
示例#2
0
void difference(SLinkList space, int s)
{
	InitSpace_SL(space);
	s = Malloc_SL(space);
	
	int r = s;
	int m = 5;
	int n = 3;

	int i, j, b, p, k;
	for(j=1; j<=m; ++j)
	{
		i = Malloc_SL(space);
		space[i].data = j;
		space[r].cur = i;
		r = i;
	}
	space[r].cur = 0;

	for(j=1; j<=n; ++j)
	{
		b = j;
		p = s;
		k = space[s].cur;
		while(k != space[r].cur && space[k].data != b)
		{
			p = k;
			k = space[k].cur;
		}

		if(k == space[r].cur)
		{
			i = Malloc_SL(space);
			space[i].data = b;
			space[i].cur = space[r].cur;
			space[r].cur = i;
		}
		else
		{
			space[p].cur = space[k].cur;
			Free_SL(space, k);
			if(r == k)
				r = p;
		}
	}
}
 Status DestroyList_SL( int& L )
{
	if( L == 0 )
	{
		return ERROR;
	}

	int curr = L;
	while( curr != 0 )
	{
		int next = g_slink_list[curr].next;
		Free_SL(g_slink_list , curr);
		curr = next;
	}

	L = 0;
	return OK;
}
void Difference_SL( const int La , const int Lb , int& S )
{
	if( OK != InitList_SL(S) )
	{
		return;
	}

	int ps = S;
	int pa = g_slink_list[La].next;
	int pb = g_slink_list[Lb].next;

	while(pa)
	{
		int new_node = Malloc_SL(g_slink_list);
		if( new_node == 0 )
		{
			DestroyList_SL(S);
			S = 0;
			return;
		}
		g_slink_list[ps].next = new_node;
		g_slink_list[new_node].data = g_slink_list[pa].data;
		ps = new_node;

		pa = g_slink_list[pa].next;
	}

	while(pb)
	{
		int p_psame = S;
		int p_same = g_slink_list[S].next; 
		while(p_same)
		{
			if( g_slink_list[p_same].data == g_slink_list[pb].data )
			{
				break;
			} 
			p_psame = p_same;
			p_same = g_slink_list[p_same].next;
		}

		if( p_same )
		{ 
			g_slink_list[p_psame].next = g_slink_list[p_same].next;
			Free_SL(g_slink_list , p_same);
			if( p_same == ps )
			{
				ps = p_psame;
			} 
		}else{
			int new_node = Malloc_SL(g_slink_list);
			if( 0 == new_node )
			{
				DestroyList_SL(S);
				S = 0;
				return;
			}
			g_slink_list[ps].next = new_node;
			g_slink_list[new_node].data = g_slink_list[pb].data;
			g_slink_list[new_node].next = 0;
			ps = new_node;
		} 
		pb = g_slink_list[pb].next;
	}
	
}