美文网首页
单链表操作

单链表操作

作者: 牛顿爱编程 | 来源:发表于2015-11-26 11:37 被阅读418次

键盘输入英语单词的个数 n 及 n 个单词,编一程序,建立一个单向链表,实现:
( 1 )如果单词重复出现,则只在链表上保留一个。
( 2 )除满足( 1 )的要求外。链表结点还应有一个计数域,记录该单词重复出现的次数,然后输出出现次数最多的前 k(k<=n) 个单词。


#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#define M 30
typedef struct LNode{
    char *word;
    int count;
    struct LNode *next;
}LNode;

//判断当前链表中是否已经存在某个单词,如果有,该节点的count++,返回1,否则返回0
int isWordInList(LNode *head, char *word) {
    LNode *node = head->next;
    while (NULL != node && strcmp(word, node->word)) {
        node = node->next;
    }
    if (NULL != node) {
        node->count++;
        return 1;
    }
    return 0;
}

//头插法插入一个新的节点
void insertWordInList(LNode *head, char *word) {
    if (isWordInList(head, word)) {
        return;
    }
    LNode *node = (LNode*)malloc(sizeof(LNode));
    node->word = word;
    node->count = 1;
    node->next = head->next;
    head->next = node;
    head->count++;
}

//输出整个链表
void printList(LNode *head) {
    LNode *node = head->next;
    while (NULL != node) {
        printf("word is %s, and cout is %d\n", node->word, node->count);
        node = node->next;
    }
}

void printListByK(LNode *head, int k) {
    int count = head->count;
    if (k >= count) {
        printList(head);
    } else {
        LNode *node = head->next;
        for (int i = 0; i < k; i++) {
            printf("word is %s, and count is %d", node->word, node->count);
            node = node->next;
        }
    }
}

//交换两个节点的值
void swapTwoLNode(LNode *nodeA, LNode *nodeB) {
    int countA = nodeA->count;
    char *wordA = nodeA->word;
    nodeA->word = nodeB->word;
    nodeA->count = nodeB->count;
    nodeB->word = wordA;
    nodeB->count = countA;
}

//效率更高的方法
void printKthWords(LNode *head, int k) {
    int i = 0;
    //选择排序,把前k的单词从大到小排列
    for (LNode *node = head->next; NULL != node && i < k; node = node->next, i++) {
        LNode *maxNode = node;
        for (LNode *nodeA = node->next; NULL != nodeA; nodeA = nodeA->next) {
            if (nodeA->count > maxNode->count) {
                maxNode = nodeA;
            }
        }
        swapTwoLNode(node, maxNode);
    }
    printListByK(head, k);
}

int main(int argc, const char * argv[]) {
    int n;
    LNode *head;
    while (n <= 0) {
        printf("Please input the count n of words:\n");//保证(n > 0)
        scanf("%d", &n);
    }
    head = (LNode *)malloc(sizeof(LNode));
    head->next = NULL;
    head->count = 0;
    for (int i = 0; i < n; i++) {
        char *word = (char *)malloc(sizeof(char) * M);
        printf("输入第%d个单词:", (i + 1));
        scanf("%s", word);
        insertWordInList(head, word);
    }
    int k;
    while (k <= 0 || k > n) {
        printf("Please input k([1,n]):");//保证k值有效
        scanf("%d", &k);
    }
    printKthWords(head, k);
    return 0;
}

相关文章

  • 线性表的链式存储-单链表

    单链表操作 [x] 单链表的创建(尾插法、头插法) [x] 单链表的查找操作 [x] 单链表的删除操作 [x] 单...

  • 单链表的删除操作

    单链表的删除操作

  • 2018-12-01

    数据结构使用二级指针创建单链表以及单链表的各种操作 //单链表创建 #include #include #incl...

  • 单链表的操作

    单链表代码定义 单链表的操作 初始化单链表 插入结点 注: L为插入的单链表,node为将要插入的结点 前插法 尾...

  • 单链表操作

    简单的单链表操作 下面是操作类

  • 数据结构-单链表学习目录

    1.单链表的基本操作 2.求单链表的长度 3.判断单链表是否为空 4.查找单链表中倒数第K个结点 5.单链表的反转...

  • 链表相关

    总结一下链表相关的操作 单链表节点的定义 实现单向链表的反向 删除单链表的所有节点

  • 数据结构课程 第三周 线性表--链式表

    逻辑次序和物理次序不一定相同 带头结点的单链表 单链表上的操作实现 初始化 判空 单链表销毁 清空单链表(头指针和...

  • Python--单向链表

    单链表python实现 节点实现 单链表操作 头部插入 尾部添加 在index位置插入 删除结点

  • 线性表

    1.线性表 1.1 顺序表(顺序存储) 静态分配 动态分配 1.2 单链表 单链表定义 不带头节点的单链表插入操作...

网友评论

      本文标题:单链表操作

      本文链接:https://www.haomeiwen.com/subject/wvkdhttx.html