单链表的基本操作大全(增删改查...)-C语言

//单链表的基本操作(增删改查...)
//新创建的链表为:1->7->4->0->9->4->8
//7 find!
//7前插入是:
//1->4->7->4->0->9->4->8
//4 find!
//4后插入是:
//1->4->8->7->4->0->9->4->8
//7 find!
//删除7后是:1->4->8->4->0->9->4->8
//删除后的链表为:1->4->8->4->0->9->4->8
//4 find!
//修改后的链表为:18->4->8->4->0->9->4->8
//反转后的链表为:8->4->9->0->4->8->4->18
//链表的长度为:8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 10
//定义链表
typedef struct node
{
	int data;
	struct node *next;
}Node;
Node *CreatList(Node *head, int length); 
void PrintList(Node *head);
Node *FindList(Node *head, int a);
Node *FindPreNode(Node *head, Node *p);
Node *FindNextNode(Node *head, Node *p);
Node *InsertListEnd(Node *head, int index, int a);
Node *InsertListHead(Node *head, int index, int a);
Node *DeleteListHead(Node *head); 
Node *DeleteListTail(Node *head);
Node *DeleteList(Node *head, int a);
Node *ModifyList(Node *head, int elem, int modify);
int LengthList(Node *head);
Node *InvertList(Node *head);
int Is_Circular(Node *head);
int main()
{
	Node *head = NULL;
	head = CreatList(head, 7);
	printf("新创建的链表为:"); 
	PrintList(head);
	head = InsertListHead(head, 7, 4);
	head = InsertListEnd(head, 4, 8);
	head = DeleteList(head, 7);
	printf("删除后的链表为:"); 
	PrintList(head);
	Node *p = FindList(head, 4);
	head = ModifyList(head, 1, 18);
	printf("修改后的链表为:"); 
	PrintList(head);
	head = InvertList(head); 
	printf("反转后的链表为:"); 
	PrintList(head);
	printf("链表的长度为:%d", LengthList(head)); 
}

//创建链表 
Node *CreatList(Node *head, int length)
{
	Node *temp, *tail;
	for(int i = 1; i <= length; i++)
	{
		temp = (Node *)malloc(sizeof(Node));
		if(temp == NULL)
		{
			printf("malloc error!!\r\n");
			return NULL;
		}
		else
		{
			temp->data = rand() % Max;
			temp->next = NULL;
			if(i == 1)
				head = temp;
			else
				tail->next = temp;
			tail = temp;
		}
	}
	return head;
}

//打印链表
void PrintList(Node *head)
{
	if(head == NULL)
		printf("Empty List! \r\n");
	while(head)
	{
		printf("%d", head->data);
		head = head->next;
		if(head)
			printf("->");
	}
	printf("\r\n");
}

//查询a在链表的位置 
Node *FindList(Node *head, int a)
{
	Node *temp = head;
	if(head == NULL)
	{
		printf("Empty List! \r\n");
		return NULL;
	}
	else
	{
		while(temp)
		{
			if(temp->data == a)
			{
				printf("%d find! \r\n", a);
				return temp;
			}
			temp = temp->next;
		}
		printf("%d not find! \r\n", a);
		return 0;
	}
}

//前驱节点的查找
Node *FindPreNode(Node *head, Node *p)
{
	Node *temp = head;
	if(p == temp)
	{
		printf("%d无前驱节点", p->data);
		return NULL;
	}
	else
	{
		while((temp->next != p) && (temp != NULL))
		{
			temp = temp->next;
		}
		return temp;
	}
}
//后继节点的查找 
Node *FindNextNode(Node *head, Node *p)
{
	Node *temp = head;
	while(temp && (temp != p))
	{
		temp = temp->next;
	}
	temp = temp->next;
	return temp;
}

