c语言刷 设计题合计
355. 设计推特
#define MAX_LEN 512 struct User {
int userId;
int followee[MAX_LEN]; // 散列表,0/1,1表示这个user被关注
struct User *next;
}; struct Tweet {
int userId;
int tweetId;
struct Tweet *next;
}; typedef struct {
struct User *user;
struct Tweet *tweet
} Twitter; Twitter* twitterCreate()
{
Twitter* obj = (Twitter*)malloc(sizeof(Twitter));
obj->user = (struct User*)malloc(sizeof(struct User));
obj->tweet = (struct Tweet*)malloc(sizeof(struct Tweet));
obj->user->next = NULL;
memset(obj->user->followee, 0, sizeof(int) * MAX_LEN);
obj->tweet->next = NULL;
return obj;
} /*
* 注意:
* 这里只给推文这个结构体增加了userId,但是没有给用户这个结构体加,
* 所以后续用户这个结构体可能会得到user是NULL,但是不能返回空
*/
void twitterPostTweet(Twitter* obj, int userId, int tweetId)
{
struct Tweet *node = (struct Tweet *)malloc(sizeof(struct Tweet));
node->userId = userId;
node->tweetId = tweetId;
// 头插法
node->next = obj->tweet->next;
obj->tweet->next = node;
} int* twitterGetNewsFeed(Twitter* obj, int userId, int* retSize)
{
struct User *user = obj->user->next;
struct Tweet *tweet = obj->tweet->next;
int *res = (int *)malloc(sizeof(int) * 10);
*retSize = 0;
while (user != NULL && user->userId != userId) {
user = user->next;
}
if (user == NULL) {
// 还是要遍历推文结构体
while (tweet != NULL && (*retSize < 10)) {
if (tweet->userId == userId) {
res[(*retSize)++] = tweet->tweetId;
}
tweet = tweet->next;
}
return res;
} while (tweet != NULL && (*retSize < 10)) {
if (tweet->userId == userId || user->followee[tweet->userId] == 1) {
res[(*retSize)++] = tweet->tweetId;
}
tweet = tweet->next;
}
return res;
} void twitterFollow(Twitter* obj, int followerId, int followeeId)
{
struct User *user = obj->user;
// 查询用户链表中是否有这个用户节点
while (user->next != NULL && user->userId != followerId) {
printf("loop \n");
user = user->next;
}
// 有:设置标志位为1
if (user->userId == followerId) {
user->followee[followeeId] = 1;
} else {
// 没有:新建一个节点
struct User *node = (struct User *)malloc(sizeof(struct User));
node->userId = followerId;
node->followee[followeeId] = 1;
node->next = obj->user->next;
obj->user->next = node;
}
} void twitterUnfollow(Twitter* obj, int followerId, int followeeId)
{
struct User *user = obj->user;
while (user->next != NULL && user->userId != followerId) {
user = user->next;
}
if (user->userId == followerId) {
user->followee[followeeId] = 0;
}
} void twitterFree(Twitter* obj)
{
free(obj->user);
free(obj->tweet);
free(obj);
} /**
* Your Twitter struct will be instantiated and called as such:
* Twitter* obj = twitterCreate();
* twitterPostTweet(obj, userId, tweetId); * int* param_2 = twitterGetNewsFeed(obj, userId, retSize); * twitterFollow(obj, followerId, followeeId); * twitterUnfollow(obj, followerId, followeeId); * twitterFree(obj);
*/
c语言刷 设计题合计的更多相关文章
- 用PHP语言刷OJ题
平常在学校都是用C,C++,Java来刷OJ题,把AC的题用不同的语言再AC一次,基本相当于翻译而已.看到学校的OJ支持提交PHP代码,于是尝试了一下. 首先,得会使用PHP,但是你如果在看这篇博客, ...
- c语言刷 队列题记录
622. 设计循环队列 https://blog.csdn.net/Galaxy_n/article/details/115978544 typedef struct { int *arrs; int ...
- C语言刷数组题记录
讲解:https://mp.weixin.qq.com/s/weyitJcVHBgFtSc19cbPdw 二分法: 704. 二分查找 int search(int* nums, int numsSi ...
- c语言刷 链表题记录
61. 旋转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode * ...
- c语言刷 DFS题记录
144. 二叉树的前序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeN ...
- 计算机二级C语言选择题错题知识点记录。
计算机二级C语言选择题错题知识点记录. 1,在数据流图中,用标有名字的箭头表示数据流.在程序流程图中,用标有名字的箭头表示控制流. 2,结构化程序设计的基本原则:自顶向下,逐步求精,模块化,限制使用g ...
- 学生管理系统-火车订票系统 c语言课程设计
概要: C 语言课程设计一---学生管理系统 使使用 C 语言实现学生管理系统.系统实现对学生的基本信息和考试成绩的 管理.采用终端命令界面,作为系统的输入输出界面.采用文件作为信息存储介质. 功能描 ...
- C语言课程设计—图书管理系统
这是本人大一第二学期初C语言课程设计的作品,嘿嘿,本来以为已经找不到原稿了,今天无意中居然在QQ网络硬盘中找到了当初的teta版,公布于此,以作纪念. C源码例如以下: #include<std ...
- Java语言课程设计——博客作业教学数据分析系统(201521123107 张翔)
#Java语言课程设计--博客作业教学数据分析系统(个人博客) 1.团队课程设计博客链接 [博客作业教学数据分析系统(From:网络五条狗)](http://www.cnblogs.com/fanta ...
随机推荐
- python封装函数到模块
导入整个模块: import 模块名 导入特定函数: from module_name import function_name 通过逗号可以分割函数名,如果需要导入多个则 from a import ...
- jsp 中的绝对路径和相对路径 ./ 和 ../的区别?
原文地址! https://www.cnblogs.com/brucetie/p/4109913.html 1. 相对路径 相对路径,当前的文件,以根目录为基准,相对于另一个文件的位置. 2.绝对路径 ...
- LeetCode673
LeetCode每日一题2021.9.20 LeetCode673. 最长递增子序列的个数 思路 在最长上升子序列的转移时,维护一个 cnt 数组,表示 以 i 结尾的最长上升子序列个数 f[i] 表 ...
- ApacheCN 数据科学译文集 2020.8
协议:CC BY-NC-SA 4.0 不要担心自己的形象,只关心如何实现目标.--<原则>,生活原则 2.3.c 在线阅读 ApacheCN 面试求职交流群 724187166 Apach ...
- Hive安装教程
Hive的安装和使用 我的版本: JAVA_HOME=/usr/local/soft/jdk1.8.0_171 HADOOP_HOME=/usr/local/soft/hadoop-2.7.6 HIV ...
- Windows安装RabbitMQ过程及相关问题
一.下载 1.首先需要下载erlang,下载地址:http://www.erlang.org/downloads 2.其次需要下载RabbitMQ,下载地址:https://www.rabbitmq. ...
- CSS:第1课
CSS选择器有:id选择器.派生选择器 1.id选择器 id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. id 选择器以 "#" 来定义. #red {colo ...
- Java线程--ForkJoinPool使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11871099.html Java线程--ForkJoinPool使用 简单解释下: Fork是 ...
- TestNG--@Factory
原文地址:http://blog.csdn.net/wanghantong TestNg的@Factory注解从字面意思上来讲就是采用工厂的方法来创建测试数据并配合完成测试 其主要应对的场景是:对于某 ...
- UITextView模拟UITextField 设置Placeholder属性 --董鑫
由于最近有用到输入框,刚开始考虑的是UITextField,因为它在没有输入的时候可以有提示的Placeholder更能,很人性化,但UITextField只能单行输入,不能跳行,对于一些强迫症的亲来 ...