main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "post.c"
#include "comment.c"
#include "reply.c"
#include "platform.c"
void getStringInput(const char* prompt, char* str, size_t maxSize) {
    printf("%s", prompt);
    fgets(str, maxSize, stdin);
    str[strcspn(str, "\n")] = '\0'; // Remove newline character at the end
}


int main()
{
    char input[100];

    int postsize = 0;
    int commentsize = 0;
    int replysize = 0;

 

    while (1)
    {
        scanf("%s", input);

        if (!strcmp(input, "create_platform"))
        {

            createPlatform();
        }

        if (!strcmp(input, "add_post"))
        {
            // printf("ssssssss");
            char username[100];
            char caption[100];

            scanf("%s", username);
            // printf("dddddddddd");
            scanf("%s", caption);
            
            addPost(username, caption);
            // printf("gggggggggg");
    
            postsize++;
        }

        if (!strcmp(input, "delete_post"))
        {
            int n;
            scanf("%d", &n);
            deletePost(n);
            postsize--;
        }
///////////////////////////////////////////////////////
        if (!strcmp(input, "view_post"))
        {


            int a;
            scanf("%d", &a);

            viewPost(a);
             //printf("ooooooooooooooooooo");
        }
//////////////////////////////////////////////////////////
        if (!strcmp(input, "current_post"))
        {
            currPost();
        }

        if (!strcmp(input, "previous_post"))
        {
            previousPost();
        }

        if (!strcmp(input, " next_post"))
        {
            nextPost();
        }

        if (!strcmp(input, "add_comment"))
        {
            char username[100];
            char content[100];
            scanf("%s", username);
            scanf("%s", content);
            addComment(username, content);
            commentsize++;
        }

        if (!strcmp(input, "delete_comment"))
        {
            int n;
            scanf("%d", &n);
            deleteComment(n);
            commentsize--;
        }

        // returns a pointer to head of linkedlist comments
        // and returns bool
        if (!strcmp(input, "view_all_comments"))
        {
            viewComments();
        }

        if (!strcmp(input, "add_reply"))
        {
            char username[100];
            char content[100];
            int n;
            scanf("%s", username);
            scanf("%s", content);
            scanf("%d", &n);

            addReply(username, content, n);
            // scanf("%s", newReply->Username);

            replysize++;
        }

        if (!strcmp(input, "delete_reply"))
        {
            int n, m;
            scanf("%d", &n);
            scanf("%d", &m);
            deleteReply(n, m);
            replysize--;
        }
    }
    return 0;
}

platform.c

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "platform.h"


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


struct Platform *newPlatform ;

