-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion66.c
108 lines (85 loc) · 1.81 KB
/
question66.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
104
105
106
107
108
/*
http://practice.geeksforgeeks.org/problems/sort-a-stack
Given a stack, sort it using recursion.
Use of any loop constructs like while, for..etc is not allowed.
We can only use the following ADT functions on Stack S:
is_empty(S) : Tests whether stack is empty or not.
push(S) : Adds new element to the stack.
pop(S) : Removes top element from the stack.
top(S) : Returns value of the top element. Note that this
function does not remove element from the stack.
*/
#include <stdio.h>
#include <stdlib.h>
//array of linked list using this struct
struct stack{
int data;
struct stack *next;
};
struct stack *head = NULL;
int top() {
struct stack *temp = head;
return temp->data;
}
int is_empty(){
return head == NULL;
}
struct stack *newNode(int data){
struct stack *temp = (struct stack *)malloc(sizeof(struct stack));
temp->data = data;
temp->next = NULL;
return temp;
}
void push(int data){
struct stack *temp = newNode(data);
if(head){
temp->next = head;
}
head = temp;
}
int pop() {
struct stack *temp = head;
head = head->next;
struct stack *result = temp;
free(temp);
return result->data;
}
void displayStack(){
struct stack *temp = head;
while(temp){
printf("%d\n", temp->data);
temp = temp->next;
}
}
void insertAfterSorting(int elm) {
//if empty just push
if(is_empty() || elm > top()) {
push(elm);
return;
}
//if not then pop the element and try inserting this one again
int x = pop();
insertAfterSorting(elm);
//push all the popped elements as recursion stack pops off
push(x);
}
void sort() {
if(!head){
return;
}
int elm = pop();
sort();
insertAfterSorting(elm);
}
int main(){
push(30);
push(-5);
push(18);
push(14);
push(-3);
displayStack();
sort();
printf("\nafter sort..\n");
displayStack();
return 0;
}