链表

/**
*单链表的建立,查找,插入,删除,逆序
**/

#include <stdio.h>
#include <malloc.h>

typedef struct node { //定义结构体类型headLink
	int num;
	struct node *next;//指针域
} Node;

//创建一个单链表
Node *creat() { //定义函数creat1()
	Node *head, *p;//定义结构指针
	
	int n;
	head = (Node *)malloc(sizeof(Node));//申请头结点空间
	head->next = NULL;//初始化一个空链表
	scanf("%d", &n);

	while (n>0) {
		p = (Node *)malloc(sizeof(Node));//分配第一个结点空间
		p->num =n;
		p->next = head->next;
		head->next = p;//head->p-> NULL
		scanf("%d", &n);
	}
	return head;//返回链表
	
}

//打印节点
void print(Node *head) {
	Node *p;
	p = head->next;
	printf("\n");
	while (p != NULL) { //输出链表
	printf("%d\n", p->num);
		p=p->next;
	}
}

//计算单链表的长度

int lenlink(Node *head)//计算单链表head中的结点个数
{
	Node *p;//定义p为工作指针
	int n=0;
	p = head->next;//保存单链表的头指针,头指针中没有数据,不能计算在其长度中

	while (p != NULL)
	{
		n++;
		p=p->next;//指针后移
	}
	return n;
}

/*在单链表中按照指定的值实现元素的查找
*查找过程挨个元素比较判断是否相等的过程
*/
Node *findnode(Node *head, int n)
{
	Node *p = head->next ;//p指向头结点的下一个结点
	while(p && p->num != n)//链表不为空,同时没有到达指定的结点
		p = p->next;//指针后移

		if(p->num == n)//已找到
			return p;
		else
			return NULL;

}

/*单链表的插入
*函数的功能是在链表head中的第n个结点后插入结点的值为num
*/
Node *insert(Node *head, int pos, int num)
{
	int i = 1;
	Node *pre; //pre为前驱结点
	Node *p;//插入的结点为p
	pre = head;
	
	while (pre->next && i< pos )
	{
		pre = pre->next;//查找第n个位置的前驱结点
		i++;
	}

	
	p = (Node *)malloc(sizeof(Node));
	p->num = num;
	p->next = pre->next;
	pre->next = p;

	return head;
}

/*单链表的删除
*在链表中删除值为num的元素

*/
Node *delete(Node *head, int num)
{
	Node *p, *pre;//pre为前驱结点,p为查找的结点
	p = head->next;//p指向第一个结点

	while(p->num != num) //查找值为num的元素
	{
		pre = p;
		p = p->next;
	}

	if (p->num == num)
	{
		pre->next = p->next;//删除操作,将其前驱next指向其后继
		free(p);//删除以后回收结点
	}
	else
		printf(" not fount the delete num in the link\n");

	return head;
}

void main() {
	Node *head, *p;
	int count;
	int num;
	int pos;

	head = creat();
	printf("**********create the link list*******\n");
	print(head);//调用函数打印节点

	count = lenlink(head);//统计链表中节点的个数
	printf("the linklist length %d\n", count);

	printf("iput find element:");
	scanf("%d", &num);
	p = findnode(head, num);//调用函数查找

	if(p)
		printf("the element find %d\n", p->num);
	else
		printf("don't find\n");
	printf("**********insert a node in link*******\n");
	printf("please input the insert pos\n");
	scanf("%d", &pos);
	printf("please input the insert num\n");
	scanf("%d", &num);

	insert(head, pos, num);
	printf("*********after insert*******\n");
	print(head);//调用函数打印节点

	printf("*********please input the delete num*******\n");
	scanf("%d", &num);
	delete(head, num);
		printf("*********after delete num*******\n");
	print(head);//调用函数打印节点
}


-----------------------------------------------------------------------------

 带有头结点的单链表逆序
设定工作指针p和q,令p指向单链表的首元结点,同时head头结点的指针
域清空,指针q保存p结点的地址,同时p指针后移,把q结点用插入法到
单链表head中,最后返回头结点的指针head

/**
*单链表数据结点逆序
*/
Node *inverse(Node *head)
{
	Node *p, *q;
	p = head->next;
	head->next = NULL;//初始化逆转后的链表头结点指针域为空

	while(p)
	{
		q = p;//q保存当前p的工作指针
		p=p->next;//指针p后移
		q->next = head->next;//q指向的结点插入到head中
		head->next = q;

	}
	return head;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值