-
Notifications
You must be signed in to change notification settings - Fork 15
/
Sorting_stack.c
103 lines (91 loc) · 1.56 KB
/
Sorting_stack.c
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
#include <stdio.h>
#include <stdlib.h>
struct Stack
{
int data;
struct Stack *next;
}*input=NULL , *tmpStack=NULL;
void push(struct Stack **st,int x)
{
struct Stack *t;
t = (struct Stack *)malloc(sizeof(struct Stack));
if (st == NULL)
{
printf("Stack is full \n");
}
else
{
t->data =x;
t->next = *st;
*st = t;
}
}
int pop(struct Stack **st)
{
struct Stack *t;
t=(struct Stack *)malloc(sizeof(struct Stack));;
int x;
if (st == NULL)
{
printf("stack is empty");
}
else
{
t = *st;
*st=(*st)->next;
x = t->data;
free(t);
}
return x;
}
void display(struct Stack *st)
{
struct Stack *p;
p = st;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void sort(){
while(input){
int temp=pop(&input);
while(tmpStack && tmpStack->data < temp ){
push(&input,pop(&tmpStack));
}
push(&tmpStack,temp);
}
}
void recursuion()
{
while(input){
int temp=pop(&input);
push(&tmpStack,temp);
}
}
void clone()
{
while(tmpStack)
{
int temp=pop(&tmpStack);
push(&input,temp);
}
}
int main(){
int n;
printf("Enter the value of n ");
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
printf("Enter element for stack ");
int a;
scanf("%d",&a);
push(&input,a);
}
display(input);
sort();
display(tmpStack);
return 0;
}