C语言 数据结构 单链表的操作代码

#include<stdio.h>
#include<stdlib.h>
typedef struct {
	int data;
	struct Node* next;
}Node;
Node* initList(){
	Node * list = (Node*)malloc(sizeof(Node));
	list->data = 0;
	list->next = NULL;
	return list;
}

//头插法
void headInsert(Node* list, int data) {
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = list->next;
	list->next = node;
	list->data++;
}
void tailInsert(Node* list, int data) {
	Node* head = list;//保存头节点
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	list = list->next;
	while (list->next) {
		list = list->next;
	}
	list->next = node;
	list = head;
	list->data++;
}
//删除
void delete(Node* list, int data) {
	Node* pre = list;
	Node* current = list->next;
	while (current) {
		if (current->data == data) {
			pre->next = current->next;
			free(current);
			break;//假设没有重复的该数据
		}
		pre = current;
		current = current->next;
	}
	list->data--;
}
//打印
void printList(Node* list) {
	list = list->next;
	while (list) {
		printf("%d ", list->data);
		list = list->next;
	}
	printf("\n");
}
int main() {
	Node* list = initList();
	headInsert(list, 1);
	headInsert(list, 2);
	headInsert(list, 3);
	headInsert(list, 4);
	headInsert(list, 5);
	tailInsert(list, 6);
	tailInsert(list, 7);
	tailInsert(list, 8);
	tailInsert(list, 9);
	printList(list);
	delete(list, 5);
	delete(list, 9);
	delete(list, 6);
	printList(list);
	return 0;
}
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个使用C语言实现的单链表的完整代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 typedef struct Node { int data; struct Node* next; } Node; // 初始化链表 Node* initList() { Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } // 在链表末尾添加节点 void append(Node* head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; Node* cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = newNode; } // 在链表指定位置插入节点 void insert(Node* head, int index, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; Node* cur = head; for (int i = 0; i < index && cur->next != NULL; i++) { cur = cur->next; } newNode->next = cur->next; cur->next = newNode; } // 删除链表指定位置的节点 void deleteNode(Node* head, int index) { if (index < 0 || index >= length(head)) { printf("删除位置不合法\n"); return; } Node* cur = head; for (int i = 0; i < index && cur->next != NULL; i++) { cur = cur->next; } Node* delNode = cur->next; cur->next = delNode->next; free(delNode); } // 获取链表的长度 int length(Node* head) { int len = 0; Node* cur = head->next; while (cur != NULL) { len++; cur = cur->next; } return len; } // 打印链表 void printList(Node* head) { Node* cur = head->next; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } printf("\n"); } int main() { Node* head = initList(); // 初始化链表 append(head, 1); // 在末尾添加节点 append(head, 2); append(head, 3); printList(head); // 打印链表: 1 2 3 insert(head, 1, 4); // 在指定位置插入节点 printList(head); // 打印链表: 1 4 2 3 deleteNode(head, 2); // 删除指定位置的节点 printList(head); // 打印链表: 1 4 3 int len = length(head); // 获取链表的长度 printf("链表长度为: %d\n", len); // 输出:链表长度为: 3 return 0; } ``` 以上是一个基本的单链表实现,包括初始化链表、在链表末尾添加节点、在指定位置插入节点、删除指定位置的节点、获取链表长度等操作。最后通过一个示例展示了如何使用这些操作来对链表进行操作

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值