3.从标准输入中读入N(1<N<10000)行以换行符结束且长度不超过2048的字符串,并在输入结束后输出其中最长10行的输入序号、长度和内容。当有多行长度相等的最长行时,输出最先输入的行的信息。
/*个人写的代码如下:*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <malloc.h>
#include <string.h>
#define LEN sizeof(struct line)
/*表示一行字符的结构体*/
struct line
{
unsigned int row;
unsigned int length;
char *content;
struct line *next;
};
{
unsigned int row;
unsigned int length;
char *content;
struct line *next;
};
/*用来删除链表的最后一个结点*/
void delNode(line* p)
{
/*找倒数第二个节点*/
while (p->next->next != NULL)
{
p = p->next;
}
free(p->next);
p->next = NULL;
void delNode(line* p)
{
/*找倒数第二个节点*/
while (p->next->next != NULL)
{
p = p->next;
}
free(p->next);
p->next = NULL;
}
/*
处理该行,是否插入,如果需要,按照字符数目降序的插入,如果链表已经超过10个,
且该行字符数比最后一个还小就直接删除该节点。
*/
int insertNode(line *head, line *temp)
{
/*链表中只有头节点的情况*/
if(head->next == NULL)
{
head->next = temp;
head->row++;
return 0;
}
{
/*链表中只有头节点的情况*/
if(head->next == NULL)
{
head->next = temp;
head->row++;
return 0;
}
line *p = head->next;
line *pre = head;
bool forInsert = false;
while (p != NULL)
{
/*插在第一个和最后一个节点之前的情况*/
if (temp->length > p->length)
{
forInsert = true;
pre->next = temp;
temp->next = p;
pre = temp;
head->row++;
if (head->row > 10)
{
/*把指向新插入的节点的指针传入函数,来删除最后一个节点*/
delNode(temp);
head->row--;
}
break;
}
pre = p;
p = p->next;
}
/*比链表中任何一个节点都小的情况*/
if (!forInsert)
{
if (head->row <= 9)
{
pre->next = temp;
head->row++;
}
else
{
free(temp);
}
}
return 0;
}
line *pre = head;
bool forInsert = false;
while (p != NULL)
{
/*插在第一个和最后一个节点之前的情况*/
if (temp->length > p->length)
{
forInsert = true;
pre->next = temp;
temp->next = p;
pre = temp;
head->row++;
if (head->row > 10)
{
/*把指向新插入的节点的指针传入函数,来删除最后一个节点*/
delNode(temp);
head->row--;
}
break;
}
pre = p;
p = p->next;
}
/*比链表中任何一个节点都小的情况*/
if (!forInsert)
{
if (head->row <= 9)
{
pre->next = temp;
head->row++;
}
else
{
free(temp);
}
}
return 0;
}
int main(void)
{
printf("请输入要输入的行数(范围为[1,10000]):");
line *head = (struct line*)malloc(LEN);
/*用头结点row来存储该链表的个数(不算头结点)*/
head->row = 0;
head->next = NULL;
head->content = NULL;
head->length = 0;
{
printf("请输入要输入的行数(范围为[1,10000]):");
line *head = (struct line*)malloc(LEN);
/*用头结点row来存储该链表的个数(不算头结点)*/
head->row = 0;
head->next = NULL;
head->content = NULL;
head->length = 0;
line *headP = head;
line *temp;
char inputs[6];
int lines;
scanf("%d",&lines);
printf("请输入内容(输入的字符串长度在[0,2048]之间,如果大于2048,则超过2048的字符将被截断):/n");
fflush(stdin);
for(int i=0; i<lines; ++i)
{
fgets(inputs,6,stdin);
int strlenth = strlen(inputs);
if (inputs[strlenth-1] == '/n')
{
inputs[strlenth-1] = '/0';
}
else
{
fflush(stdin);
}
temp = (struct line*)malloc(LEN);
temp->length = strlen(inputs);
temp->row = i + 1;
temp->content = (char*)malloc((temp->length+1) * sizeof(char));
memcpy(temp->content,inputs,temp->length+1);
temp->next = NULL;
insertNode(head,temp);
}
/*输出结果,并释放链表*/
printf("/n/n/n/n输出结果:/n****************************/n");
line* p = head->next;
line* pFree;
while (p != NULL)
{
printf("行号:%d/n",p->row);
printf("长度:%d/n",p->length);
printf("内容:%s/n",p->content);
printf("****************************/n");
pFree = p;
p = p->next;
free(pFree);
}
free(head);
return 0;
}
line *temp;
char inputs[6];
int lines;
scanf("%d",&lines);
printf("请输入内容(输入的字符串长度在[0,2048]之间,如果大于2048,则超过2048的字符将被截断):/n");
fflush(stdin);
for(int i=0; i<lines; ++i)
{
fgets(inputs,6,stdin);
int strlenth = strlen(inputs);
if (inputs[strlenth-1] == '/n')
{
inputs[strlenth-1] = '/0';
}
else
{
fflush(stdin);
}
temp = (struct line*)malloc(LEN);
temp->length = strlen(inputs);
temp->row = i + 1;
temp->content = (char*)malloc((temp->length+1) * sizeof(char));
memcpy(temp->content,inputs,temp->length+1);
temp->next = NULL;
insertNode(head,temp);
}
/*输出结果,并释放链表*/
printf("/n/n/n/n输出结果:/n****************************/n");
line* p = head->next;
line* pFree;
while (p != NULL)
{
printf("行号:%d/n",p->row);
printf("长度:%d/n",p->length);
printf("内容:%s/n",p->content);
printf("****************************/n");
pFree = p;
p = p->next;
free(pFree);
}
free(head);
return 0;
}