C语言之单链表(增删改查逆)

单链表的增删该查逆

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

#define FALSE 0
#define TRUE  1

typedef struct _node   //创建结构体
{
	int data;
	struct _node *next;   //创建一个结构体指针
}Node;

int head_insert(Node **h,int data)  //头插法
{
	if(h == NULL)
	{
		return 0;
	}
	
	Node *node = (Node *)malloc(sizeof(Node) / sizeof(char));  //创建一个新的节点
	if(node == NULL)
	{
		return 0;
	}
	
	node->data = data;   //头插法关键代码
	node->next = *h;
	*h = node;
	
	return 1;
	
}

int last_insert(Node **h,int data)
{
	if(h == NULL)
	{
		return FALSE;
	}
	Node *node = (Node *)malloc(sizeof(Node) / sizeof(char));    //创建一个新的节点
	if(node == NULL)   //判断节点是否建立成功
	{
		return FALSE;
	}
	
	node->data = data;    //新节点的初始化
	node->next = NULL;
	
	Node *tmp = *h;
	if(tmp == NULL)   //判断链表是否为空
	{
		*h = node;
	}
	else
	{
		while(tmp->next)   //尾插法关键代码
		{
			tmp = tmp->next;
		}
		tmp->next = node;
		
	}
	
	return TRUE;
}

void Display(Node *h)  //打印链表值的函数
{
	if(h == NULL)
	{
		return;
	}
	int count = 0;
	while(h)
	{
		if(count++ % 4 == 0)
		{
			printf("\n");
		}
		printf("%8d",h->data);
		h = h->next;
	}
	printf("\n");
	
}

int pos_insert(Node **h,int pos,int data)   //随机插入链表
{
	if(h == NULL || pos < 1)
	{
		return FALSE;
	}
	Node *node = (Node *)malloc(sizeof(Node) / sizeof(char));   //创建一个新的节点
	if(node == NULL)
	{
		return FALSE;
	}
	node->data = data;
	node->next = NULL;
	
	Node *tmp = *h;
	if(*h == NULL)   //判断链表是否为空
	{
		if(pos != 1)
		{
			printf("该表为空表,无法插入数据\n");
			return FALSE;
		}
		node->next = NULL;	//如果输入插入的为值是1,则直接插在空链表的后面
		*h = node;
	}
	else                   //随机插入关键代码
	{
		if(pos == 1)
		{
			node->next = tmp->next;
			tmp->next = node;
		}
		else
		{
			int i;
			for(i = 0;i < pos - 2;i++)
			{
				if(!tmp)
				{
					printf("插入的数越界\n");
					return FALSE;
					
				}
				tmp = tmp->next;
			}
			node->next = tmp->next;
			tmp->next = node;
		}
	}
	
	return TRUE;
}

int Delete_pos(Node **h,int pos)           //随机删除链表的节点
{
	if(h == NULL || *h == NULL || pos < 1)
	{
		return FALSE;
	}
	Node *tmp = *h;
	if(pos == 1)
	{
		*h = tmp->next;
		free(tmp);
	}
	else                           随机删除关键代码
	{
		int i;
		for(i = 0;i < pos - 2;i++)
		{
			if(tmp->next == NULL)
			{
				printf("删除的数越界\n");
				return FALSE;
			}
			tmp = tmp->next;
		}
		Node *p = tmp->next;
		tmp->next = p->next;
		free(p);
	}
	
	return TRUE;
}

int Reverse_list(Node **h)               //链表逆序
{
	if(h == NULL || *h == NULL || (*h)->next == NULL)
	{
		return FALSE;
	}
	
	Node *pre = *h;
	Node *cur = (*h)->next;
	Node *tmp;
	
	while(cur)
	{
		tmp = cur->next;
		cur->next = pre;
		pre = cur;
		cur = tmp;
	}
	
	(*h)->next = NULL;
	*h = pre;
	
	return TRUE;
}

int Clean_List(Node *h)     //删除节点
{
	if(h == NULL)
	{
		return FALSE;
	}
	while(h->next)
	{	
		pos_delete(h,1);
	}
	
	return TRUE;
}

int main()
{
	Node *head = NULL;
	int i;
	
	for(i = 0; i < 10;i++)
	{
		//head_insert(&head,i);
		last_insert(&head,i);
	}
	//pos_insert(&head,10,100);
	//Delete_pos(&head,5);
	Display(head);
	Reverse_list(&head);
	Display(head);
	
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 C 语言单链表增删改查代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct Node { int data; struct Node* next; }; // 定义链表头节点指针 struct Node* head = NULL; // 插入节点函数 void insertNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (head == NULL) { head = newNode; } else { struct Node* temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } printf("插入节点成功!\n"); } // 删除节点函数 void deleteNode(int data) { if (head == NULL) { printf("链表为空,无法删除!\n"); return; } struct Node* temp = head; if (temp->data == data) { head = head->next; free(temp); printf("删除节点成功!\n"); return; } while (temp->next != NULL && temp->next->data != data) { temp = temp->next; } if (temp->next == NULL) { printf("未找到要删除的节点!\n"); return; } struct Node* deleteNode = temp->next; temp->next = deleteNode->next; free(deleteNode); printf("删除节点成功!\n"); } // 修改节点函数 void modifyNode(int data, int newData) { if (head == NULL) { printf("链表为空,无法修改!\n"); return; } struct Node* temp = head; while (temp != NULL && temp->data != data) { temp = temp->next; } if (temp == NULL) { printf("未找到要修改的节点!\n"); return; } temp->data = newData; printf("修改节点成功!\n"); } // 查找节点函数 void searchNode(int data) { if (head == NULL) { printf("链表为空,无法查找!\n"); return; } struct Node* temp = head; while (temp != NULL && temp->data != data) { temp = temp->next; } if (temp == NULL) { printf("未找到要查找的节点!\n"); return; } printf("找到要查找的节点,数据为:%d\n", temp->data); } // 打印链表函数 void printList() { if (head == NULL) { printf("链表为空,无法打印!\n"); return; } struct Node* temp = head; printf("链表中的节点数据为:"); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { insertNode(1); insertNode(2); insertNode(3); printList(); modifyNode(2, 4); printList(); searchNode(3); deleteNode(2); printList(); return 0; } ``` 以上是单链表的基本操作函数,包括插入节点、删除节点、修改节点、查找节点以及打印链表等功能。你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值