📄 list_operate.c
字号:
#include <stdlib.h>
#include <stdio.h>
#include "userconst.h"
static void merge(struct list_node *head, unsigned int element); /* 每次输入后进行整合 */
static void print_list(struct list_node *head); /* 链表输出 */
static struct list_node *find_node(struct list_node *begin_addr, unsigned int element, struct list_node *head); /* 寻找目标节点 */
static struct list_node *compare(struct list_node *head, struct list_node *need_ptr); /* 根据频度进行排序 */
static void insert(struct list_node *new_node, struct list_node *orgin_node); /* 节点的插入 */
struct list_node *init_list(struct list_node *head)
{
unsigned int length = 0;
unsigned int i = 0;
struct list_node *current;
struct list_node *temp_ptr;
current = head;
printf("请您输入链表的长度\n");
scanf("%u", &length);
printf("\n");
head->data = length;
if(0 == length)
{
printf("链表为空\n");
exit(1);
}
printf("请您输入链表中的元素\n");
for(i = 0; i < length; i++)
{
temp_ptr = (struct list_node *)malloc(sizeof(struct list_node));
scanf("%u", &(temp_ptr->data));
temp_ptr->freq = 0;
current->next = temp_ptr;
temp_ptr->prior = current;
temp_ptr->next = head;
head->prior = temp_ptr;
current = current->next;
/*
free(temp_ptr);
*/
}
printf("双向链表建立成功\n");
print_list(head);
return head;
}
void output_list(struct list_node *head, unsigned int element)
{
merge(head, element);
print_list(head);
}
static void merge(struct list_node *head, unsigned int element)
{
struct list_node *temp_ptr;
temp_ptr = head;
while(head != temp_ptr->next)
{
temp_ptr = temp_ptr->next; /* 从头结点或者找到元素的结点后一个作为起始位 */
temp_ptr = find_node(temp_ptr, element, head);
if(NULL == temp_ptr)
{
return;
}
temp_ptr->freq = temp_ptr->freq + 1;
insert(temp_ptr, compare(head, temp_ptr));
}
}
static void print_list(struct list_node *head)
{
unsigned int temp_len;
struct list_node *temp_ptr;
temp_ptr = head;
temp_len = head->data;
if(0 != temp_len)
{
while(head != (temp_ptr->next))
{
temp_ptr = temp_ptr->next;
printf("%u ", temp_ptr->data);
}
printf("\n频数为\n");
temp_ptr = head;
while(head != (temp_ptr->next))
{
temp_ptr = temp_ptr->next;
printf("%u ", temp_ptr->freq);
}
printf("\n");
}
}
static struct list_node *find_node(struct list_node *begin_addr, unsigned int element, struct list_node *head)
{
struct list_node *temp_ptr;
temp_ptr = begin_addr;
while((element != (temp_ptr->data)) && (head != temp_ptr->next)) /* temp_ptr从头结点后一个结点开始 */
{
temp_ptr = temp_ptr->next;
}
if((element != (temp_ptr->data) && head == temp_ptr->next))
{
temp_ptr = NULL; /* 从头到尾没有要求的元素 */
}
return temp_ptr; /* 返回找到元素的结点地址 可以处理多个数字相同情况 */
}
static struct list_node *compare(struct list_node *head, struct list_node *need)
{
struct list_node *ptr;
ptr = head->next;
while(need != ptr)
{
if(need->freq <= ptr->freq) /* 比较频度 */
{
ptr = ptr->next;
}
else
{
return ptr;
}
}
return ptr;
}
static void insert(struct list_node *new_node, struct list_node *orgin_node)
{
if(new_node == orgin_node)
{
return;
}
new_node->prior->next = new_node->next; /* 解决新结点的问题*/
new_node->next->prior = new_node->prior;
orgin_node->prior->next = new_node;
new_node->next = orgin_node;
// new_node->prior = orgin_node->prior->next;
new_node->prior = orgin_node->prior;
// orgin_node->prior = new_node;
orgin_node->prior = new_node;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -