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 InnerNodethe 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
|