Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
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
843
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]);
}
}
STACK
SORT
Up Next
Sort Using Stacks