最開始用了很原始的數(shù)據(jù)初始化方法,多謝dskit指點(diǎn),現(xiàn)在已經(jīng)改正為哈希字典了。因為在創(chuàng)建新節(jié)點(diǎn)的時候浪費(fèi)時間比較嚴(yán)重,所以如果改成用向量會更好一些。
#include<stdio.h>
#include<stdlib.h>

int map[25]=
{2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9};

typedef struct LNode
{
int data[8];
struct LNode *next;
};
int main()


{
int row,c,tag,i,j,n,temp[7];//輸入次數(shù)
n=0;//電話號碼個數(shù)
LNode *head=0; //指向鏈表頭節(jié)點(diǎn)
LNode *front= 0; //指向當(dāng)前節(jié)點(diǎn)的前一位置
LNode *tail= 0; //指向當(dāng)前遍歷到的節(jié)點(diǎn)
scanf("%d\n",&row);
if(row<=0&&row>100000) return 0;
for(i=0; i<row; i++)//將有效輸入存入temp數(shù)組

{
for(j=0; (c= getchar())&&(c!='\n')&&(j<7); )

{
if(c!='-')

{
if(c>='A'&&c<='Y')temp[j++]= map[c-'A'];
else if(c>='0'&&c<='9')temp[j++]= c-'0';
}
}
if(n==0) //新建第一節(jié)點(diǎn)

{
LNode *p= (LNode *)malloc(sizeof(LNode));
for(int j=0; j<7; j++)
p->data[j]= temp[j];
p->data[7]= 1;
p->next= 0;
head= p;
n++;
continue;
}
else //新建非第一節(jié)點(diǎn)

{
tag= -1;
for(front= head,tail= head; tail!= 0; front= tail,tail= tail->next)

{
for(j=0; j<7; j++)

{
if(tail->data[j]>temp[j]) //找到的目標(biāo)節(jié)點(diǎn)值大于temp

{

if(front==tail)
{tag=2;break;}

else
{tag=3;break;}
}
else if(tail->data[j]<temp[j]) //目標(biāo)節(jié)點(diǎn)值小于temp

{

if(tail->next==0)
{tag= 0;break;}
else break;
}

else if(j==6&&tail->data[j]==temp[j])
{tag=1;tail->data[7]+=1;break;} //目標(biāo)節(jié)點(diǎn)值等于temp
}
if(tag==-1)continue; //未找到插入點(diǎn)
else if(tag==1)break; //目標(biāo)節(jié)點(diǎn)值等于temp
else if(tag==0) //目標(biāo)節(jié)點(diǎn)值小于temp

{
LNode *p= (LNode *)malloc(sizeof(LNode));
for(j=0; j<7; j++)
p->data[j]= temp[j];
p->data[7]= 1;
p->next= 0;
tail->next= p;
n++;
break;
}
else if(tag==2) //找到的目標(biāo)節(jié)點(diǎn)值大于temp,目標(biāo)節(jié)點(diǎn)為頭結(jié)點(diǎn)

{
LNode *p= (LNode *)malloc(sizeof(LNode));
for(j=0; j<7; j++)
p->data[j]= temp[j];
p->data[7]= 1;
p->next= head;
head= p;
n++;
break;
}
else if(tag==3) //找到的目標(biāo)節(jié)點(diǎn)值大于temp,目標(biāo)節(jié)點(diǎn)非頭結(jié)點(diǎn)

{
LNode *p= (LNode *)malloc(sizeof(LNode));
for(j=0; j<7; j++)
p->data[j]= temp[j];
p->data[7]= 1;
p->next= tail;
front->next= p;
n++;
break;
}
}
}
}
tag= 0;
for(tail= head; tail!=0; tail= tail->next) //遍歷鏈表輸出

{
if(tail->data[7]>1)

{
tag= 1;
for(i=0; i<8; i++)

{
if(i==3)printf("-");
if(i==7)printf(" ");
printf("%d",tail->data[i]);
}
printf("\n");
}
}
if(tag==0)printf("No duplicates.");
}