struct Platform *createPlatform(){
// printf("sfhbwej");
   newPlatform = (struct Platform *)malloc(sizeof(struct Platform));
//    printf("sfhbwej");
    newPlatform->posts = NULL;
    newPlatform->lastViewedPost = NULL;
    return newPlatform;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool addPost(char username[], char caption[])
 {  printf("qwerty");
if (!newPlatform) return;
    Post* newPost = createPost(username, caption);
printf("ttttttttttttttt");
    // newPost=newPost->next;

    newPost->next = newPlatform->posts;
    newPlatform->posts = newPost;
    newPlatform->lastViewedPost = newPost;

    return (true);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool deletePost(int n)
{

    Post *postprev = NULL;
    Post *postcurr = newPlatform->posts;

    if (postcurr == NULL)
    {
        return false;
    }
    else
    {
        for (int i = 0; i < n; i++)
        {
            postprev = postcurr;
            postcurr = postcurr->next;
        }
    }

    if (postprev == NULL)
    {
        newPlatform->posts = postcurr->next;
    }
    else
    {
        postprev->next = postcurr->next;
    }
    free(postcurr);
    free(postprev);

    return true;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Post *viewPost(int n)
{
    Post *temp = newPlatform->posts;

    if (temp != NULL)
    {printf("qqqqqqqqqq");
        for (int i = 0; i < n; i++)
        {printf("wwwwwwwwwwwwww");
            temp = temp->next;
        }
    printf("eeeeeeeeeeeeeeeee");
    newPlatform->lastViewedPost = temp;
    return temp;

    }
    else{printf("77777777777777");
        newPlatform->lastViewedPost = temp;
    return NULL;
    }
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Post *currPost()
{
    if (newPlatform->lastViewedPost != NULL)
        return (newPlatform->lastViewedPost);
    else
        return NULL;
    // if condition is not necessary coz lastviewedpost is initialized to null anyway but havnt yet 
    // considered cases where lastviewedpost gets deleted
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Post *nextPost()
{
    // return null if no post was viewed
    if (newPlatform->lastViewedPost == NULL)
    {
        return NULL;
    }
    // return null if there is only one post
    if (newPlatform->lastViewedPost->next == NULL)
    {
        return newPlatform->lastViewedPost;
    }
    // else return the next post of the last viewed post
    newPlatform->lastViewedPost = newPlatform->lastViewedPost->next;
    return newPlatform->lastViewedPost;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Post *previousPost()
{

    if (newPlatform->lastViewedPost == NULL)
    {
        return NULL;
    }

    if (newPlatform->lastViewedPost == newPlatform->posts)
    {
        return newPlatform->lastViewedPost;
    }

    Post *temp = newPlatform->posts;
    while (temp != NULL && temp->next != newPlatform->lastViewedPost)
    {
        temp = temp->next;
    }

    newPlatform->lastViewedPost = temp;
    return temp;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool addComment(char *username, char *content)
{

    if (newPlatform->lastViewedPost == NULL)
    {
        return 1;
    }

    Comment *comment = createComment(username, content);
    comment->next = newPlatform->lastViewedPost->Comments;
    newPlatform->lastViewedPost->Comments = comment;
    return 1;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool deleteComment(int n)
{
    if (newPlatform->lastViewedPost == NULL)
    {
        return false;
    }

    Comment *commentprev = NULL;
    Comment *commentcurr = newPlatform->lastViewedPost->Comments;

    for (int i = 0; i < n; i++)
    {
        commentprev = commentcurr;
        commentcurr = commentcurr->next;
    }

    if (commentcurr == NULL)
    {
        return false;
    }
    if (commentprev == NULL)
    {
        newPlatform->lastViewedPost->Comments = commentcurr->next;
    }
    else
    {
        commentprev->next = commentcurr->next;
    }

    free(commentcurr);
    free(commentprev);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Comment *viewComments()
{
    if (newPlatform->lastViewedPost == NULL)
    {
        return NULL;
    }

    return newPlatform->lastViewedPost->Comments;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool addReply(char *username, char *content, int n)
{

    if (newPlatform->lastViewedPost == NULL)
    {
        return true;
    }

    Comment *temp = newPlatform->lastViewedPost->Comments;

    if (temp != NULL)
    {
        for (int i = 0; i < n; i++)
        {
            temp = temp->next;
        }
    }
    if (temp == NULL)
    {
        return true;
    }

    Reply *reply = createReply(username, content);
    reply->next = temp->Replies;
    temp->Replies = reply;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool deleteReply(int n, int m)
{

    if (newPlatform->lastViewedPost == NULL)
    {
        return false;
    }

    Comment *temp = newPlatform->lastViewedPost->Comments;
    int commentCount = 1;

    if (temp != NULL)
    {
        for (int i = 0; i < n; i++)
        {
            temp = temp->next;
        }
    }

    if (temp == NULL)
    {
        return false;
    }

    Reply *prevReply = NULL;
    Reply *currReply = temp->Replies;

    if (currReply != NULL)
    {
        for (int j = 0; j < m; j++)
        {
            prevReply = currReply;
            currReply = currReply->next;
        }
    }

    if (currReply == NULL)
    {
        return false;
    }

    if (prevReply == NULL)
    {
        temp->Replies = currReply;
    }
free(temp);
free(prevReply);
free(currReply);
    return true;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


platform.h

#ifndef PLATFORM_H
#define PLATFORM_H
#include "post.h"
#include <stdbool.h>

struct Platform
{
    Post *posts;
    Post *lastViewedPost;
    
} ;

extern struct Platform* newPlatform;



struct Platform *createPlatform();
bool addPost(char *username, char *caption);
bool deletePost(int n);
Post *viewPost(int n);
Post *currPost();
Post *nextPost();
Post *previousPost();
bool addComment(char *username, char *content);
bool deleteComment(int n);
Comment* viewComments();
bool addReply(char *username,char *content,int n);
bool deleteReply(int n, int m);

#endif

reply.c
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "reply.h"
Reply *createReply(char *username, char *content)
{
    Reply *newReply = (Reply *)malloc(sizeof(Reply));
    strcpy(newReply->Username,username);
    strcpy(newReply->Content,content);
    newReply->next=NULL;
    return newReply;
}
reply.h
#ifndef REPLY_H
#define REPLY_H
typedef struct Reply
{
    char *Username;
    char *Content;
    struct Reply* next;

} Reply;

Reply *createReply(char *username, char *content);

#endif

comment.c
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "comment.h"

Comment *createComment(char *username, char *content)
{
    Comment *newComment = (Comment *)malloc(sizeof(Comment));
    strcpy(newComment->Username,username);
    strcpy(newComment->Content,content);
    newComment->Replies = NULL;
    newComment->next = NULL;
    return newComment;
}

comment.h
#ifndef COMMENT_H
#define COMMENT_H
#include "reply.h"
typedef struct Comment
{
    char *Username;
    char *Content;
    struct Reply* Replies;
    struct Comment *next;
} Comment;

Comment *createComment(char *username, char *content);
#endif

post.c
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "post.h"
#include "platform.h"
#include<string.h>

Post *createPost(char *username, char *caption)
{

   Post *newPost = (Post *)malloc(sizeof(Post));
   
   // newPost->Username = strdup(username);
   // newPost->Caption = strdup(caption);

   strcpy(newPost->Username ,username);
   strcpy( newPost->Caption,caption);
   newPost->Comments = NULL;
   newPost->next = NULL;

   return newPost;
}
post.h
#ifndef POST_H
#define POST_H
#include "comment.h"

typedef struct Post
{
    char *Username;
    char *Caption;
    Comment* Comments;
    struct Post* next;
    
    
    

} Post;

Post *createPost(char *username, char *caption);
#endif