Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Q1. write a c program to implement binary search algorithm?

Ans:- A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary search
technique works only on a sorted array, so an array must be sorted to apply binary search on the array. It is a searching
technique that is better then the liner search technique as the number of iterations decreases in the binary search. The
logic behind the binary search is that there is a key. This key holds the value to be searched. The highest and the lowest
value are added and divided by 2. Highest and lowest and the first and last element in the array. The mid value is then
compared with the key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key value is less then
mid, mid-1 becomes the highest value and the process is repeated on the shortened array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

Q2. write kruskal's algorithm for finding shortest path in a graph with an example?
Anks:- Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the
graph. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
The Kruskal's algorithm is given as follows.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for
combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solution:
Q1. write a c program to implement binary search algorithm?
Ans:- A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary search
technique works only on a sorted array, so an array must be sorted to apply binary search on the array. It is a searching
technique that is better then the liner search technique as the number of iterations decreases in the binary search. The
logic behind the binary search is that there is a key. This key holds the value to be searched. The highest and the lowest
value are added and divided by 2. Highest and lowest and the first and last element in the array. The mid value is then
compared with the key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key value is less then
mid, mid-1 becomes the highest value and the process is repeated on the shortened array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

Q2. write kruskal's algorithm for finding shortest path in a graph with an example?
Anks:- Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the
graph. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
The Kruskal's algorithm is given as follows.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for
combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solution:
Q1. write a c program to implement binary search algorithm?
Ans:- A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary search
technique works only on a sorted array, so an array must be sorted to apply binary search on the array. It is a searching
technique that is better then the liner search technique as the number of iterations decreases in the binary search. The
logic behind the binary search is that there is a key. This key holds the value to be searched. The highest and the lowest
value are added and divided by 2. Highest and lowest and the first and last element in the array. The mid value is then
compared with the key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key value is less then
mid, mid-1 becomes the highest value and the process is repeated on the shortened array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

Q2. write kruskal's algorithm for finding shortest path in a graph with an example?
Anks:- Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the
graph. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
The Kruskal's algorithm is given as follows.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for
combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solution:
Q1. write a c program to implement binary search algorithm?
Ans:- A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary search
technique works only on a sorted array, so an array must be sorted to apply binary search on the array. It is a searching
technique that is better then the liner search technique as the number of iterations decreases in the binary search. The
logic behind the binary search is that there is a key. This key holds the value to be searched. The highest and the lowest
value are added and divided by 2. Highest and lowest and the first and last element in the array. The mid value is then
compared with the key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key value is less then
mid, mid-1 becomes the highest value and the process is repeated on the shortened array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

Q2. write kruskal's algorithm for finding shortest path in a graph with an example?
Anks:- Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the
graph. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
The Kruskal's algorithm is given as follows.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for
combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solution:
Q1. write a c program to implement binary search algorithm?
Ans:- A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary search
technique works only on a sorted array, so an array must be sorted to apply binary search on the array. It is a searching
technique that is better then the liner search technique as the number of iterations decreases in the binary search. The
logic behind the binary search is that there is a key. This key holds the value to be searched. The highest and the lowest
value are added and divided by 2. Highest and lowest and the first and last element in the array. The mid value is then
compared with the key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key value is less then
mid, mid-1 becomes the highest value and the process is repeated on the shortened array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

Q2. write kruskal's algorithm for finding shortest path in a graph with an example?
Anks:- Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the
graph. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
The Kruskal's algorithm is given as follows.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for
combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solution:
Q1. write a c program to implement binary search algorithm?
Ans:- A Binary Search is a sorting algorithm, that is used to search an element in a sorted array. A binary search
technique works only on a sorted array, so an array must be sorted to apply binary search on the array. It is a searching
technique that is better then the liner search technique as the number of iterations decreases in the binary search. The
logic behind the binary search is that there is a key. This key holds the value to be searched. The highest and the lowest
value are added and divided by 2. Highest and lowest and the first and last element in the array. The mid value is then
compared with the key. If mid is equal to the key, then we get the output directly. Else if the key is greater then mid then
the mid+1 becomes the lowest value and the process is repeated on the shortened array. Else if the key value is less then
mid, mid-1 becomes the highest value and the process is repeated on the shortened array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

Q2. write kruskal's algorithm for finding shortest path in a graph with an example?
Anks:- Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the
graph. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of
focusing on a global optimum.
The Kruskal's algorithm is given as follows.
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
o Step 4: Remove an edge from Q
o Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for
combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Solution:
the weight of the edges given as :

Edge AE AD AC AB BC CD DE

