7.15day4

本文探讨了LeetCode中的链表问题,包括两两交换链表节点的指针处理,删除链表倒数第N个节点的策略,以及如何判断和找到环形链表的入口。关键在于理解和管理好指针,避免空指针异常。

24. 两两交换链表中的节点 - 力扣(Leetcode)

两两交换节点 需要考虑交换节点后节点的先后指向问题和标兵指针的指向问题

先考虑单个节点问题

对head进行特殊处理

 需要讨论指针指向问题和指针跟随问题 如图

 

 特别要注意空指针问题

19. 删除链表的倒数第 N 个结点 - 力扣(Leetcode)

分情况处理问题 注意空指针报错

面试题 02.07. 链表相交 - 力扣(Leetcode)

暴力解决

 

142. 环形链表 II - 力扣(Leetcode)

分为两部分

        1判断是否有环:

         主要是如果有环 快慢指针在环中必相遇

      2有换就用相遇指针和头指针在环型链表中相遇

   

相遇指针和头指针在环型链表中相遇为环的入口指针

算法大抵为:

 

一定要管理好指针不为空!!!!!!!

11-29
![](https://cdn.luogu.com.cn/upload/image_hosting/unknowndomain/2025/11/28/IMG20251128143506.jpg) ### 题目重述 利用消息队列实现一个简单的客户端/服务器应用:多个客户端进程可并发地向服务器进程发送消息,服务器收到后向每个客户端发送回执。例如,客户端(PID=1234)发送 `"hello"`,服务器回执 `"receipt of<hello> to Process 1234"`。 要求编写服务器和客户端程序,使用消息队列进行通信。 --- ### 给出答案(答案为带注释的代码块) #### 服务端程序:`server.c` ```c #include <stdio.h> #include <stdlib.h> #include <sys/msg.h> #include <string.h> #include <unistd.h> #define SERVER_KEY 1000 #define MAX_MSG_SIZE 128 // 消息结构体 struct msg_buffer { long mtype; // 消息类型 long client_pid; // 客户端 PID char text[MAX_MSG_SIZE]; // 消息正文 }; int main() { key_t key = SERVER_KEY; int msgid = msgget(key, 0666 | IPC_CREAT); if (msgid == -1) { perror("msgget failed"); exit(1); } printf("Server started, waiting for messages...\n"); struct msg_buffer msg; while (1) { // 接收任意类型为1的消息(来自客户端) if (msgrcv(msgid, &msg, sizeof(long) + MAX_MSG_SIZE, 1, 0) == -1) { perror("msgrcv failed"); break; } printf("Server received from PID %ld: %s\n", msg.client_pid, msg.text); // 构造回执消息 char receipt[128]; snprintf(receipt, sizeof(receipt), "receipt of<%s> to Process %ld", msg.text, msg.client_pid); // 根据客户端 PID 查找其专属消息队列 int client_msgid = msgget((key_t)(msg.client_pid % 65535), 0666); if (client_msgid != -1) { struct msg_buffer reply; reply.mtype = 1; // 回复类型 strncpy(reply.text, receipt, MAX_MSG_SIZE); msgsnd(client_msgid, &reply, strlen(reply.text) + 1, 0); } else { printf("No reply queue for PID %ld\n", msg.client_pid); } } // 清理消息队列 msgctl(msgid, IPC_RMID, NULL); return 0; } ``` --- #### 客户端程序:`client.c` ```c #include <stdio.h> #include <stdlib.h> #include <sys/msg.h> #include <string.h> #include <unistd.h> #define SERVER_KEY 1000 #define MAX_MSG_SIZE 128 // 消息结构体 struct msg_buffer { long mtype; long client_pid; char text[MAX_MSG_SIZE]; }; int main() { // 创建自己的消息队列(基于 PID) key_t client_key = getpid() % 65535; int client_msgid = msgget(client_key, 0666 | IPC_CREAT); if (client_msgid == -1) { perror("msgget (client)"); exit(1); } // 连接服务器消息队列 int server_msgid = msgget(SERVER_KEY, 0666); if (server_msgid == -1) { perror("msgget (server)"); exit(1); } struct msg_buffer msg; msg.mtype = 1; // 类型固定为1 msg.client_pid = getpid(); // 记录自身 PID strcpy(msg.text, "hello"); // 发送内容 // 发送到服务器 if (msgsnd(server_msgid, &msg, sizeof(long) + MAX_MSG_SIZE, 0) == -1) { perror("msgsnd"); exit(1); } printf("Client %ld sent: hello\n", msg.client_pid); // 接收回执 struct msg_buffer reply; if (msgrcv(client_msgid, &reply, MAX_MSG_SIZE, 1, 0) == -1) { perror("msgrcv (reply)"); exit(1); } printf("Client received: %s\n", reply.text); // 删除自己的消息队列 msgctl(client_msgid, IPC_RMID, NULL); return 0; } ``` --- ### 代码解析 1. **服务器逻辑**:创建一个全局消息队列(`SERVER_KEY`),持续监听类型为1的消息。提取客户端PID和消息内容,生成回执,并通过客户端独立的消息队列返回响应。 2. **客户端逻辑**:基于自身PID创建私有消息队列用于接收回执;向服务器公共队列发送包含自身PID和消息内容的数据包;随后等待并打印回执。 3. **通信机制**:使用 `msgget` 按键获取队列ID,`msgsnd` 发送消息,`msgrcv` 接收指定类型消息,`msgctl` 控制资源释放。 4. **多客户端支持**:每个客户端使用其PID模值得到唯一键建立独立接收通道,确保服务器能准确路由回执。 --- ### 知识点(列出该代码中遇到的知识点) - **消息队列(Message Queue)**:内核级进程间通信机制,支持多进程异步收发结构化消息,通过键值定位队列。 - **消息类型(mtype)**:用于过滤接收特定类型消息,实现多路复用或优先级调度。 - **IPC资源标识**:`msgget` 使用 `ftok` 或显式键生成唯一ID,不同进程可通过相同键访问同一资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值