单链表的创建、遍历、测长、插入、删除、访问、排序、逆序

马上要找工作了,最近把数据结构复习了一下,从单链表开始,一点一点巩固。解析的内容太长就省了,直接贴出代码,需要说明的一点是,下面创建的单链表以及相关操作都是带头结点的。

typedef struct node {
	int data;
	struct node *next;
}Node,*PNode;

PNode createList(int n)
{
	int value;
	PNode pHead = (PNode)malloc(sizeof(Node));
	PNode pTail = pHead;
	pTail->next = NULL;
	for (int i = 1;i <= n;++i)
	{
		printf("请输入第%d个节点的值:", i);
		scanf_s("%d", &value);

		PNode pNew = (PNode)malloc(sizeof(Node));
		pNew->data = value;
		pTail->next = pNew;
		pTail = pNew;
		pTail->next = NULL;
	}
	return pHead;
}

void traverse(PNode pHead)
{
	printf("遍历结果为:\n");
	PNode pTr = pHead;
	while (pTr->next != NULL)
	{
		printf("uuu %d\n", pTr->next->data);
		pTr = pTr->next;
	}
	printf("\n");
}

int length(PNode pHead)
{
	PNode pLen = pHead;
	int len = 0;
	while (pLen->next != NULL)
	{
		pLen = pLen->next;
		len++;
	}
	return len;
}

int insert(PNode pHead, int pos, int val)
{
	PNode pIns = pHead;
	PNode pLen = pHead;
	int len = 0;
	while (pLen->next != NULL){
		pLen = pLen->next;
		len++;
	}
	if (pos < 1 || pos >len + 1){
		printf("insert failed!\n");
		return -1;
	}
	else {
		for (int i = 1;i < pos;++i)
			pIns = pIns->next;
		PNode pNew = (PNode)malloc(sizeof(Node));
		pNew->data = val;
		pNew->next = pIns->next;
		pIns->next = pNew;
	}
	return 0;
}

int del(PNode pHead, int pos)
{
	PNode pLen = pHead;
	int len = 0;
	while (pLen->next != NULL) {
		pLen = pLen->next;
		len++;
	}
	if (pos < 1 || pos>len) {
		return -1;
	}
	else {
		PNode pDel = pHead;
		for (int i = 1;i < pos;++i)
			pDel = pDel->next;
		if (pos == len) {
			free(pDel->next);
			pDel->next = NULL;
		}
		else {
			PNode pNext = pDel->next->next;
			free(pDel->next);
			pDel->next = pNext;
		}
	}

	return 0;
}

PNode locate(PNode pHead, int value)
{
	PNode p = pHead->next;
	while (p&&p->data != value)
		p = p->next;

	return p;
}

PNode get(PNode pHead, int k)
{
	PNode p = pHead;
	for (int i = 1;i <= k;++i)
		p = p->next;
	return p;
}

bool isEmpty(PNode pHead)
{
	if (pHead->next == NULL)
		return true;
	else
		return false;
}

PNode sort(PNode pHead)
{
	PNode p;
	int n, temp;
	n = length(pHead);
	if (pHead == NULL || pHead->next == NULL)
		return pHead;
	p = pHead;
	for (int j = 1;j < n;++j) {
		p = pHead->next;
		for (int i = 0;i < n - j;++i) {
			if (p->data > p->next->data) {
				temp = p->data;
				p->data = p->next->data;
				p->next->data = temp;
			}
			p = p->next;
		}
	}
	return pHead;
}

PNode reverse(PNode pHead)
{
	PNode p, q;
	p = pHead->next->next;//存储第二个结点
	while (p->next != NULL) {
		q = p->next;		//记录要移动的结点
		p->next = q->next;	 //把该结点从原链表中移除
		q->next = pHead->next->next;//把该结点连接到pHead之后
		pHead->next->next = q;
	}
	p->next = pHead->next;	//把pHead移动到新表尾,此时链表成环
	pHead->next = p->next->next;//找到移动完之后的新pHead
	p->next->next = NULL;	//断开环
	return pHead;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值