C语言动态链表,改写程序,学生的人数由用户输入获得,使用链表实现。用malloc和free动态申请和释放内存
发布网友
发布时间:2022-04-24 17:19
我来回答
共1个回答
热心网友
时间:2023-10-25 13:42
以前做过的类似的一个 你拿去看看改改
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
#define WORDSIZE 32
#define DESTSIZE 512
#define NR 26
struct node_st {
struct node_st *arr[NR];
char *str;
};
static int pos_hash(char ch)
{
return ch - 'a';
}
static int read_file(FILE *fp, char *word, char *dest)
{
char buf[BUFSIZE] = {};
if (fgets(buf, BUFSIZE, fp) == NULL)
return -1;
buf[strlen(buf)-1] = 0;
sscanf(buf, "%s %s", word, dest);
return 0;
}
static void tree_insert(struct node_st **root, const char *word, const char *dest)
{
struct node_st *new;
int i;
if (*root == NULL) {
new = malloc(sizeof(*new));
//if error
for (i = 0; i < NR; i++)
(new->arr)[i] = NULL;
new->str = NULL;
*root = new;
}
if (*word == '\0') {
(*root)->str = strp(dest);
return;
}
tree_insert(&((*root)->arr)[pos_hash(*word)], word+1, dest);
}
static char *tree_search(struct node_st *root, char *word)
{
if (root == NULL)
return NULL;
if (*word == '\0')
return root->str;
tree_search((root->arr)[pos_hash(*word)], word+1);
}
int main(int argc, char **argv)
{
FILE *fp;
struct node_st *root = NULL;
char word[WORDSIZE] = {};
char dest[DESTSIZE] = {};
char *ret;
if (argc < 3)
return 1;
fp = fopen(argv[1], "r");
//if error
while (1) {
if (read_file(fp, word, dest) < 0)
break;
tree_insert(&root, word, dest);
memset(word, '\0', WORDSIZE);
memset(dest, '\0', DESTSIZE);
}
#if 1
if ((ret = tree_search(root, argv[2])) == NULL)
printf("the word is wrong\n");
else
printf("%s: %s\n", argv[2], ret);
#endif
return 0;
}