IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> C语言单词管理 -> 正文阅读

[数据结构与算法]C语言单词管理

C语言单词管理

留学生作业,C语言实现单词管理,要求单词以链表形式存储,每个单词的字母以子链表存储,即实现链表的链表形式。在链表上实现增删查改排序输出等操作。

Notice:
Red text indicates your entries in the console, your entries will not
actually be red.
White text indicates the output of your program. 

task

A list of lists must be implemented, which can be sorted according to various criteria.
An entry in the outer list represents one word. A word consists of letters, so an entry in the inner list should represent one letter. Both lists are simply linked and each refer to the next entry in the list.
The structs of the lists are given:

typedef struct innernode {
  char letter;
  struct innernode* next;
} InnerNode;

typedef struct node {
  InnerNode* word;
  struct node* next;
} Node;

Read the entire information before starting the implementation.
Program
Write a console application that displays a menu. After each executed command, the menu should be displayed again until the program is terminated.
The following commands should be supported:
? a (append), a new word is read in and added to the end of the list (see example).
? i (insert), a new word and an index at which this is to be inserted are read in and the word is inserted into the list accordingly (see example).
? d (delete), an index is read in and its entry is deleted from the list (see example).
? s (sort), the list should be sorted alphabetically or according to the length of the words in ascending or descending order. Two further entries are required for this (see example).
? p (print), the list should be output in the given order. Each inner node should output its letter and outer nodes are separated with spaces (see example). If the list is empty, “empty list” should be output.
? x (exit), the program is terminated and all memory is released again.
Note: You can temporarily use a string to simplify the reading in of the input and you can assume that no more than 20 characters per word are entered. Then the string has to be converted into a list of char.
Note that when sorting, as well as when inserting and deleting, the first element of the list can change. So don’t headforget to update the variable.
General information on inputs:
Spaces and line breaks should be ignored. Rest assured that valid characters are entered when characters are expected and numbers are entered when numbers are expected. You can also assume that the indices entered are in the valid range of the list.
sort by
To sort the outer list, implement a sorting algorithm of your choice. For example, Selection Sort, Insertion Sort or Bubble Sort.
To sortdo this, use a function that accepts three parameters:
? Node* head the pointer to the first list element.
? int (compare)(InnerNode, InnerNode*) a function pointer that compares two inner lists (see below).
? int descending if set to 0 it should be sorted in ascending order, otherwise in descending order.
It is advisable to return the list pointer as a return value so that it can be used Nodein the calling function.
The function pointer allows us to use the same code for sorting, even if we sort according to different criteria. A comparefunction is passed that takes two values (in our case InnerNode
the first element of an inner list) and returns an integer value:
? the return value is negative if the first element is smaller than the second.
? the return value is positive if the first element is larger than the second.
? and the return value is 0 if both elements are the same size.
For your reference, a compare function, which should be used for alphabetical sorting, is already provided:

  int compare_alphabethically(InnerNode* m, InnerNode* n){
    int valueM = m?m->letter:0; // ASCII value of the letter
    int valueN = n?n->letter:0; // or 0 when node is NULL
    int diff = valueM - valueN; // indicates which is the higher value
    if(diff==0){ // on equal value
      if(m!=NULL && n!=NULL){ // if both lists have another entry
        // continue recursively
        return compare_alphabethically(m->next, n->next);
      }
    }
    return diff;
  }

Note: The function compares ASCII values, so in ascending order first capital letters and then lower case letters are in the order. If two letters are the same, the next two letters are compared until one of the words ends.
Similarly, implement a function compare_lengththat returns which of the two words is longer.
These two functions are to be passed to the sorting function and called in it to compare the inner lists.
If two elements have the same value (same length or same letters), they can be next to each other after sorting in any order.

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

typedef struct innode {
    char letter;
    struct innode* next;
} InnerNode;

typedef struct node {
    InnerNode* word;
    struct node* next;
} Node;

int compare_alphabethically(InnerNode* m, InnerNode* n){
    int valueM = m?m->letter:0; // ASCII value of the letter
    int valueN = n?n->letter:0; // or 0 when node is NULL
    int diff = valueM - valueN; // indicates which is the higher value
    if(diff==0){ // on equal value
        if(m!=NULL && n!=NULL){ // if both lists have another entry
            // continue recursively
            return compare_alphabethically(m->next, n->next);
        }
    }
    return diff;
}

源码下载

链接:https://pan.baidu.com/s/127IleY9fwPs0GbLdgVosfQ
提取码:1111

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-12-13 13:06:58  更:2021-12-13 13:09:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 16:27:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码