TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Sort Using Stacks
Umarul Farook
Jan 13
2016
Code
793
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
#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