//在节点后面插入元素
Node *InsertListEnd(Node *head, int index, int a)
{
	Node *pt = (Node *)malloc(sizeof(Node));
	pt->data = a;
	//在特定位置插入 
//	for(int i = 1; i < index; i++)
//	{
//		temp = temp->next;
//	}
//	if(temp->next == NULL)
//	{
//		temp->next = pt;
//		pt->next = NULL;
//	}
//	else
//	{
//		pt->next = temp->next;
//		temp->next = pt;
//	}
	Node *temp = FindList(head, index);
	if(temp->next == NULL)
	{
		temp->next = pt;
		pt->next = NULL;
	}
	else
	{
		//先链接后面 
		pt->next = temp->next;
		temp->next = pt;
		printf("%d后插入是:\r\n", index);
		PrintList(head);
	}
	return head;
}

//在节点前面插入元素
Node *InsertListHead(Node *head, int index, int a)
{
	Node *pt = (Node *)malloc(sizeof(Node));
	pt->data = a;
	//在特定位置插入 
//	if(index == 1)
//	{
//		pt->next = head;
//		head = pt;
//	} 
//	else
//	{
//		for(int i = 1; i < index - 1; i++)
//		{
//			temp = temp->next;
//		}
//		pt->next = temp->next;
//		temp->next = pt;
//	}
	Node *temp = FindList(head, index);
	if(temp == head)
	{
		pt->next = head;
		head = pt;
	}
	else
	{
		//寻找前驱节点 
		Node *pre = FindPreNode(head, temp); 
		pre->next = pt;
		pt->next = temp;
		printf("%d前插入是:\r\n", index);
		PrintList(head); 
	}
	return head;
}
//删除链表头
Node *DeleteListHead(Node *head)
{
	Node *temp = head;
	if(temp == NULL)
	{
		printf("List is Empty! \n");
		return NULL;
	}
	head = temp->next;
	free(temp);
	return head;
}
//删除链表尾
Node *DeleteListTail(Node *head)
{
	Node *temp = head;
	if(temp == NULL)
	{
		printf("List is Empty! \n");
		return NULL;
	}
	while(temp->next != NULL)
	{
		temp = temp->next;
	}
	//寻找前驱节点 
	Node *pre = FindPreNode(head, temp);
	free(temp);
	pre->next = NULL;
	return head;
}
//删除链表中的任意
Node *DeleteList(Node *head, int a)
{
	if(head == NULL)
	{
		printf("List is Empty! \n");
		return NULL;
	}
	Node *temp = FindList(head, a);
	if(temp == NULL)
	{
		printf("No find! \n");
		return NULL;
	}
	if(temp == head)
	{
		head = DeleteListHead(head);
	}
	else if(temp->next == NULL)
	{
		head = DeleteListTail(head);
	}
	else
	{
		Node *pt = head;
		while(pt->next != temp)
		{
			pt = pt->next;
		}
		pt->next = temp->next;
		free(temp);
		printf("删除%d后是:", a);
		PrintList(head); 
	}
	return head;
}
//修改链表元素
Node *ModifyList(Node *head, int elem, int modify)
{
	Node *temp = head;
	while(temp != NULL)
	{
		if(temp->data == elem)
			temp->data = modify;
		temp = temp->next;
	}
	return head;
}
//查看链表长度 
int LengthList(Node *head)
{
	int length = 0;
	while(head)
	{
		length++;
		head = head->next;
	}
	return length;
}
//倒置链表---->分出来 
Node *InvertList(Node *head)
{
	if(head == NULL || head->next == NULL)
		return head;
	else
	{
		Node *tmp = NULL;
		Node *ptmp = NULL;
		while(head != NULL)
		{
			//方法一 
//			tmp = head;
//			head = head->next;
//			tmp->next = ptmp;
//			ptmp = tmp;
			//方法二 
			tmp = head->next; 
			head->next = ptmp;
			ptmp = head;
			head = tmp;
		}
		head = ptmp;
		return head;
	}
} 
//判断链表是否有环----------不理解
/*判断链表有环*/
int Is_Circular(Node *head)
{
        if(head == NULL || head->next == NULL)
		{
                return 0;       
        }
		/*快慢指针,当二者相等时,一定有环*/
        Node *p1 = head;
        Node *p2 = head;
        while(p1 != NULL && p2 != NULL)
		{
            p2 = p2->next; 
            if(p1 == p2)
                return 1;
            p2 = p2->next;
            p1 = p1->next;
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脆订壳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值