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. NumPy 秘籍中文第二版·翻译完成

    原文:NumPy Cookbook - Second Edition 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 Apache ...

  2. CF1229D - Wojtek and Card Tricks

    做法一 首先将排列用康托展开映射到整数,并且预处理出排列之间乘法的结果,复杂度 \(\mathcal{O}(k(k!) ^ 2)\). 枚举左端点,有可能分段的右端点至多只有 \(k!\) 个,不妨将 ...

  3. 解决github.com无法访问

    解决 绕过DNS解析,直接使用本地DNS记录进行直接跳转. DNS查询 在浏览器中打开DNS查询网站:http://tool.chinaz.com/dns?type=1&host=github ...

  4. Spring service本类中方法互相调用事物失效问题

    简介 Spring事物利用的是AOP,动态代理采用CGLIB代理(默认,也可以用Proxy代理,但是Proxy代理效率低于CGLIB代理).故只要弄懂Spring的AOP实现,就知道为什么servic ...

  5. C 数组排序后输出至文件

    如题 C实现 #include<stdio.h> #define COUNT 9 //数组长度+1 #define FILE_NAME "data.txt" //文件名 ...

  6. js静态成员和实例成员

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. aidl的应用场景

    支付宝 package com.tesy.alipay; import com.test.alipay.Iservice.Stub; import android.app.Service; impor ...

  8. 广播接收者案例_ip拨号器

    (1)定义一个类继承BroadCastReceiver public class OutGoingCallReceiver extends BroadcastReceiver { //当接收到外拨电话 ...

  9. IPV4地址学习笔记

    IP地址由网络号和主机号组成.IPV4由32位正整数来表示. A类IP:8位网络号+24主机号,网络号以0开头,网络号范围0~127 B类IP:16位网络号+16主机号,网络号以10开头,网络号范围1 ...

  10. Linux curl命令进行网络请求

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11841353.html 1. curl get请求: curl http://www.baid ...