Ejemplo n.º 1
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;
		}
	}
}
Ejemplo n.º 2
0
 Status ListInsert_SL( const int L , const int i , const 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 )
		{ 
			int new_p = Malloc_SL(g_slink_list);
			if( new_p == 0) return OVERFLOW;
			g_slink_list[prev_p].next = new_p;
			g_slink_list[new_p].next = curr_p;
			g_slink_list[new_p].data = e;
			return OK;
		}  

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

	if( c == i)
	{
		int new_p = Malloc_SL(g_slink_list);
		if( new_p == 0) return OVERFLOW;
		g_slink_list[prev_p].next = new_p;
		g_slink_list[new_p].data = e;
		g_slink_list[new_p].next = 0;
		return OK;
	}

	return ERROR;
}
Ejemplo n.º 3
0
 Status InitList_SL( int& L )
{
	if( L != 0 )
	{
		return ERROR;
	}

	L = Malloc_SL(g_slink_list);
	if( L == 0 )
	{
		return OVERFLOW;
	}
	return OK;
}
Ejemplo n.º 4
0
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;
	}
	
}