Skip to content

Commit

Permalink
Merge pull request Ishaan28malik#1852 from dispuzzler/patch-1
Browse files Browse the repository at this point in the history
Create ReverseStack.c
  • Loading branch information
Ishaan28malik authored Oct 23, 2022
2 parents b853a92 + 8c7a8bb commit 6e9e35e
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions Algorithms-in-C/ReverseStack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdio.h>

#define MAXSIZE 7
#define TRUE 1
#define FALSE 0

struct Stack {
int top;
int array[MAXSIZE];
} st;


void initialize() {
st.top = -1;
}


int isFull() {
if(st.top >= MAXSIZE-1)
return TRUE;
else
return FALSE;
}


int isEmpty() {
if(st.top == -1)
return TRUE;
else
return FALSE;
}

void push(int num) {
if (isFull())
printf("Stack is Full...\n");
else {
st.array[st.top + 1] = num;
st.top++;
}
}


int pop() {
if (isEmpty())
printf("Stack is Empty...\n");
else {
st.top = st.top - 1;
return st.array[st.top+1];
}
}


void printStack(){
if(!isEmpty()){
int temp = pop();
printStack();
printf(" %d ", temp);
push( temp);
}
}
void insertAtBottom(int item) {
if (isEmpty()) {
push(item);
} else {


int top = pop();
insertAtBottom(item);


push(top);
}
}

void reverse() {
if (!isEmpty()) {

int top = pop();
reverse();


insertAtBottom(top);
}
}


int getSize(){
return st.top+1;
}

int main() {
initialize(st);
push(1);
push(2);
push(3);
push(4);
push(5);
printf("Original Stack\n");
printStack();
reverse();
printf("\nReversed Stack\n");
printStack();
return 0;
}

0 comments on commit 6e9e35e

Please sign in to comment.