C# Corner
Tech
News
Videos
Forums
Trainings
Books
Live
More
Interviews
Events
Jobs
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Sort Using Stacks
WhatsApp
Umarul Farook
Jan 13
2016
867
0
1
#include < stdio.h >
int
stack1[100], stack2[100];
int
top1 = 0, top2 = 0;
void
insert();
void
show();
main() {
int
ch;
do
{
printf(
"\n1 : Insert "
);
printf(
"\n2 : Check "
);
printf(
"\n3 : Exit "
);
printf(
"\nPlease enter your choice ?\n"
);
scanf(
"%d"
, & ch);
switch
(ch) {
case
1:
insert();
break
;
case
2:
show();
break
;
case
3:
printf(
"The program ended"
);
break
;
default
:
printf(
"\nInvalid choice is entered..........!!\n"
);
break
;
}
}
while
(ch != 3);
}
void
insert()
{
int
i;
top1 = top1 + 1;
printf(
"Enter the data to insert ?\n"
);
scanf(
"%d"
, & stack1[top1]);
printf(
"The data inserted Successfully..\n"
);
if
(top1 >= 2)
{
while
(top1 > 1) {
if
(stack1[top1 - 1] > stack1[top1]) {
top2 = top2 + 1;
stack2[top2] = stack1[top1 - 1];
stack1[top1 - 1] = stack1[top1];
top1 = top1 - 1;
}
else
break
;
}
while
(top2 >= 1)
{
top1 = top1 + 1;
stack1[top1] = stack2[top2];
top2 = top2 - 1;
}
}
}
void
show()
{
int
i;
printf(
"\nThe stack elements are.....\n"
);
for
(i = top1; i >= 1; --i)
{
printf(
"[ %d ]\n"
, stack1[i]);
}
}
SORT
STACK
Up Next
Sort Using Stacks