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语言刷 设计题合计的更多相关文章

  1. 用PHP语言刷OJ题

    平常在学校都是用C,C++,Java来刷OJ题,把AC的题用不同的语言再AC一次,基本相当于翻译而已.看到学校的OJ支持提交PHP代码,于是尝试了一下. 首先,得会使用PHP,但是你如果在看这篇博客, ...

  2. c语言刷 队列题记录

    622. 设计循环队列 https://blog.csdn.net/Galaxy_n/article/details/115978544 typedef struct { int *arrs; int ...

  3. C语言刷数组题记录

    讲解:https://mp.weixin.qq.com/s/weyitJcVHBgFtSc19cbPdw 二分法: 704. 二分查找 int search(int* nums, int numsSi ...

  4. c语言刷 链表题记录

    61. 旋转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode * ...

  5. c语言刷 DFS题记录

    144. 二叉树的前序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeN ...

  6. 计算机二级C语言选择题错题知识点记录。

    计算机二级C语言选择题错题知识点记录. 1,在数据流图中,用标有名字的箭头表示数据流.在程序流程图中,用标有名字的箭头表示控制流. 2,结构化程序设计的基本原则:自顶向下,逐步求精,模块化,限制使用g ...

  7. 学生管理系统-火车订票系统 c语言课程设计

    概要: C 语言课程设计一---学生管理系统 使使用 C 语言实现学生管理系统.系统实现对学生的基本信息和考试成绩的 管理.采用终端命令界面,作为系统的输入输出界面.采用文件作为信息存储介质. 功能描 ...

  8. C语言课程设计—图书管理系统

    这是本人大一第二学期初C语言课程设计的作品,嘿嘿,本来以为已经找不到原稿了,今天无意中居然在QQ网络硬盘中找到了当初的teta版,公布于此,以作纪念. C源码例如以下: #include<std ...

  9. Java语言课程设计——博客作业教学数据分析系统(201521123107 张翔)

    #Java语言课程设计--博客作业教学数据分析系统(个人博客) 1.团队课程设计博客链接 [博客作业教学数据分析系统(From:网络五条狗)](http://www.cnblogs.com/fanta ...

随机推荐

  1. mongo用户认证

    mongo@rayos:/opt/mongodb$ mongo --port 28017 MongoDB shell version v4.0.13 connecting to: mongodb:// ...

  2. C# 读取txt文件生成Word文档

    本文将以C#程序代码为例介绍如何来读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: Visual Studio 2017 .Net Framework 4.6.1 ...

  3. python08day

    内容回顾 数据类型的补充 str:pass tuple: (1)----->int count 计数 index 通过元组获取索引 list sort 排序从小到大 sort(reverse=T ...

  4. 男孩和女孩(二)-->相识

    转载请注明来源:https://www.cnblogs.com/hookjc/ 那天是男孩的十九岁生日:男孩还是像平常一样,一大早就起来了(快七点了).一切都是那么的平常,直到第一节课下课,男孩的同窗 ...

  5. Tomcat 下载安装,启动,停止,注册服务,开机自启

    感谢大佬:https://blog.csdn.net/wangmx1993328/article/details/81013715 目录 Tomcat 下载 startup.bat 启动 Tomcat ...

  6. node.js 使用domain模块捕获异步回调中的异常

    和其他服务器端语言相比,貌似node.js 对于异常捕捉确实非常困难. 首先你会想到try/catch ,但是在使用过程中我们会发现并没有真正将错误控制在try/catch 语句中. 为什么? 答案是 ...

  7. 《Effective Python》笔记——第1章 用Pythonic方式来思考

    一. 遵循PEP8风格指南. PEP8是对python代码格式而编订的风格指南.地址:https://www.python.org/dev/peps/pep-0008/ 个人觉得不一定完全按照PEP8 ...

  8. Pollard's rho algorithm和涉及到的两个循环检测算法

    0. 简单介绍 Pollard的\(\rho\)算法是John Pollard在1975年发明的,用于分解质因数[1].假定被分解的数为N,N的最小的质因数为\(p(p\ne N)\),那么该算法可以 ...

  9. python基础语法_字符串编码

    Python常用字符编码 http://www.cnblogs.com/schut/p/8406897.html   Python常见字符编码间的转换   在字符串写入文件时,有时会因编码问题导致无法 ...

  10. 用Express 创建项目

    1.Node.js Express 框架安装:npm install express --save在当前目录下创建一个node_modules 2.安装必要的中间件npm install body-p ...