Almacenamiento de compresión de palabras

Almacenamiento de compresión de palabras

Información del tema

Si se utiliza una lista enlazada individualmente para almacenar palabras, se pueden utilizar los siguientes métodos para comprimir el espacio de almacenamiento. Si los sufijos de las dos palabras son iguales, se puede utilizar el mismo espacio de almacenamiento para almacenar el mismo sufijo. Por ejemplo, las palabras originales Str1“loading”y las palabras almacenadas en una lista enlazada individualmente se almacenan en la Str2“being”siguiente forma comprimida.
Inserte la descripción de la imagen aquí
Diseñe un algoritmo eficiente para completar el almacenamiento comprimido de dos listas enlazadas individualmente.

Requisito: Leer el código preestablecido y escribir la función SNODE * ziplist( SNODE * head1, SNODE * head2 )ziplist para encontrar el sufijo común en las dos listas vinculadas. Si hay un sufijo común, comprimir y devolver un puntero al sufijo común; de lo contrario, devuelve NULL.

Pre-código:

#include <stdio.h>
#include <stdlib.h>

typedef struct sdata
{
    
      char  data;
   struct sdata *next;
} SNODE;

void setlink( SNODE *, char * ), outlink( SNODE * );
int listlen( SNODE * );
SNODE * ziplist( SNODE *, SNODE * );
SNODE * findlist( SNODE *, SNODE * );

int main( )
{
    
    	SNODE * head1, * head2, *head;
	char str1[100], str2[100];

	gets( str1 );
	gets( str2 );

	head1 = (SNODE *) malloc( sizeof(SNODE) );
	head2 = (SNODE *) malloc( sizeof(SNODE) );
	head = (SNODE *) malloc( sizeof(SNODE) );
	head->next = head1->next = head2->next = NULL;

	setlink( head1, str1 );
	setlink( head2, str2);

	head->next = ziplist( head1, head2 );

	head->next = findlist( head1, head2 );
	outlink( head );
	return 0;
}

void setlink( SNODE *head, char *str )
{
    
    
	SNODE *p ;

	while ( *str != '\0' )
	{
    
       p  = ( SNODE * ) malloc( sizeof( SNODE ) );
		p->data = *str;
		p->next = NULL;
		str++;
		head->next = p;
		head = p;
	}
	return;
}

void outlink( SNODE * head )
{
    
    	while ( head->next != NULL )
	{
    
    
		printf( "%c", head -> next -> data );
		head = head -> next;
	}
	printf("\n");
	return;
}

int listlen( SNODE * head )
{
    
    
	int len=0;
	while ( head->next != NULL )
	{
    
    
		len ++;
		head = head->next;
	}
	return len;
}

SNODE * findlist( SNODE * head1, SNODE * head2 )
{
    
    
	int m, n;
	SNODE *p1=head1, *p2=head2;

	m = listlen( head1 );
	n = listlen( head2 );
	while  ( m > n )
	{
    
    	p1 = p1->next;
		m--;
	}
	while  ( m < n )
	{
    
    	p2 = p2->next;
		n--;
	}

	while( p1->next != NULL && p1->next != p2->next )
	{
    
    
		p1 = p1->next;
		p2 = p2->next;
	}
	return p1->next;
}
/* Here is waiting for you!     */
/*
  SNODE * ziplist( SNODE * head1, SNODE * head2 )
  {
  }
*/
Entrada de prueba Espere salida
Caso de prueba 1 abcdef
dbdef
def
Caso de prueba 2 ation
abation
ation

responder

SNODE *ziplist(SNODE *head1, SNODE *head2)
{
    
    
    int m, n;
    SNODE *p1 = head1, *p2 = head2;

    m = listlen(head1);
    n = listlen(head2);

    while (m > n)
    {
    
    //如果head1比head2长便将开头的指针向后移动
        p1 = p1->next;
        m--;
    }
    while (m < n)
    {
    
    //如果head2比head1长便将开头的指针向后移动
        p2 = p2->next;
        n--;
    }
    while (p1->next != NULL && p1->next->data != p2->next->data)
    {
    
    //如果未指向终点,且两个字符不相等,则将双起始点都向后移动
        p1 = p1->next;
        p2 = p2->next;
    }

    SNODE *startp1 = p1;
    SNODE *startp2 = p2;
    while (p1->next != NULL)
    {
    
    
        if (p1->next->data == p2->next->data)
        {
    
    
            p1 = p1->next;
            p2 = p2->next;
        }
        else
        {
    
    
            startp1 = p1->next;
            startp2 = p2->next;
            p1 = p1->next;
            p2 = p2->next;
        }
    }
    startp2->next = startp1->next;
    return startp1;
}

Supongo que te gusta

Origin blog.csdn.net/zhj12399/article/details/109223170
Recomendado
Clasificación