最開始用了很原始的數據初始化方法,多謝dskit指點,現在已經改正為哈希字典了。因為在創建新節點的時候浪費時間比較嚴重,所以如果改成用向量會更好一些。
#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];//輸入次數
n=0;//電話號碼個數
LNode *head=0; //指向鏈表頭節點
LNode *front= 0; //指向當前節點的前一位置
LNode *tail= 0; //指向當前遍歷到的節點
scanf("%d\n",&row);
if(row<=0&&row>100000) return 0;
for(i=0; i<row; i++)//將有效輸入存入temp數組

{
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) //新建第一節點

{
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 //新建非第一節點

{
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]) //找到的目標節點值大于temp

{

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

else
{tag=3;break;}
}
else if(tail->data[j]<temp[j]) //目標節點值小于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;} //目標節點值等于temp
}
if(tag==-1)continue; //未找到插入點
else if(tag==1)break; //目標節點值等于temp
else if(tag==0) //目標節點值小于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) //找到的目標節點值大于temp,目標節點為頭結點

{
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) //找到的目標節點值大于temp,目標節點非頭結點

{
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.");
}