From f1645c5ba4d0c8bdd850a02dccf61cfd30ec5dc0 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 21 Oct 2019 22:35:28 +0300 Subject: [PATCH 1/2] Stack implementation --- C/stack.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 C/stack.c diff --git a/C/stack.c b/C/stack.c new file mode 100644 index 0000000..5e11647 --- /dev/null +++ b/C/stack.c @@ -0,0 +1,85 @@ +/* The given file gives the basics of a stack. The following functions are the main functions on the stack: + 1) push operation + 2) pop operation. + 3) get top + 4) get stack size + +This is a program written in C, with comments explaining the logic behind it. +*/ + + +#include + +typedef struct _NODE { + int data; + struct _NODE* prev; +} node; + +typedef struct _STACK{ + int size; + struct _NODE* top; +} stack; + +void initStack(stack* s) { + s->size = 0; + s->top = NULL; +} + +void push(stack* s, int data) { + node* newNode = malloc(sizeof(node)); + + newNode->data = data; + newNode->prev = s->top; + s->top = newNode; + s->size++; +} + +void pop(stack* s) { + node* delNode = s->top; + s->top = delNode->prev; + free(delNode); + s->size--; +} + +int getSize(stack s) { + return s.size; +} + +void printStack(stack s) { + node* dataPrint = NULL; + while (s.top != NULL) { + dataPrint = s.top; + printf("%d\n", dataPrint->data); + s.top = dataPrint->prev; + } +} + + +/*node getTop(stack s) { + return s->top; +}*/ + +int main (int argc, char ** argv) { + stack mySt; + initStack(&mySt); + + + + push(&mySt, 10); + push(&mySt, 11); + push(&mySt, 12); + push(&mySt, 13); + + printStack(mySt); + printf("%d\n", getSize(mySt)); + + pop(&mySt); + pop(&mySt); + push(&mySt, 20); + printStack(mySt); + printf("%d\n", getSize(mySt)); + + + + + } \ No newline at end of file From 1865e061a7dd364f6badacc8c34ec21320db431c Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 22 Oct 2019 13:58:12 +0300 Subject: [PATCH 2/2] Add queue implementation --- C/queue.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 C/queue.c diff --git a/C/queue.c b/C/queue.c new file mode 100644 index 0000000..5d071c4 --- /dev/null +++ b/C/queue.c @@ -0,0 +1,93 @@ +#include +#include + +typedef struct _NODE { + int data; + struct _NODE* next; +} node; + +typedef struct _QUEUE { + struct _NODE* front; + struct _NODE* back; + int size; +}queue; + + +void initQueue(queue* q) +{ + q->front = q->back = NULL; + q->size = 0; +} + +void push(queue* q, int data) +{ + node* newNode = (node *)malloc(sizeof(node)); + + newNode->data = data; + if (q->front == NULL) + { + newNode->next = NULL; + q->front = newNode; + } + else if (q->back == NULL) + { + q->front->next = newNode; + q->back = newNode; + } + else + { + q->back->next = newNode; + q->back = newNode; + q->back->next = NULL; + } + + q->size++; +} + +void pop(queue* q) +{ + node* delNode = q->front; + q->front = delNode->next; + free(delNode); + q->size--; + +} + +void printQueue (queue q) +{ + node* printData; + while (q.front != NULL) + { + printData = q.front; + printf("%d ", printData->data); + q.front = printData->next; + } + printf("\n"); +} + +int getTop(queue q) +{ + return q.size; +} + +int main(int argc, char** argv) +{ + queue myQ; + + initQueue(&myQ); + push(&myQ, 10); + push(&myQ, 11); + push(&myQ, 12); + push(&myQ, 13); + + printQueue(myQ); + printf("%d\n", getTop(myQ)); + + pop(&myQ); + pop(&myQ); + printQueue(myQ); + + printf("%d\n",getTop(myQ)); + + return 0; +} \ No newline at end of file