Weight 5 10 7 1 3 4 2
Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
the cost of MST = 1 + 2 + 3 + 4 = 10.
C Program:
#include <iostream> for(int i = 0;i < edges;++i)
#include <vector> {
#include <utility> x = p[i].second.first;
#include <algorithm> y = p[i].second.second;
using namespace std; cost = p[i].first;
const int MAX = 1e4 + 5; if(root(x) != root(y))
int id[MAX], nodes, edges; {
minimumCost += cost;
pair <long long, pair<int, int> > p[MAX];
union1(x, y);
void init()
}
{
}
for(int i = 0;i < MAX;++i)
return minimumCost;
id[i] = i;
}
}
int main()
int root(int x) {
{ int x, y;
while(id[x] != x) long long weight, cost, minimumCost;
{ init();
id[x] = id[id[x]]; cout <<"Enter Nodes and edges";
x = id[x]; cin >> nodes >> edges;
} for(int i = 0;i < edges;++i)
return x; {
} cout<<"Enter the value of X, Y and edges";
void union1(int x, int y) cin >> x >> y >> weight;
{ p[i] = make_pair(weight, make_pair(x, y));
int p = root(x); }
int q = root(y); sort(p, p + edges);
id[p] = id[q]; minimumCost = kruskal(p);
} cout <<"Minimum cost is "<< minimumCost << endl;
long long kruskal(pair<long long, pair<int, int> > p[])
{ return 0;
}
int x, y;
long long cost, minimumCost = 0;
the weight of the edges given as :

Edge AE AD AC AB BC CD DE

Weight 5 10 7 1 3 4 2
Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
the cost of MST = 1 + 2 + 3 + 4 = 10.
C Program:
#include <iostream> for(int i = 0;i < edges;++i)
#include <vector> {
#include <utility> x = p[i].second.first;
#include <algorithm> y = p[i].second.second;
using namespace std; cost = p[i].first;
const int MAX = 1e4 + 5; if(root(x) != root(y))
int id[MAX], nodes, edges; {
minimumCost += cost;
pair <long long, pair<int, int> > p[MAX];
union1(x, y);
void init()
}
{
}
for(int i = 0;i < MAX;++i)
return minimumCost;
id[i] = i;
}
}
int main()
int root(int x) {
{ int x, y;
while(id[x] != x) long long weight, cost, minimumCost;
{ init();
id[x] = id[id[x]]; cout <<"Enter Nodes and edges";
x = id[x]; cin >> nodes >> edges;
} for(int i = 0;i < edges;++i)
return x; {
} cout<<"Enter the value of X, Y and edges";
void union1(int x, int y) cin >> x >> y >> weight;
{ p[i] = make_pair(weight, make_pair(x, y));
int p = root(x); }
int q = root(y); sort(p, p + edges);
id[p] = id[q]; minimumCost = kruskal(p);
} cout <<"Minimum cost is "<< minimumCost << endl;
long long kruskal(pair<long long, pair<int, int> > p[])
{ return 0;
}
int x, y;
long long cost, minimumCost = 0;
the weight of the edges given as :

Edge AE AD AC AB BC CD DE

Weight 5 10 7 1 3 4 2
Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
the cost of MST = 1 + 2 + 3 + 4 = 10.
C Program:
#include <iostream> for(int i = 0;i < edges;++i)
#include <vector> {
#include <utility> x = p[i].second.first;
#include <algorithm> y = p[i].second.second;
using namespace std; cost = p[i].first;
const int MAX = 1e4 + 5; if(root(x) != root(y))
int id[MAX], nodes, edges; {
minimumCost += cost;
pair <long long, pair<int, int> > p[MAX];
union1(x, y);
void init()
}
{
}
for(int i = 0;i < MAX;++i)
return minimumCost;
id[i] = i;
}
}
int main()
int root(int x) {
{ int x, y;
while(id[x] != x) long long weight, cost, minimumCost;
{ init();
id[x] = id[id[x]]; cout <<"Enter Nodes and edges";
x = id[x]; cin >> nodes >> edges;
} for(int i = 0;i < edges;++i)
return x; {
} cout<<"Enter the value of X, Y and edges";
void union1(int x, int y) cin >> x >> y >> weight;
{ p[i] = make_pair(weight, make_pair(x, y));
int p = root(x); }
int q = root(y); sort(p, p + edges);
id[p] = id[q]; minimumCost = kruskal(p);
} cout <<"Minimum cost is "<< minimumCost << endl;
long long kruskal(pair<long long, pair<int, int> > p[])
{ return 0;
}
int x, y;
long long cost, minimumCost = 0;
the weight of the edges given as :

Edge AE AD AC AB BC CD DE

Weight 5 10 7 1 3 4 2
Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
the cost of MST = 1 + 2 + 3 + 4 = 10.
C Program:
#include <iostream> for(int i = 0;i < edges;++i)
#include <vector> {
#include <utility> x = p[i].second.first;
#include <algorithm> y = p[i].second.second;
using namespace std; cost = p[i].first;
const int MAX = 1e4 + 5; if(root(x) != root(y))
int id[MAX], nodes, edges; {
minimumCost += cost;
pair <long long, pair<int, int> > p[MAX];
union1(x, y);
void init()
}
{
}
for(int i = 0;i < MAX;++i)
return minimumCost;
id[i] = i;
}
}
int main()
int root(int x) {
{ int x, y;
while(id[x] != x) long long weight, cost, minimumCost;
{ init();
id[x] = id[id[x]]; cout <<"Enter Nodes and edges";
x = id[x]; cin >> nodes >> edges;
} for(int i = 0;i < edges;++i)
return x; {
} cout<<"Enter the value of X, Y and edges";
void union1(int x, int y) cin >> x >> y >> weight;
{ p[i] = make_pair(weight, make_pair(x, y));
int p = root(x); }
int q = root(y); sort(p, p + edges);
id[p] = id[q]; minimumCost = kruskal(p);
} cout <<"Minimum cost is "<< minimumCost << endl;
long long kruskal(pair<long long, pair<int, int> > p[])
{ return 0;
}
int x, y;
long long cost, minimumCost = 0;
the weight of the edges given as :

Edge AE AD AC AB BC CD DE

Weight 5 10 7 1 3 4 2
Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
the cost of MST = 1 + 2 + 3 + 4 = 10.
C Program:
#include <iostream> for(int i = 0;i < edges;++i)
#include <vector> {
#include <utility> x = p[i].second.first;
#include <algorithm> y = p[i].second.second;
using namespace std; cost = p[i].first;
const int MAX = 1e4 + 5; if(root(x) != root(y))
int id[MAX], nodes, edges; {
minimumCost += cost;
pair <long long, pair<int, int> > p[MAX];
union1(x, y);
void init()
}
{
}
for(int i = 0;i < MAX;++i)
return minimumCost;
id[i] = i;
}
}
int main()
int root(int x) {
{ int x, y;
while(id[x] != x) long long weight, cost, minimumCost;
{ init();
id[x] = id[id[x]]; cout <<"Enter Nodes and edges";
x = id[x]; cin >> nodes >> edges;
} for(int i = 0;i < edges;++i)
return x; {
} cout<<"Enter the value of X, Y and edges";
void union1(int x, int y) cin >> x >> y >> weight;
{ p[i] = make_pair(weight, make_pair(x, y));
int p = root(x); }
int q = root(y); sort(p, p + edges);
id[p] = id[q]; minimumCost = kruskal(p);
} cout <<"Minimum cost is "<< minimumCost << endl;
long long kruskal(pair<long long, pair<int, int> > p[])
{ return 0;
}
int x, y;
long long cost, minimumCost = 0;
the weight of the edges given as :

Edge AE AD AC AB BC CD DE

Weight 5 10 7 1 3 4 2
Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD

Weight 1 2 3 4 5 7 10

The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.
Hence, the final MST is the one which is shown in the step 4.
the cost of MST = 1 + 2 + 3 + 4 = 10.
C Program:
#include <iostream> for(int i = 0;i < edges;++i)
#include <vector> {
#include <utility> x = p[i].second.first;
#include <algorithm> y = p[i].second.second;
using namespace std; cost = p[i].first;
const int MAX = 1e4 + 5; if(root(x) != root(y))
int id[MAX], nodes, edges; {
minimumCost += cost;
pair <long long, pair<int, int> > p[MAX];
union1(x, y);
void init()
}
{
}
for(int i = 0;i < MAX;++i)
return minimumCost;
id[i] = i;
}
}
int main()
int root(int x) {
{ int x, y;
while(id[x] != x) long long weight, cost, minimumCost;
{ init();
id[x] = id[id[x]]; cout <<"Enter Nodes and edges";
x = id[x]; cin >> nodes >> edges;
} for(int i = 0;i < edges;++i)
return x; {
} cout<<"Enter the value of X, Y and edges";
void union1(int x, int y) cin >> x >> y >> weight;
{ p[i] = make_pair(weight, make_pair(x, y));
int p = root(x); }
int q = root(y); sort(p, p + edges);
id[p] = id[q]; minimumCost = kruskal(p);
} cout <<"Minimum cost is "<< minimumCost << endl;
long long kruskal(pair<long long, pair<int, int> > p[])
{ return 0;
}
int x, y;
long long cost, minimumCost = 0;
Q3.Ans:- #include <stdio.h> display(); printf("%d", front1-
#include <stdlib.h> break; >info);
struct node case 7: }
{ queuesize(); void deq()
int info; break; {
struct node *ptr; default: front1 = front;
}*front,*rear,*temp,*front1; printf("Wrong
choice, Please enter correct if (front1 == NULL)
int frontelement(); choice "); {
void enq(int data); break; printf("\n Error:
void deq(); } Trying to display elements
void empty(); } from empty queue");
void display(); } return;
void create(); void create() }
void queuesize(); { else
front = rear = NULL; if (front1->ptr !=
int count = 0; } NULL)
void queuesize() {
void main() { front1 = front1-
{ printf("\n Queue >ptr;
int no, ch, e; size : %d", count); printf("\n Dequed
} value : %d", front->info);
printf("\n 1 - Enque"); void enq(int data) free(front);
printf("\n 2 - Deque"); { front = front1;
printf("\n 3 - Front if (rear == NULL) }
element"); { else
printf("\n 4 - Empty"); rear = (struct node {
printf("\n 5 - Exit"); *)malloc(1*sizeof(struct printf("\n Dequed
printf("\n 6 - Display"); node)); value : %d", front->info);
printf("\n 7 - Queue rear->ptr = NULL; free(front);
size"); rear->info = data; front = NULL;
create(); front = rear; rear = NULL;
while (1) } }
{ else count--;
printf("\n Enter { }
choice : "); temp=(struct node
scanf("%d", &ch); *)malloc(1*sizeof(struct int frontelement()
switch (ch) node)); {
{ rear->ptr = temp; if ((front != NULL) &&
case 1: temp->info = data; (rear != NULL))
printf("Enter temp->ptr = NULL; return(front->info);
data : "); else
scanf("%d", &no); rear = temp; return 0;
enq(no); } }
break; count++; void empty()
case 2: } {
deq(); void display() if ((front == NULL) &&
break; { (rear == NULL))
case 3: front1 = front; printf("\n Queue
e = empty");
frontelement(); if ((front1 == NULL) && else
if (e != 0) (rear == NULL)) printf("Queue not
printf("Front { empty");}
element : %d", e); printf("Queue is
else empty");
printf("\n No return;
front element in Queue as }
queue is empty"); while (front1 != rear)
break; {
case 4: printf("%d ", front1-
empty(); >info);
break; front1 = front1->ptr;
case 5: }
exit(0); if (front1 == rear)
case 6:
Q3.Ans:- #include <stdio.h> case 5: printf("%d ",
#include <stdlib.h> exit(0); front1->info);
struct node case 6: front1 = front1->ptr;
{ display(); }
int info; break; if (front1 == rear)
struct node *ptr; case 7: printf("%d", front1-
}*front,*rear,*temp,*front1; queuesize(); >info);
break; }
int frontelement(); default: void deq()
void enq(int data); printf("Wrong {
void deq(); choice, Please enter correct front1 = front;
void empty(); choice ");
void display(); break; if (front1 == NULL)
void create(); } {
void queuesize(); } printf("\n Error:
} Trying to display elements
int count = 0; void create() from empty queue");
{ return;
void main() front = rear = NULL; }
{ } else
int no, ch, e; void queuesize() if (front1->ptr !=
{ NULL)
printf("\n 1 - Enque"); printf("\n Queue {
printf("\n 2 - Deque"); size : %d", count); front1 = front1-
printf("\n 3 - Front } >ptr;
element"); void enq(int data) printf("\n Dequed
printf("\n 4 - Empty"); { value : %d", front->info);
printf("\n 5 - Exit"); if (rear == NULL) free(front);
printf("\n 6 - Display"); { front = front1;
printf("\n 7 - Queue rear = (struct node }
size"); *)malloc(1*sizeof(struct else
create(); node)); {
while (1) rear->ptr = NULL; printf("\n
{ rear->info = data; Dequed value : %d", front-
printf("\n Enter front = rear; >info);
choice : "); } free(front);
scanf("%d", &ch); else front = NULL;
switch (ch) { rear = NULL;
{ temp=(struct node }
case 1: *)malloc(1*sizeof(struct count--;
printf("Enter node)); }
data : "); rear->ptr = temp;
scanf("%d", &no); temp->info = data; int frontelement()
enq(no); temp->ptr = NULL; {
break; if ((front != NULL) &&
case 2: rear = temp; (rear != NULL))
deq(); } return(front->info);
break; count++; else
case 3: } return 0;
e = void display() }
frontelement(); { void empty()
if (e != 0) front1 = front; {
printf("Front if ((front == NULL) &&
element : %d", e); if ((front1 == NULL) && (rear == NULL))
else (rear == NULL)) printf("\n Queue
printf("\n { empty");
No front element in Queue as printf("Queue is else
queue is empty"); empty"); printf("Queue not
break; return; empty");}
case 4: }
empty(); while (front1 != rear)
break; {
Q3.Ans:- #include <stdio.h> case 5: printf("%d ",
#include <stdlib.h> exit(0); front1->info);
struct node case 6: front1 = front1->ptr;
{ display(); }
int info; break; if (front1 == rear)
struct node *ptr; case 7: printf("%d", front1-
}*front,*rear,*temp,*front1; queuesize(); >info);
break; }
int frontelement(); default: void deq()
void enq(int data); printf("Wrong {
void deq(); choice, Please enter correct front1 = front;
void empty(); choice ");
void display(); break; if (front1 == NULL)
void create(); } {
void queuesize(); } printf("\n Error:
} Trying to display elements
int count = 0; void create() from empty queue");
{ return;
void main() front = rear = NULL; }
{ } else
int no, ch, e; void queuesize() if (front1->ptr !=
{ NULL)
printf("\n 1 - Enque"); printf("\n Queue {
printf("\n 2 - Deque"); size : %d", count); front1 = front1-
printf("\n 3 - Front } >ptr;
element"); void enq(int data) printf("\n Dequed
printf("\n 4 - Empty"); { value : %d", front->info);
printf("\n 5 - Exit"); if (rear == NULL) free(front);
printf("\n 6 - Display"); { front = front1;
printf("\n 7 - Queue rear = (struct node }
size"); *)malloc(1*sizeof(struct else
create(); node)); {
while (1) rear->ptr = NULL; printf("\n
{ rear->info = data; Dequed value : %d", front-
printf("\n Enter front = rear; >info);
choice : "); } free(front);
scanf("%d", &ch); else front = NULL;
switch (ch) { rear = NULL;
{ temp=(struct node }
case 1: *)malloc(1*sizeof(struct count--;
printf("Enter node)); }
data : "); rear->ptr = temp;
scanf("%d", &no); temp->info = data; int frontelement()
enq(no); temp->ptr = NULL; {
break; if ((front != NULL) &&
case 2: rear = temp; (rear != NULL))
deq(); } return(front->info);
break; count++; else
case 3: } return 0;
e = void display() }
frontelement(); { void empty()
if (e != 0) front1 = front; {
printf("Front if ((front == NULL) &&
element : %d", e); if ((front1 == NULL) && (rear == NULL))
else (rear == NULL)) printf("\n Queue
printf("\n { empty");
No front element in Queue as printf("Queue is else
queue is empty"); empty"); printf("Queue not
break; return; empty");}
case 4: }
empty(); while (front1 != rear)
break; {
Q3.Ans:- #include <stdio.h> case 5: printf("%d ",
#include <stdlib.h> exit(0); front1->info);
struct node case 6: front1 = front1->ptr;
{ display(); }
int info; break; if (front1 == rear)
struct node *ptr; case 7: printf("%d", front1-
}*front,*rear,*temp,*front1; queuesize(); >info);
break; }
int frontelement(); default: void deq()
void enq(int data); printf("Wrong {
void deq(); choice, Please enter correct front1 = front;
void empty(); choice ");
void display(); break; if (front1 == NULL)
void create(); } {
void queuesize(); } printf("\n Error:
} Trying to display elements
int count = 0; void create() from empty queue");
{ return;
void main() front = rear = NULL; }
{ } else
int no, ch, e; void queuesize() if (front1->ptr !=
{ NULL)
printf("\n 1 - Enque"); printf("\n Queue {
printf("\n 2 - Deque"); size : %d", count); front1 = front1-
printf("\n 3 - Front } >ptr;
element"); void enq(int data) printf("\n Dequed
printf("\n 4 - Empty"); { value : %d", front->info);
printf("\n 5 - Exit"); if (rear == NULL) free(front);
printf("\n 6 - Display"); { front = front1;
printf("\n 7 - Queue rear = (struct node }
size"); *)malloc(1*sizeof(struct else
create(); node)); {
while (1) rear->ptr = NULL; printf("\n
{ rear->info = data; Dequed value : %d", front-
printf("\n Enter front = rear; >info);
choice : "); } free(front);
scanf("%d", &ch); else front = NULL;
switch (ch) { rear = NULL;
{ temp=(struct node }
case 1: *)malloc(1*sizeof(struct count--;
printf("Enter node)); }
data : "); rear->ptr = temp;
scanf("%d", &no); temp->info = data; int frontelement()
enq(no); temp->ptr = NULL; {
break; if ((front != NULL) &&
case 2: rear = temp; (rear != NULL))
deq(); } return(front->info);
break; count++; else
case 3: } return 0;
e = void display() }
frontelement(); { void empty()
if (e != 0) front1 = front; {
printf("Front if ((front == NULL) &&
element : %d", e); if ((front1 == NULL) && (rear == NULL))
else (rear == NULL)) printf("\n Queue
printf("\n { empty");
No front element in Queue as printf("Queue is else
queue is empty"); empty"); printf("Queue not
break; return; empty");}
case 4: }
empty(); while (front1 != rear)
break; {
Q3.Ans:- #include <stdio.h> case 5: printf("%d ",
#include <stdlib.h> exit(0); front1->info);
struct node case 6: front1 = front1->ptr;
{ display(); }
int info; break; if (front1 == rear)
struct node *ptr; case 7: printf("%d", front1-
}*front,*rear,*temp,*front1; queuesize(); >info);
break; }
int frontelement(); default: void deq()
void enq(int data); printf("Wrong {
void deq(); choice, Please enter correct front1 = front;
void empty(); choice ");
void display(); break; if (front1 == NULL)
void create(); } {
void queuesize(); } printf("\n Error:
} Trying to display elements
int count = 0; void create() from empty queue");
{ return;
void main() front = rear = NULL; }
{ } else
int no, ch, e; void queuesize() if (front1->ptr !=
{ NULL)
printf("\n 1 - Enque"); printf("\n Queue {
printf("\n 2 - Deque"); size : %d", count); front1 = front1-
printf("\n 3 - Front } >ptr;
element"); void enq(int data) printf("\n Dequed
printf("\n 4 - Empty"); { value : %d", front->info);
printf("\n 5 - Exit"); if (rear == NULL) free(front);
printf("\n 6 - Display"); { front = front1;
printf("\n 7 - Queue rear = (struct node }
size"); *)malloc(1*sizeof(struct else
create(); node)); {
while (1) rear->ptr = NULL; printf("\n
{ rear->info = data; Dequed value : %d", front-
printf("\n Enter front = rear; >info);
choice : "); } free(front);
scanf("%d", &ch); else front = NULL;
switch (ch) { rear = NULL;
{ temp=(struct node }
case 1: *)malloc(1*sizeof(struct count--;
printf("Enter node)); }
data : "); rear->ptr = temp;
scanf("%d", &no); temp->info = data; int frontelement()
enq(no); temp->ptr = NULL; {
break; if ((front != NULL) &&
case 2: rear = temp; (rear != NULL))
deq(); } return(front->info);
break; count++; else
case 3: } return 0;
e = void display() }
frontelement(); { void empty()
if (e != 0) front1 = front; {
printf("Front if ((front == NULL) &&
element : %d", e); if ((front1 == NULL) && (rear == NULL))
else (rear == NULL)) printf("\n Queue
printf("\n { empty");
No front element in Queue as printf("Queue is else
queue is empty"); empty"); printf("Queue not
break; return; empty");}
case 4: }
empty(); while (front1 != rear)
break; {
Q3.Ans:- #include <stdio.h> break; while (front1 != rear)
#include <stdlib.h> case 5: {
struct node exit(0); printf("%d ",
{ case 6: front1->info);
int info; display(); front1 = front1->ptr;
struct node *ptr; break; }
}*front,*rear,*temp,*front1; case 7: if (front1 == rear)
queuesize(); printf("%d", front1-
int frontelement(); break; >info);
void enq(int data); default: }
void deq(); printf("Wrong void deq()
void empty(); choice, Please enter correct {
void display(); choice "); front1 = front;
void create(); break;
void queuesize(); } if (front1 == NULL)
} {
int count = 0; } printf("\n Error:
void create() Trying to display elements
void main() { from empty queue");
{ front = rear = NULL; return;
int no, ch, e; } }
void queuesize() else
printf("\n 1 - Enque"); { if (front1->ptr !=
printf("\n 2 - Deque"); printf("\n Queue NULL)
printf("\n 3 - Front size : %d", count); {
element"); } front1 = front1-
printf("\n 4 - Empty"); void enq(int data) >ptr;
printf("\n 5 - Exit"); { printf("\n Dequed
printf("\n 6 - Display"); if (rear == NULL) value : %d", front->info);
printf("\n 7 - Queue { free(front);
size"); rear = (struct node front = front1;
create(); *)malloc(1*sizeof(struct }
while (1) node)); else
{ rear->ptr = NULL; {
printf("\n Enter rear->info = data; printf("\n
choice : "); front = rear; Dequed value : %d", front-
scanf("%d", &ch); } >info);
switch (ch) else free(front);
{ { front = NULL;
case 1: temp=(struct node rear = NULL;
printf("Enter *)malloc(1*sizeof(struct }
data : "); node)); count--;
scanf("%d", &no); rear->ptr = temp; }
enq(no); temp->info = data;
break; temp->ptr = NULL; int frontelement()
case 2: {
deq(); rear = temp; if ((front != NULL) &&
break; } (rear != NULL))
case 3: count++; return(front->info);
e = } else
frontelement(); void display() return 0;
if (e != 0) { }
printf("Front front1 = front; void empty()
element : %d", e); {
else if ((front1 == NULL) && if ((front == NULL) &&
printf("\n (rear == NULL)) (rear == NULL))
No front element in Queue as { printf("\n Queue
queue is empty"); printf("Queue is empty");
break; empty"); else
case 4: return; printf("Queue not
empty(); } empty");}
5. What is stack? Write algorithm of push and pop operation.Stack is a linear data
structure that follows a particular order in which the operations are performed. The order
may be LIFO(Last In First Out) or FILO(First In Last Out)
Mainly the following three basic operations are performed in the stack: (a)Push: Adds an
item in the stack. If the stack is full, then it is said to be an Overflow condition.
(b)Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow condition.(c)
Peek or Top: Returns the top element of the stack. (d) isEmpty: Returns true if the stack is
empty, else false.

Time Complexities of operations on stack:- push(), pop(), isEmpty() and peek() all take
O(1) time. We do not run any loop in any of these operations.
Implementing Stack using Arrays.
// C program for array implementation of stack
#include <limits.h>#include <stdio.h>#include <stdlib.h> // A structure to represent a
stack struct Stack { int top; unsigned capacity; int* array; };// function to create a stack
of given capacity. It initializes size of // stack as 0*/
struct Stack* createStack(unsigned 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;}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack){ return stack->top == stack->capacity - 1; }// Stack is empty
when top is equal to -1*/ int isEmpty(struct Stack* stack) { return stack->top == -1; } //
Function to add an item to stack. It increases top by 1*/ void push(struct Stack* stack, int
item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to
stack\n", item); } // Function to remove an item from stack. It decreases top by 1 */
int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack-
>array[stack->top--]; } // Function to return the top from stack without removing it */
int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return
stack->array[stack->top]; } // Driver program to test above functions */ int main() { struct
Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30);
printf("%d popped from stack\n", pop(stack)); return 0; }
5. What is stack? Write algorithm of push and pop operation.Stack is a linear data
structure that follows a particular order in which the operations are performed. The order
may be LIFO(Last In First Out) or FILO(First In Last Out)
Mainly the following three basic operations are performed in the stack: (a)Push: Adds an
item in the stack. If the stack is full, then it is said to be an Overflow condition.
(b)Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow condition.(c)
Peek or Top: Returns the top element of the stack. (d) isEmpty: Returns true if the stack is
empty, else false.

Time Complexities of operations on stack:- push(), pop(), isEmpty() and peek() all take
O(1) time. We do not run any loop in any of these operations.
Implementing Stack using Arrays.
// C program for array implementation of stack
#include <limits.h>#include <stdio.h>#include <stdlib.h> // A structure to represent a
stack struct Stack { int top; unsigned capacity; int* array; };// function to create a stack
of given capacity. It initializes size of // stack as 0*/
struct Stack* createStack(unsigned 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;}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack){ return stack->top == stack->capacity - 1; }// Stack is empty
when top is equal to -1*/ int isEmpty(struct Stack* stack) { return stack->top == -1; } //
Function to add an item to stack. It increases top by 1*/ void push(struct Stack* stack, int
item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to
stack\n", item); } // Function to remove an item from stack. It decreases top by 1 */
int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack-
>array[stack->top--]; } // Function to return the top from stack without removing it */
int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return
stack->array[stack->top]; } // Driver program to test above functions */ int main() { struct
Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30);
printf("%d popped from stack\n", pop(stack)); return 0; }
5. What is stack? Write algorithm of push and pop operation.Stack is a linear data
structure that follows a particular order in which the operations are performed. The order
may be LIFO(Last In First Out) or FILO(First In Last Out)
Mainly the following three basic operations are performed in the stack: (a)Push: Adds an
item in the stack. If the stack is full, then it is said to be an Overflow condition.
(b)Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow condition.(c)
Peek or Top: Returns the top element of the stack. (d) isEmpty: Returns true if the stack is
empty, else false.

Time Complexities of operations on stack:- push(), pop(), isEmpty() and peek() all take
O(1) time. We do not run any loop in any of these operations.
Implementing Stack using Arrays.
// C program for array implementation of stack
#include <limits.h>#include <stdio.h>#include <stdlib.h> // A structure to represent a
stack struct Stack { int top; unsigned capacity; int* array; };// function to create a stack
of given capacity. It initializes size of // stack as 0*/
struct Stack* createStack(unsigned 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;}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack){ return stack->top == stack->capacity - 1; }// Stack is empty
when top is equal to -1*/ int isEmpty(struct Stack* stack) { return stack->top == -1; } //
Function to add an item to stack. It increases top by 1*/ void push(struct Stack* stack, int
item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to
stack\n", item); } // Function to remove an item from stack. It decreases top by 1 */
int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack-
>array[stack->top--]; } // Function to return the top from stack without removing it */
int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return
stack->array[stack->top]; } // Driver program to test above functions */ int main() { struct
Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30);
printf("%d popped from stack\n", pop(stack)); return 0; }
5. What is stack? Write algorithm of push and pop operation.Stack is a linear data
structure that follows a particular order in which the operations are performed. The order
may be LIFO(Last In First Out) or FILO(First In Last Out)
Mainly the following three basic operations are performed in the stack: (a)Push: Adds an
item in the stack. If the stack is full, then it is said to be an Overflow condition.
(b)Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow condition.(c)
Peek or Top: Returns the top element of the stack. (d) isEmpty: Returns true if the stack is
empty, else false.

Time Complexities of operations on stack:- push(), pop(), isEmpty() and peek() all take
O(1) time. We do not run any loop in any of these operations.
Implementing Stack using Arrays.
// C program for array implementation of stack
#include <limits.h>#include <stdio.h>#include <stdlib.h> // A structure to represent a
stack struct Stack { int top; unsigned capacity; int* array; };// function to create a stack
of given capacity. It initializes size of // stack as 0*/
struct Stack* createStack(unsigned 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;}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack){ return stack->top == stack->capacity - 1; }// Stack is empty
when top is equal to -1*/ int isEmpty(struct Stack* stack) { return stack->top == -1; } //
Function to add an item to stack. It increases top by 1*/ void push(struct Stack* stack, int
item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to
stack\n", item); } // Function to remove an item from stack. It decreases top by 1 */
int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack-
>array[stack->top--]; } // Function to return the top from stack without removing it */
int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return
stack->array[stack->top]; } // Driver program to test above functions */ int main() { struct
Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30);
printf("%d popped from stack\n", pop(stack)); return 0; }
5. What is stack? Write algorithm of push and pop operation.Stack is a linear data
structure that follows a particular order in which the operations are performed. The order
may be LIFO(Last In First Out) or FILO(First In Last Out)
Mainly the following three basic operations are performed in the stack: (a)Push: Adds an
item in the stack. If the stack is full, then it is said to be an Overflow condition.
(b)Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow condition.(c)
Peek or Top: Returns the top element of the stack. (d) isEmpty: Returns true if the stack is
empty, else false.

Time Complexities of operations on stack:- push(), pop(), isEmpty() and peek() all take
O(1) time. We do not run any loop in any of these operations.
Implementing Stack using Arrays.
// C program for array implementation of stack
#include <limits.h>#include <stdio.h>#include <stdlib.h> // A structure to represent a
stack struct Stack { int top; unsigned capacity; int* array; };// function to create a stack
of given capacity. It initializes size of // stack as 0*/
struct Stack* createStack(unsigned 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;}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack){ return stack->top == stack->capacity - 1; }// Stack is empty
when top is equal to -1*/ int isEmpty(struct Stack* stack) { return stack->top == -1; } //
Function to add an item to stack. It increases top by 1*/ void push(struct Stack* stack, int
item) { if (isFull(stack)) return; stack->array[++stack->top] = item; printf("%d pushed to
stack\n", item); } // Function to remove an item from stack. It decreases top by 1 */
int pop(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return stack-
>array[stack->top--]; } // Function to return the top from stack without removing it */
int peek(struct Stack* stack) { if (isEmpty(stack)) return INT_MIN; return
stack->array[stack->top]; } // Driver program to test above functions */ int main() { struct
Stack* stack = createStack(100); push(stack, 10); push(stack, 20); push(stack, 30);
printf("%d popped from stack\n", pop(stack)); return 0; }

You might also like