diff --git a/Binary Search Tree/README.md b/Binary Search Tree/README.md new file mode 100644 index 0000000..52c754e --- /dev/null +++ b/Binary Search Tree/README.md @@ -0,0 +1,39 @@ +# Binary Search Tree (BST) + +This directory contains the implementation of a Binary Search Tree (BST) in C. The BST allows efficient storage and retrieval of data in a sorted manner. + +## Files + +- `bst-definition.c`: This file contains the implementation of various functions related to the BST, including `getnode()`, `insert()`, `inorder()`, `preorder()`, `postorder()`, `delete()`, and `minvaluenode()`. +- `bst-main.c`: This file provides a user interface to interact with the BST. It allows users to insert elements into the tree, delete elements from the tree, and perform tree traversals. +- `bst.h`: This header file contains the structure definition for the `bstree` node and function prototypes for the BST operations. + +## Compilation + +To compile the program, you can use the following commands: + +```bash +gcc -c bst-definition.c -o bst-definition.o +gcc -c bst-main.c -o bst-main.o +gcc bst-definition.o bst-main.o -o program +``` + +## Usage + +After compiling the program, you can run it using the following command: + +```bash +./program +``` +Once the program starts, you will be presented with a menu to perform different operations on the BST: + +1. Insert Element in Tree: This option allows you to insert a new element into the BST. +2. Delete Element from Tree: This option allows you to delete an element from the BST. +3. Tree Traversals: This option displays the pre-order, in-order, and post-order traversals of the BST. +4. EXIT: This option exits the program. + +## Note + +Ensure that all the necessary files (bst-definition.c, bst-main.c, bst.h) are present in the same directory before compiling and running the program. + +Feel free to explore and modify the code to suit your requirements. Enjoy working with Binary Search Trees! diff --git a/Stack ADT/using opaque pointer/README.md b/Stack ADT/using opaque pointer/README.md new file mode 100644 index 0000000..d57b49c --- /dev/null +++ b/Stack ADT/using opaque pointer/README.md @@ -0,0 +1,12 @@ +# Implementation of stack using opaque pointer in C + +This showcases an implementation of a stack using the `opaque pointer` concept in the `C` programming language. It consists of three files: + +- `stack.h`: This is the header file that contains the declarations of the functions and data structures used in the stack implementation. +- `stack.c`: This file contains the actual implementation of the stack functions defined in the header file. +- `main.c`: This is the driver code that demonstrates how to use the stack functions. + +### > How to compile & run + +- Compile with `gcc stack.c main.c`. +- Run with `./a.out` (Linux environment) or `.\a.exe` (Windows environment). diff --git a/Stack ADT/using opaque pointer/array based implementation/main.c b/Stack ADT/using opaque pointer/array based implementation/main.c new file mode 100644 index 0000000..0a95c01 --- /dev/null +++ b/Stack ADT/using opaque pointer/array based implementation/main.c @@ -0,0 +1,15 @@ +#include "stack.h" +#define create(stack) Stack*stack=create() + +int main() +{ + create(stack); + + for (int i = 1; i <= 10; push(stack, i++)); + pop(stack); + pop(stack); + pop(stack); + top(stack); + + return 0; +} \ No newline at end of file diff --git a/Stack ADT/using opaque pointer/array based implementation/stack.c b/Stack ADT/using opaque pointer/array based implementation/stack.c new file mode 100644 index 0000000..d33aadb --- /dev/null +++ b/Stack ADT/using opaque pointer/array based implementation/stack.c @@ -0,0 +1,54 @@ +#include "stack.h" +#include +#define length(arr) (sizeof (arr) / sizeof *(arr)) + +struct Stack +{ + int arr[10]; + int top; +}; + +Stack *create(void) +{ + Stack *stack = malloc(sizeof(Stack)); + if (!stack) + return NULL; + stack->top = -1; + return stack; +} + +inline static _Bool overflow(Stack *stack) +{ + return length(stack->arr) - 1 == stack->top; +} + +inline static _Bool underflow(Stack *stack) +{ + return -1 == stack->top; +} + +int printf(const char *, ...); + +void push(Stack *stack, int data) +{ + if (overflow(stack)) + printf("Stack overflow!\n"); + else + stack->arr[++(stack->top)] = data; +} + +void pop(Stack *stack) +{ + if (underflow(stack)) + printf("Stack underflow!\n"); + else + printf("%d\n", stack->arr[(stack->top)--]); +} + +void top(Stack *stack) +{ + if (underflow(stack)) + printf("Stack underflow!\n"); + else + printf("%d\n", stack->arr[stack->top]); +} \ No newline at end of file diff --git a/Stack ADT/using opaque pointer/array based implementation/stack.h b/Stack ADT/using opaque pointer/array based implementation/stack.h new file mode 100644 index 0000000..b11999c --- /dev/null +++ b/Stack ADT/using opaque pointer/array based implementation/stack.h @@ -0,0 +1,10 @@ +#ifndef STACK_H +#define STACK_H + +typedef struct Stack Stack; +Stack *create(void); +void push(Stack *, int); +void pop(Stack *); +void top(Stack *); + +#endif \ No newline at end of file diff --git a/Stack ADT/using opaque pointer/linked list based implementation/main.c b/Stack ADT/using opaque pointer/linked list based implementation/main.c new file mode 100644 index 0000000..d2957b7 --- /dev/null +++ b/Stack ADT/using opaque pointer/linked list based implementation/main.c @@ -0,0 +1,16 @@ +#include "stack.h" +#define create(stack) Stack*stack=create() + +int main() +{ + create(stack); + + for (int i = 1; i <= 10; push(stack, i++)); + pop(stack); + pop(stack); + pop(stack); + top(stack); + + + return 0; +} \ No newline at end of file diff --git a/Stack ADT/using opaque pointer/linked list based implementation/stack.c b/Stack ADT/using opaque pointer/linked list based implementation/stack.c new file mode 100644 index 0000000..6bca677 --- /dev/null +++ b/Stack ADT/using opaque pointer/linked list based implementation/stack.c @@ -0,0 +1,78 @@ +#include "stack.h" +#include +#define capacity 10 + +struct Node +{ + int data; + Node *next; +}; + +struct Stack +{ + Node *top; + int count; +}; + +Stack *create(void) +{ + Stack *stack = malloc(sizeof(Stack)); + if (!stack) + return NULL; + stack->top = NULL; + stack->count = 0; + return stack; +} + +inline static _Bool overflow(Stack *stack) +{ + return capacity == stack->count; +} + +inline static _Bool underflow(Stack *stack) +{ + return !stack->count; +} + +int printf(const char *, ...); + +void push(Stack *stack, int data) +{ + if (overflow(stack)) + printf("Stack overflow!\n"); + else + { + Node *newNode = malloc(sizeof(Node)); + if (!newNode) + return; + if (!stack->count) // for the first inserted node + newNode->next = NULL; + newNode->data = data; + newNode->next = stack->top; + stack->top = newNode; + stack->count++; + } +} + +void pop(Stack *stack) +{ + if (underflow(stack)) + printf("Stack underflow!\n"); + else + { + printf("%d\n", stack->top->data); + + Node *del = stack->top; + stack->top = stack->top->next; + free(del); + stack->count--; + } +} + +void top(Stack *stack) +{ + if (underflow(stack)) + printf("Stack underflow!\n"); + else + printf("%d\n", stack->top->data); +} \ No newline at end of file diff --git a/Stack ADT/using opaque pointer/linked list based implementation/stack.h b/Stack ADT/using opaque pointer/linked list based implementation/stack.h new file mode 100644 index 0000000..6ab45f9 --- /dev/null +++ b/Stack ADT/using opaque pointer/linked list based implementation/stack.h @@ -0,0 +1,11 @@ +#ifndef STACK_H +#define STACK_H + +typedef struct Node Node; +typedef struct Stack Stack; +Stack *create(void); +void push(Stack *, int); +void pop(Stack *); +void top(Stack *); + +#endif \ No newline at end of file