{"id":17504373,"url":"https://github.com/jeepway/stack-c-polymorphism","last_synced_at":"2025-03-28T20:15:01.860Z","repository":{"id":258113962,"uuid":"873433897","full_name":"JeepWay/stack-C-polymorphism","owner":"JeepWay","description":"This project implements a stack using function pointers in C to simulate polymorphism. The stack supports common operations such as push, pop, top and so on.","archived":false,"fork":false,"pushed_at":"2024-10-16T07:49:13.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T20:14:58.520Z","etag":null,"topics":["array","c","c-language","dynamic-size","polymorphism","pop","push","stack","stack-array"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JeepWay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-16T06:55:02.000Z","updated_at":"2024-10-16T07:49:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"cd380da1-e87d-47ad-87fc-f378d2dede99","html_url":"https://github.com/JeepWay/stack-C-polymorphism","commit_stats":null,"previous_names":["jeepway/stack-c-polymorphism"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Fstack-C-polymorphism","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Fstack-C-polymorphism/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Fstack-C-polymorphism/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JeepWay%2Fstack-C-polymorphism/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JeepWay","download_url":"https://codeload.github.com/JeepWay/stack-C-polymorphism/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246093180,"owners_count":20722402,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","c","c-language","dynamic-size","polymorphism","pop","push","stack","stack-array"],"created_at":"2024-10-20T00:15:19.059Z","updated_at":"2025-03-28T20:15:01.843Z","avatar_url":"https://github.com/JeepWay.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Implementation of a Stack with Function Pointers to Simulate Polymorphism in C Language\n\n## Description\n* The stack is implemented using array.\n* The stack uses function pointers to simulate polymorphism in C language.\n* The function pointers are used to point to the real implementation of common stack operations, \ne.g., push, pop, and top, like [std::stack](https://cplusplus.com/reference/stack/stack/) in C++.\n* This project is default to support the data type of `int`, `char`, and `string`, but it can be easily extended \nto support other data types, e.g., `float`, `double` and so on.\n\n## Function Pointers\n* `void (*init)(struct Stack *s)`: Initialize the stack.\n* `void (*push)(struct Stack *s, void *element)`: Push an element into the stack.\n* `void* (*pop)(struct Stack *s)`: Pop an element from the stack.\n* `void* (*top)(struct Stack *s)`: Get the top element of the stack.\n* `bool (*isFull)(struct Stack *s)`: Check if the stack is full.\n* `bool (*isEmpty)(struct Stack *s)`: Check if the stack is empty.\n* `int (*size)(struct Stack *s)`: Get the current size of the stack.\n* `void (*free)(struct Stack *s)`: Free the memory occupied by the stack.\n* `void (*print)(struct Stack *s)`: Print the information of the stack.\n\n## Simple Usage\n1. Compile the source code to object files.\n    ```bash\n    $ make test_stack\n    $ gcc -c -o \u003cyour_program\u003e.o \u003cyour_program\u003e.c\n    ```\n2. Link the object files to the executable file.\n    ```bash\n    $ gcc -Wall -o \u003cyour_program\u003e \u003cyour_program\u003e.o stack/stack.o stack/stack_char.o stack/stack_string.o stack/stack_int.o\n    $ ./\u003cyour_program\u003e\n    ```\n3. Demo of stack of `int` data type.\n    ```cpp\n    #include \"stack/stack.h\"\n    int main() {\n        Stack *stack = createStack(INT);\n        if (!stack-\u003etop(stack)) {\n            printf(\"stack is empty\\n\");\n        }\n        int i = 123;\n        stack-\u003epush(stack, \u0026i);\n        int *poppedInt = (int *)stack-\u003epop(stack);\n        printf(\"Popped from int stack: %d\\n\\n\", *poppedInt);\n        stack-\u003efree(stack);\n        return 0;\n    }\n    ```\n4. Demo of stack of `char` data type.\n    ```cpp\n    #include \"stack/stack.h\"\n    int main() {\n        Stack *stack = createStack(CHAR);\n        if (*(char*)stack-\u003etop(stack) == '\\0') {\n            printf(\"stack is empty\\n\");\n        }\n        char c = 'a';\n        stack-\u003epush(stack, \u0026c);  // passed by reference\n        char *poppedChar = (char *)stack-\u003epop(stack);\n        printf(\"Popped from char stack: %c\\n\\n\", *poppedChar);\n        stack-\u003efree(stack);\n        return 0;\n    }\n    ```\n\n## Key Note\n* The parameter passed to `createStack` is the type of the stack, which can be `INT`, `CHAR`, or `STRING`. You can also see the supported data types in the `struct StackTyp`e of [`stack/stack.h`](https://github.com/JeepWay/stack-C-polymorphism/blob/main/stack/stack.h).\n* When using `push`, you need to pass the memory address of the data to be pushed into the stack, because the function pointer of `push` is defined as `void (*push)(struct Stack *s, void *element)`.\n* When using `pop` and `top`, you need to cast the return value to the correct data type, just like using `malloc()` in C, to avoid the warning of `incompatible pointer type`, beacuse the return value is a `void *` pointer.\n* When stack is empty during `pop` and `top`: \n    * For `INT` type, the return value of `pop` and `top` are `NULL`.\n    * For `CHAR` type, the return value of `pop` and `top` are `\"\\0\"`.\n    * For `STRING` type, the return value of `pop` and `top` are `\"\\0\"`.\n\n## Test\n* Test stack with polymorphism\n    ```bash\n    make test TARGET=test_stack\n    ```\n* Test stack of int\n    ```bash\n    make test TARGET=test_stack_int   \n    ```\n* Test stack of char\n    ```bash\n    make test TARGET=test_stack_char   \n    ```\n* Test stack of string\n    ```bash\n    make test TARGET=test_stack_string   \n    ```\n\n## Extension\nIf you want to extend the stack to support other data types, e.g., `float`, `double`, you can follow the steps below:\n1. Add a new stack file, e.g., `stack_float.c` and `stack_float.h` in the `stack` directory.\n2. Implement the stack functions in `stack_float.c` and declare the functions in `stack_float.h`.\n3. Add the new rules in the `Makefile` to compile the new stack files.\n    ```makefile\n    OBJS_STACK = stack/stack.o stack/stack_char.o stack/stack_string.o stack/stack_int.o stack/stack_float.o \n    ```\n4. Update `enum StackType` in `stack/stack.h` to support the new data type.\n5. Update `stack/stack.c` to support the new data type.\n    * Add `#include \"stack_float.h\"`\n    * Add the switch case according to the new data type in `createStack` function.\n6. Re-compile the source code and link the object files to the executable file.\n    ```bash\n    $ make test_stack\n    $ gcc -c -o \u003cyour_program\u003e.o \u003cyour_program\u003e.c\n    $ gcc -Wall -o \u003cyour_program\u003e \u003cyour_program\u003e.o stack/stack.o stack/stack_char.o stack/stack_string.o stack/stack_int.o stack/stack_float.o\n    $ ./\u003cyour_program\u003e\n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeepway%2Fstack-c-polymorphism","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeepway%2Fstack-c-polymorphism","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeepway%2Fstack-c-polymorphism/lists"}