C语言链表问题

杭电ACM1020题。
我思路是用链表实现统计字符串中字符出现的次数。
clipboard.png

clipboard.png
为什么不能AC?
望指教!

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10001

typedef char ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode Position;
typedef PtrToNode List;
struct Node
{
    ElementType Data;
    int Quantity;
    Position Next;
};

List InitList();
Position Find(ElementType X, List L);
Position Last(List L);
int IsLast(Position P);
void Append(List L,ElementType X);
void DisplayList( List L);


int main() {
    int instance;
    char string[MAXSIZE], warp, *cell;
    Position P;
    List L;

    scanf("%d",&instance);
    warp = getchar();
    if( warp == '\n')
    {
        while( instance-- ){
            L = InitList();
            gets(string);
            cell = string;
            while( *cell != '\0'){
                P = Find(*cell,L);
                /*如果List 中有,则Quantity +1,否则在链表尾插入*/
                if( P != NULL )
                {
                    P->Quantity++;
                }
                else
                {
                    Append(L,*cell);
                }
                cell++;
            }
            DisplayList( L );
            printf("\n");
        }
    }
    return 0;
}

List InitList()
{
    List L;

    L = malloc( sizeof(struct Node));
    if( L == NULL)
        exit(0);
    L->Next = NULL;

    return L;
}

Position Find(ElementType X, List L)
{
    Position P;

    P = L->Next;
    while( P != NULL && P->Data != X)
        P = P->Next;
    return P;
}

int IsLast( Position P)
{
    return P->Next == NULL;
}

Position Last( List L)
{
    Position  P;

    if( L == NULL)
        exit(0);
    else
    {
        P = L;
        while( !IsLast( P )){
            P = P->Next;
        }
    }
    return P;
}

void Append( List L, ElementType X)
{
    PtrToNode TmpCell, LastCell;

    if( L == NULL )
        exit(0);
    else{
        TmpCell = malloc( sizeof( struct Node ) );
        if( TmpCell == NULL )
            exit(0);
        else
        {
            LastCell = Last( L );
            TmpCell->Data = X;
            TmpCell->Quantity = 1;
            LastCell->Next = TmpCell;
            TmpCell->Next = NULL;
        }
    }
}

void DisplayList( List L)
{
    if( L == NULL )
        exit(0);
    else
    {
        /*头指针不含元素*/
        L = L->Next;
        while( L != NULL){
            if( L->Quantity > 1 )
                printf("%d",L->Quantity);
            printf("%c",L->Data);
            L = L->Next;
        }
    }
}
阅读 2.3k
1 个回答

泻药。同学你理解错题意了。这题并不是统计总相同字符数,只是相邻的相同字符数。也就是说,ABBAA 应该返回 A2B2A。下面是 AC 的代码:

#include <stdio.h>

int main () {
    int n, cnt;
    char string[10001], *ch;
    scanf("%d", &n);
    if (getchar() == '\n')
        while (n--) {
            gets(string);
            cnt = 0;
            ch = string;
            while (*ch) {
                ++cnt;
                if (*ch != *(ch + 1)) {
                    if (cnt == 1) putchar(*ch); else printf("%d%c", cnt, *ch);
                    cnt = 0;
                }
                ch++;
            }
            putchar('\n');
        }
    return 0;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进