-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 6 assignment.txt
108 lines (100 loc) · 1.64 KB
/
Day 6 assignment.txt
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Question 1]
int maxfun()
{
int temp=stack[top];
for(int i=top-1;i>=0;i--)
{
if(stack[i]>temp)
{
temp=stack[i];
}
}
return temp;
}
Question 2]
int minfun()
{
int temp=stack[top];
for(int i=top-1;i>=0;i--)
{
if(stack[i]<temp)
{
temp=stack[i];
}
}
return temp;
}
//C program implementation
#include<stdio.h>
#define MAX 50
void push(int);
void display();
int maxfun();
int minfun();
int top=-1;
int stack[MAX];
void main()
{
int n,ele;
printf("Enter the size of stack: ");
scanf("%d",&n);
printf("Enter the stack elements: ");
for(int i=0;i<n;i++)
{
scanf("%d",&ele);
push(ele);
}
printf("Stack elements are: \n");
display();
printf("\nMaximum element is: %d",maxfun());
printf("\nMinimum element is: %d",minfun());
}
void push(int ele)
{
if(top==MAX-1)
{
printf("Overflow");
return;
}
top++;
stack[top]=ele;
}
int maxfun()
{
int temp=stack[top];
for(int i=top-1;i>=0;i--)
{
if(stack[i]>temp)
{
temp=stack[i];
}
}
return temp;
}
int minfun()
{
int temp=stack[top];
for(int i=top-1;i>=0;i--)
{
if(stack[i]<temp)
{
temp=stack[i];
}
}
return temp;
}
void display()
{
if(top==-1)
{
printf("Underflow");
return;
}
int temp;
temp=top;
while(temp!=-1)
{
printf("%d\t",stack[temp]);
temp--;
}
}