diff --git a/codes/arrays/array_rev.c b/codes/arrays/array_rev.c index a2c15fc..833f02d 100644 --- a/codes/arrays/array_rev.c +++ b/codes/arrays/array_rev.c @@ -9,7 +9,8 @@ int main(){ { scanf("%d",&arr[i]); } - printf("Reversed Array is\n"); + for(int loop = n-1; loop >= 0; loop--) + printf("%d ", arr[loop]); return 0; -} \ No newline at end of file +} diff --git a/codes/queue/queue_implement.c b/codes/queue/queue_implement.c new file mode 100644 index 0000000..5f7452b --- /dev/null +++ b/codes/queue/queue_implement.c @@ -0,0 +1,83 @@ +#include +# define SIZE 100 +//prototype +void enqueue(); +void dequeue(); +void show(); +int arr[SIZE]; +int Rear = - 1; +int Front = - 1; +int main() +{ + int ch; + while (1) + { + printf("1.Enqueue Operation\n"); + printf("2.Dequeue Operation\n"); + printf("3.Display the Queue\n"); + printf("4.Exit\n"); + printf("Enter your choice of operations : "); + scanf("%d", &ch); + switch (ch) + { + case 1: + enqueue(); + break; + case 2: + dequeue(); + break; + case 3: + show(); + break; + case 4: + exit(0); + default: + printf("Incorrect choice \n"); + } + } +} + +void enqueue() +{ + int insert_item; + if (Rear == SIZE - 1) + printf("Overflow \n"); + else + { + if (Front == - 1) + + Front = 0; + printf("Element to be inserted in the Queue\n : "); + scanf("%d", &insert_item); + Rear = Rear + 1; + arr[Rear] = insert_item; + } +} + +void dequeue() +{ + if (Front == - 1 || Front > Rear) + { + printf("Underflow \n"); + return ; + } + else + { + printf("Element deleted from the Queue: %d\n", arr[Front]); + Front = Front + 1; + } +} + +void show() +{ + + if (Front == - 1) + printf("Empty Queue \n"); + else + { + printf("Queue: \n"); + for (int i = Front; i <= Rear; i++) + printf("%d ", arr[i]); + printf("\n"); + } +} \ No newline at end of file diff --git a/codes/recurssion/sum_n_number.c b/codes/recurssion/sum_n_number.c index ba9e240..976a7d2 100644 --- a/codes/recurssion/sum_n_number.c +++ b/codes/recurssion/sum_n_number.c @@ -3,7 +3,7 @@ int main() { int n; - int sum_natural(int ); + //int sum_natural(int ); printf("n = "); scanf("%d",&n); printf("Sum of first %d natural numbers = %d\n",n,sum_natural(n)); @@ -16,4 +16,4 @@ int sum_natural(int n) { return n + sum_natural(n-1); } -} \ No newline at end of file +} diff --git a/codes/stack/stack_implement_cpp.cpp b/codes/stack/stack_implement_cpp.cpp new file mode 100644 index 0000000..5dd28ca --- /dev/null +++ b/codes/stack/stack_implement_cpp.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +//implementation of stack using a class +class Stack{ + //properties + public: + int top; + int *arr; + int size; + +//constructor to dynamically allocate the array size + Stack(int size){ + this->size=size; + arr=new int[size]; + top=-1; + } + void push ( int element){ + if(size-top>1){ + top++; + arr[top]=element; + } + else{ + cout<<"stack overflow"; + } + } + void pop(){ + if(top>=0){ + top--; + } + else{ + cout<<"stack underflow"<=0 && top44 + + st.pop(); + cout<<"Top is "<43 + //another pop + st.pop(); + cout<<"Top is "<