数据结构的第一次任务 以下是全部要求

线性表的应用
实验目的:掌握线性表的基本结构和操作方法,培养学生灵活使用表解决实际问题的能力。
实验内容: 键盘输入英语单词的个数n及n个单词,编写程序,建立一个单向链表,实现: (1)如果单词重复出现,则只在链表上保留一个。
(2)除满足(1)的要求外。链表结点还应有一个计数域,记录该单词重复出现的次数,然后输出出现次数最多的前k(k<=n,需键盘输入)个单词。
注:次数并列的情况考虑、不考虑均可。提示:本题链表结点的数据域存放英文单词,可用字符数组表示,单词重复出现时,链表中只保留一个,单词是否相等的判断使用strcmp函数,结点中增设计数域,统计单词重复出现的次数。

是否要求哨兵节点?

#include <stdio.h>
#include <string.h>
typedef struct _Node
{
    char word[30];
    int count;
    struct _Node * next;
}Node;

void AddNode(Node *head,char *word)
{
    Node *p = head;
    while(p->next != NULL)
    {
        if(strcmp(p->next->word,word) == 0)
        {
            p->next->count++;
            return;
        }
        p = p->next;
    }
    Node *q = (Node*)malloc(sizeof(Node));
    q->next= NULL;
    p->next= q;
    q->count = 1;
    strcpy(q->word,word);
}

int main()
{
    Node head;
    head.next = NULL;
    head.count = 0;
    //
    int n;
    printf("输入单词数量:");
    scanf("%d",&n);
    char word[30] = {0};
    for(int i=0;i<n;i++)
    {
        scanf("%s",word);
        AddNode(&head,word);
    }
    Node *p = head.next;
    Node *q = p;
    while(p != NULL)
    {
        if(p->count > q->count)
            q = p;
        p = p->next;
    }
    printf("出现次数最多单词为:%s\n",q->word);
    return 0;
}

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

#define MAX_SIZE 64

typedef struct Node_
{
    char word[MAX_SIZE];
    int count;
    struct Node_ *next;
} Node;

Node *allocNode()
{
    Node *p = (Node *)malloc(sizeof(Node));
    memset(p, 0, sizeof(Node));
    return p;
}

Node *createList()
{
    return allocNode();
}

void destroyList(Node *head)
{
    while (head)
    {
        Node *p = head;
        head = head->next;
        free(p);
    }
}

void addWord(Node *head, const char *word)
{
    Node *pre = head;
    Node *p = head->next;
    while (p && strcmp(p->word, word) != 0)
    {
        pre = p;
        p = p->next;
    }
    if (p)
    {
        p->count++;
    }
    else
    {
        Node *q = allocNode();
        strcpy(q->word, word);
        q->count = 1;
        pre->next = q;
    }
}

void printList(const Node *head, int n)
{
    const Node *p = head->next;
    while (p && --n >= 0)
    {
        printf("%s %d\n", p->word, p->count);
        p = p->next;
    }
}

void sortList(Node *head)
{
    Node *tail = head;
    while (tail)
    {
        int maxCount = 0;
        Node *q_pre = NULL;
        Node *q = NULL;
        Node *p_pre = tail;
        Node *p = tail->next;
        while (p)
        {
            if (p->count > maxCount)
            {
                maxCount = p->count;
                q_pre = p_pre;
                q = p;
            }
            p_pre = p;
            p = p->next;
        }
        if (q && tail != q_pre)
        {
            Node *t = tail->next;
            q_pre->next = q->next;
            tail->next = q;
            q->next = t;
        }
        tail = tail->next;
    }
}

int main()
{
    char word[MAX_SIZE];
    int n;
    printf("Enter the number of words: ");
    scanf("%d", &n);
    printf("Enter %d words: ", n);
    Node *list = createList();
    for (int i = 0; i < n; i++)
    {
        scanf("%s", word);
        addWord(list, word);
    }
    printList(list, n);

    sortList(list);

    int k;
    printf("Enter the number k (k<=n): ");
    scanf("%d", &k);
    printList(list, k);
    return 0;
}
$ gcc -Wall main.c
$ ./a.out
Enter the number of words: 5
Enter 5 words: aa ab aa a a
aa 2
ab 1
a 2
Enter the number k (k<=n): 2
aa 2
a 2