Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program to implement stack using array.
- #include<stdio.h>
- #include<conio.h>
- struct stack {
- int arr[10];
- int top;
- } st;
- int isFull() {
- if (st.top == 9) {
- return 1;
- }
- else {
- return 0;
- }
- }
- int isEmpty() {
- if (st.top == -1) {
- return 1;
- }
- else {
- return 0;
- }
- }
- void push() {
- if (!isFull()) {
- int val;
- printf("Enter value to be pushed: ");
- scanf("%d", &val);
- st.top++;
- st.arr[st.top] = val;
- printf("Successfully pushed %d in the stack.", val);
- getch();
- }
- else {
- printf("The Stack is Full.");
- getch();
- }
- }
- void pop() {
- if (!isEmpty()) {
- printf("Successfully popped %d from the stack.", st.arr[st.top]);
- st.top--;
- getch();
- }
- else {
- printf("The stack is Empty.");
- getch();
- }
- }
- void display() {
- int i;
- if (isEmpty()) {
- printf("The stack is Empty.");
- getch();
- }
- else {
- for (i = st.top; i >= 0; i--) {
- printf("%d\n", st.arr[i]);
- }
- getch ();
- }
- }
- void main()
- {
- char choice;
- clrscr();
- st.top = -1;
- while (1) {
- clrscr();
- printf("1.Push\n2.Pop\n3.Display\n4.Exit\n");
- choice = getch();
- switch (choice) {
- case '1':
- push();
- break;
- case '2':
- pop();
- break;
- case '3':
- display();
- break;
- case '4':
- return;
- default:
- printf("Invalid input.");
- getch();
- }
- }
- }
Add Comment
Please, Sign In to add comment