链表的基础知识

//一直觉得学计算机归根到底都是算法。试着回忆了一下链表的知识

//单向链表结构
typedef struct node
{
	int info;
	struct node *next;	//	再加类似这样的一条语句就是双向链表了
}node;

//单向链表首部插入结点
node *push_front(node *head,int info)
{
	node* p=(node *)malloc(sizeof(node));
	p->next=head;
	p->data=info;
	return p;
}

//返回链表的第一个元素
int front_element(node *head)
{
	return first(head)->data;
}
node* first(node* head)
{
	return head;
}

//返回链表最后一个元素
int back_element(node* head)
{
	rerurn last(head)->data;
}
node* last(node* head)
{
	node *p=head;
	for (;p->next!=NULL;p=p->next);
	return p;
}

//记录单向链表中结点的个数
int count(node *head)
{
	int numberofnodes=0;
	node *p=head;
	if (p==NULL)
		return p;
	else
	{
		for (;p!=NULL;p=p->next)
			numberofnodes++;
		return numberofnodes;
	}
}

//得到单向链表中数据项出现的频率
int frequency(node *head,int value)
{
	int freq=0;
	node *p=head;
	for (;p!=NULL;p=p->next)
		if (p->data==value)
			freq++;
	return freq;
}

//搜索单向链表中特定数据项
int searchindex(node *head,int s)
{
	int search_status=NOTFOUND;
	int c=0;
	node *p=head;
	for (;p!=NULL;p=p->next)
	{
		c++;
		if (s==p->data)
		{
			search_status=FOUND;
			break;
		}
	}
	if (search_status==FOUND)
		return c;
	else
		return -1;
}

//在单向链表中返回特定结点的地址
node* get_address(node* head,int index)
{
	node *p=head;
	int c=0;
	for (;p!=NULL;p=p->next)
	{
		c++;
		if (c==index)
			break;
	}
	return p;
}

//在单向链表中的特定位置插入结点
node* insert(node *head,int location,int info)
{
	int c=0;
	node *p=head;
	node *r=p;
	node *q=(ndoe*)malloc(sizeof(node));
	if (location < count(head))
	{
		//找出与给定位置匹配的位置
		for (;p!=NULL;p=p->next)
		{
			c++;
			if (c==location)
				break;
		}
		q->next=p->next;
		q->next=q;
		q->data=info;
	}
	return r;
}

//显示单向链表的所有内容
void dispaly(node *head)
{
	node *p=head;
	//让我们遍历到末尾,直到NULL为止
	for(;p!=NULL;p=p->next)
		printf("Value=%d Address %u Next Address %u\n",p->data,p,p->next);
}

//得到单向链表的最大元素,得到最小值则同理
int findmax(node *head)
{
	node *P;
	int max=0;
	p=head;
	max=p->data;
	while (p!=NULL)
	{
		if (p->data > max)
			max=p->data;
		p=p->next;
	}
	return max;
}

//使用给定值编辑特定结点的内容
node* replace(node* head,int location,int info)
{
	int c=0;
	node *p=head;
	node *r=p;
	node *q=(node*)malloc(sizeof(node*));
	if (location<count(head))
	{
		//找到目标结点的位置
		for (;p!=NULL;p=p->next)
		{
			c++;
			if(c==location-1)
				break;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值