#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#define MAX 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define INIT_LIST_SIZE 100
#define LISTINCREMENT 100
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType* elem;
int length;
int listsize;
}SqList;
Status InitList(SqList* L)
{
L->elem = (ElemType*)malloc(INIT_LIST_SIZE * sizeof(ElemType));
if (!L->elem) exit(OVERFLOW);
L->length = 0;
L->listsize = INIT_LIST_SIZE;
return OK;
}
typedef struct node
{
char s[30];
struct node* next;
int count;
}node, * List;
Status GetLength(SqList* L) //求长度
{
return L->length;
}
Status ListInsert(SqList* L, int i, ElemType e) //插入
{
ElemType* newbase, * q, * p;
if (i<1 || i>L->length + 1) return ERROR;
if (L->length > L->listsize)
{
newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
q = L->elem + i - 1; //q为插入位置
for (p = L->elem + L->length - 1; p >= q; p--)
{
*(p + 1) = *p;
}
*q = e;
++L->length;
return OK;
}
void PrnList(SqList* L) //遍历
{
int i;
for (i = 0; i < (*L).length; i++)
{
if (i == 0)printf("(");
printf(" %d ", L->elem[i]);
if (i == (*L).length - 1)printf(")\n");
}
}
void InsertToDict(List* dict, char* s)
{
int index = (s[0] - 'a');
node* p = (*dict + index)->next;
node* word = (node*)malloc(sizeof(node));
word->count = 1;
strcpy_s(word->s, s);
word->next = NULL;
if (NULL == p)
{
(*dict + index)->next = word;
}
else
{
if (strcmp(s, p->s) < 0)
{
word->next = p;
(*dict + index)->next = word;
return;
}
while (p->next != NULL)
{
if (strcmp(s, p->next->s) < 0)
{
word->next = p->next;
p->next = word;
return;
}
else
p = p->next;
}
if (p->next != word)
p->next = word;
}
}
int cmp(const void* a, const void* b) //List数组中任意两个元素的地址
{
List* i = (List*)a; //强制转换
List* j = (List*)b;
return ((*j)->count - (*i)->count);
}
void FindTop(List dict, List top[], int n)
{
node* p = dict;
node* q = p->next;
node* tmp[100000] = { 0 };
int i, index = 0;
while (p < dict + 26)
{
while (q != NULL)
{
tmp[index++] = q;
q = q->next;
}
p++;
q = p->next;
}
qsort(tmp, index, sizeof(List), cmp);
for (i = 0; i < n; ++i)
top[i] = tmp[i];
}
int FindInDict(node** dict, char* s)
{
int index = (s[0] - 'a');
node* p = ((*dict) + index)->next;
while (p != NULL)
{
if (strcmp((p->s), s) < 0)
p = p->next;
else if (strcmp(p->s, s) > 0)
return 0;
else
{
p->count++;
return 1;
}
}
return 0;
}
int main()
{
int n = 0;
int i=0;
int a;
char ch;
char buf[MAX]; //字符缓冲区
SqList L;
if (InitList(&L))
printf("顺序表已建\n");
FILE* fp;//文件指针
fopen_s(&fp,"D:\\ab.txt","r"); // 打开待读取文件
if (fp == NULL)// 如果失败
{
printf("Failed to open file. Bye\n");
exit(1); // 退出程序
}
// getc(fp)从打开的文件中获取一个字符
printf("打开txt文件并输出其内容:\n");
while ((ch = getc(fp)) != EOF)
putchar(ch);
printf("\n");
int len = 0;
int c_ch = 0;
node* dict = (node*)calloc(26, sizeof(node)); //定义动态数组,存放的是相应单词的头结点
fseek(fp, 0, SEEK_SET);
if (fp != NULL)
{
while ((ch = fgetc(fp)) != EOF)
//注意这里必须(ch=fgetc(fp)),因为!=优先级高,先算!=结果为1,不加()结果ch=1
{
c_ch++;
len = 0;
while (ch >= 'a' && ch <= 'z')
{
buf[len++] = ch;
ch = fgetc(fp);
c_ch++;
}
}
printf("the number of character is:%d\n", c_ch);
}
else
perror("fopen:The_Holy_Bible_Res.txt");
system("pause");
for (i = 0; i < c_ch; i++)
{
fscanf_s(fp, "%d", &a);//
ListInsert(&L, i + 1, a);
}
fclose(fp);// 关闭文件
PrnList(&L);//遍历顺序表
return 0;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/093922134146163.png?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit)