数据结构NOJ006LOCATE操作

不同于市面上的用c++写的noj,此文章由c写,符合在校生需求,且附有注释,且看可可我慢慢更新。

相信这周很多同学已经做完第6题了,可可做慢了,私密马赛。

思路:找到要插入的位置和要插入的数据,先删除原数据再插入。通过对pre进行比较来找到要插入的位置。

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
	char data;
	int freq;
	struct node* next, * pre;
}Lnode,*Linklist;

void CREAT(Linklist head,int DNodeCount)//创建双向循环链表 
{
	char temp;
	int i;
	Linklist tail;
	tail = head;
	for (i = 0; i < DNodeCount; i++)
	{
		Linklist p = (Linklist)malloc(sizeof(Lnode));
		scanf(" %c", &temp);//因为是字符型,所以要排除空格,在%c前加个空格
		p->data = temp;
		p->freq = 0;
		p->pre = tail;
		tail->next = p;
		tail = p;
	}
	tail->next = head;
	head->pre = tail;
}

void LOCATE(Linklist head,char accessData)
{
	Linklist temp = head->next;
	Linklist p, access;
	p = head->next;
	while (temp!= head)//遍历链表
	{
		while (temp->data != accessData)//temp的data不是所找就向后
		{
			temp = temp->next;
		}
		access = temp;//找到了
		access->freq++;
		while (p->freq >= temp->freq)//找到该插入的位置
		{
			p = p->next;
		}
		access->next->pre = access->pre;
		temp->pre->next = temp->next;
		temp = temp->next;
		access->pre = p->pre;
		access->next = p;
		p->pre->next = access;
		p->pre = access;
		break;
	}
}
int main()
{
	int accessCount, DNodeCount;
	int i;
	char accessData;
	Linklist head;
	head= (Linklist)malloc(sizeof(Lnode));
	head->next = NULL;
	head->freq = 0;
	head->pre = NULL;
	scanf("%d %d", &DNodeCount, &accessCount);
	CREAT(head, DNodeCount);
	for (i = 0; i < accessCount; i++)
	{
		scanf(" %c", &accessData);
		LOCATE(head, accessData);
	}
	Linklist temp = head->next;
	for (i = 0; i < DNodeCount; i++)
	{
		printf("%c ", temp->data);
		temp = temp->next;
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值