From a4af5155e4a8f4b8bd689c4ac528fca55b1c0ae7 Mon Sep 17 00:00:00 2001 From: anirudhthakur <67862243+anirudhthakur@users.noreply.github.com> Date: Sun, 18 Oct 2020 22:26:47 +0530 Subject: [PATCH 1/2] Create stack.c --- stack.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 stack.c diff --git a/stack.c b/stack.c new file mode 100644 index 0000000..2b86799 --- /dev/null +++ b/stack.c @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define MAX 1000 + +class Stack { + int top; + +public: + int a[MAX]; // Maximum size of Stack + + Stack() { top = -1; } + bool push(int x); + int pop(); + int peek(); + bool isEmpty(); +}; + +bool Stack::push(int x) +{ + if (top >= (MAX - 1)) { + cout << "Stack Overflow"; + return false; + } + else { + a[++top] = x; + cout << x << " pushed into stack\n"; + return true; + } +} + +int Stack::pop() +{ + if (top < 0) { + cout << "Stack Underflow"; + return 0; + } + else { + int x = a[top--]; + return x; + } +} +int Stack::peek() +{ + if (top < 0) { + cout << "Stack is Empty"; + return 0; + } + else { + int x = a[top]; + return x; + } +} + +bool Stack::isEmpty() +{ + return (top < 0); +} + +// Driver program to test above functions +int main() +{ + class Stack s; + s.push(10); + s.push(20); + s.push(30); + cout << s.pop() << " Popped from stack\n"; + + return 0; +} From 5d4d73a7b1db469f5a68b75521a1912bfafc1258 Mon Sep 17 00:00:00 2001 From: anirudhthakur <67862243+anirudhthakur@users.noreply.github.com> Date: Sun, 18 Oct 2020 22:29:30 +0530 Subject: [PATCH 2/2] Create queue.c added queue implementation --- queue.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 queue.c diff --git a/queue.c b/queue.c new file mode 100644 index 0000000..7a28045 --- /dev/null +++ b/queue.c @@ -0,0 +1,102 @@ +// C program for array implementation of queue +#include +#include +#include + +// A structure to represent a queue +struct Queue { + int front, rear, size; + unsigned capacity; + int* array; +}; + +// function to create a queue +// of given capacity. +// It initializes size of queue as 0 +struct Queue* createQueue(unsigned capacity) +{ + struct Queue* queue = (struct Queue*)malloc( + sizeof(struct Queue)); + queue->capacity = capacity; + queue->front = queue->size = 0; + + // This is important, see the enqueue + queue->rear = capacity - 1; + queue->array = (int*)malloc( + queue->capacity * sizeof(int)); + return queue; +} + +// Queue is full when size becomes +// equal to the capacity +int isFull(struct Queue* queue) +{ + return (queue->size == queue->capacity); +} + +// Queue is empty when size is 0 +int isEmpty(struct Queue* queue) +{ + return (queue->size == 0); +} + +// Function to add an item to the queue. +// It changes rear and size +void enqueue(struct Queue* queue, int item) +{ + if (isFull(queue)) + return; + queue->rear = (queue->rear + 1) + % queue->capacity; + queue->array[queue->rear] = item; + queue->size = queue->size + 1; + printf("%d enqueued to queue\n", item); +} + +// Function to remove an item from queue. +// It changes front and size +int dequeue(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + int item = queue->array[queue->front]; + queue->front = (queue->front + 1) + % queue->capacity; + queue->size = queue->size - 1; + return item; +} + +// Function to get front of queue +int front(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + return queue->array[queue->front]; +} + +// Function to get rear of queue +int rear(struct Queue* queue) +{ + if (isEmpty(queue)) + return INT_MIN; + return queue->array[queue->rear]; +} + +// Driver program to test above functions./ +int main() +{ + struct Queue* queue = createQueue(1000); + + enqueue(queue, 10); + enqueue(queue, 20); + enqueue(queue, 30); + enqueue(queue, 40); + + printf("%d dequeued from queue\n\n", + dequeue(queue)); + + printf("Front item is %d\n", front(queue)); + printf("Rear item is %d\n", rear(queue)); + + return 0; +}