-
Notifications
You must be signed in to change notification settings - Fork 1
/
单词压缩存储.cpp
140 lines (121 loc) · 2.66 KB
/
单词压缩存储.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// main.cpp
// 数据结构
//
// Created by 刘冠中 on 2016/10/18.
// Copyright © 2016年 刘冠中. All rights reserved.
//
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#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 )
{
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--;
}
SNODE *q1 = NULL, *q2 = NULL;
for (; p1 && p1->next != NULL; p1 = p1->next, p2 = p2->next)
{
if (p1->next->data != p2->next->data)
q1 = q2 = NULL;
else if (q1 == NULL)
{
q1 = p1;
q2 = p2;
}
}
if (q1 == NULL)
exit(0);
q2->next = q1->next;
return q1->next;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */