This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Stacks-ArrayImp.cpp
73 lines (66 loc) · 1.48 KB
/
Stacks-ArrayImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
struct Stack
{
int top;
int capacity;
int* array;
};
struct Stack* createStack(int capacity)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
return stack;
}
//To check if stack is full or not
int isFull(struct Stack* stack)
{ return stack->top == stack->capacity - 1; }
//To check if stack is empty or not
int isEmpty(struct Stack* stack)
{ return stack->top == -1; }
//For adding new element
void push(struct Stack* stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
//For deleting the topmost element
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
//To view topmost element
int peek(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int main()
{
cout<<"Enter size of stack:";
int n,tmp;
cin>>n;
//Creation of stack
struct Stack* stack = createStack(n);
cout<<"Enter elements of stack:";
for(int i=0;i<n;i++)
{
cin>>tmp;
push(stack,tmp);
}
//Deleting topmost element
printf("%d popped from stack\n", pop(stack));
//Viewing topmost element
printf("Top item is %d\n", peek(stack));
return 0;
}