Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload opaque pointer implementation of stack #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Binary Search Tree/README.md
Original file line number Diff line number Diff line change
@@ -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!
12 changes: 12 additions & 0 deletions Stack ADT/using opaque pointer/README.md
Original file line number Diff line number Diff line change
@@ -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).
15 changes: 15 additions & 0 deletions Stack ADT/using opaque pointer/array based implementation/main.c
Original file line number Diff line number Diff line change
@@ -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;
}
54 changes: 54 additions & 0 deletions Stack ADT/using opaque pointer/array based implementation/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "stack.h"
#include <stdlib.h>
#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]);
}
10 changes: 10 additions & 0 deletions Stack ADT/using opaque pointer/array based implementation/stack.h
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "stack.h"
#include <stdlib.h>
#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);
}
Original file line number Diff line number Diff line change
@@ -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