What is ADT?
ADT - Abstract Data Type
It provides information on data elements and its associated operation. It won't give us any information on implementation.
What is a Stack?
A stack is an abstract datatype that follows Last In First Out principle. Last element added to the stack must be the first element to be removed.
Stack supports two main operations:
push() - inserts new element to the top of stack
pop() - Remove the top element from the stack
See Also:
C Program For Array Implementation Of Queue
C Program For Array Implementation Of Stack
C Program For Linked List Implementation Of Stack
C Program For Linked List Implementation Of Queue
C Program For Double Ended Queue (Dequeue)
C Program To Implement Circular Stack
C Program To Implement Circular Queue
#include <stdio.h>
#include <stdlib.h>
int *stack = NULL;
int stackSize, top = -1;
/* push element to the top of stack */
void push(int data) {
if (top >= stackSize - 1) {
printf("Stack overflow\n");
return;
}
stack[++top] = data;
printf("Data added to stack:%d\n", stack[top]);
}
/* check whether stack is empty or not */
int isEmpty() {
if (top == -1)
return 1;
else
return 0;
}
/* pop/remove top element from stack */
void pop() {
if (isEmpty()) {
printf("Stack underflow\n");
return;
}
printf("Data poped from stack:%d\n", stack[top]);
stack[top--] = 0;
}
ADT - Abstract Data Type
It provides information on data elements and its associated operation. It won't give us any information on implementation.
What is a Stack?
A stack is an abstract datatype that follows Last In First Out principle. Last element added to the stack must be the first element to be removed.
Stack supports two main operations:
push() - inserts new element to the top of stack
pop() - Remove the top element from the stack
See Also:
C Program For Array Implementation Of Queue
C Program For Array Implementation Of Stack
C Program For Linked List Implementation Of Stack
C Program For Linked List Implementation Of Queue
C Program For Double Ended Queue (Dequeue)
C Program To Implement Circular Stack
C Program To Implement Circular Queue
Example Program For Array Implementation Of Stack:
#include <stdio.h>
#include <stdlib.h>
int *stack = NULL;
int stackSize, top = -1;
/* push element to the top of stack */
void push(int data) {
if (top >= stackSize - 1) {
printf("Stack overflow\n");
return;
}
stack[++top] = data;
printf("Data added to stack:%d\n", stack[top]);
}
/* check whether stack is empty or not */
int isEmpty() {
if (top == -1)
return 1;
else
return 0;
}
/* pop/remove top element from stack */
void pop() {
if (isEmpty()) {
printf("Stack underflow\n");
return;
}
printf("Data poped from stack:%d\n", stack[top]);
stack[top--] = 0;
}
/* display elements in stack */
void display() {
int i = 0;
if (isEmpty()) {
printf("No data present in stack\n");
return;
}
for (i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
int main () {
int size, data, ch;
printf("Enter the size of the stack:");
scanf("%d", &size);
stackSize = size;
stack = (int *)malloc(sizeof (int) *size);
while (1) {
printf("1. push\n2. Pop\n3. IsEmpty\n");
printf("4. Display stack objects\n");
printf("5. Object count\n6. Exit\n");
printf("Enter ur option:");
scanf("%d", &ch);
switch (ch){
case 1:
printf("Enter the ur data:");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
if (isEmpty())
printf("Stack is Empty\n");
else
printf("Stack is not Empty\n");
break;
case 4:
display();
break;
case 5:
if (isEmpty())
printf("No objects in stack\n");
else
printf("Object count:%d\n", top+1);
break;
case 6:
exit(0);
default:
printf("U have entered wrong option\n");
break;
}
}
return 0;
}
Output: (C Program For Array Implementation Of Stack)
jp@jp-VirtualBox:$ ./a.out
Enter the size of the stack:2
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:3
Stack is Empty
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:1
Enter the ur data:10
Data added to stack:10
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:1
Enter the ur data:20
Data added to stack:20
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:1
Enter the ur data:30
Stack overflow
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:4
20
10
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:5
Object count:2
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:2
Data poped from stack:20
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:2
Data poped from stack:10
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:3
Stack is Empty
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:6
Enter the size of the stack:2
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:3
Stack is Empty
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:1
Enter the ur data:10
Data added to stack:10
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:1
Enter the ur data:20
Data added to stack:20
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:1
Enter the ur data:30
Stack overflow
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:4
20
10
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:5
Object count:2
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:2
Data poped from stack:20
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:2
Data poped from stack:10
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:3
Stack is Empty
1. push
2. Pop
3. IsEmpty
4. Display stack objects
5. Object count
6. Exit
Enter ur option:6
No comments:
Post a Comment