-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubset_generation.c
148 lines (124 loc) · 3.58 KB
/
subset_generation.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
/*----------------------------------------------------------------------------*/
typedef struct all_subsets_node
{
int* subset;
int size;
struct all_subsets_node* link;
}ALL_SUBSETS;
ALL_SUBSETS* all_subsets; // creating an instance of struct all_subsets_node
ALL_SUBSETS* createNodeArr(int* data)
{
ALL_SUBSETS* newNode = (ALL_SUBSETS*)malloc(sizeof(ALL_SUBSETS));
newNode -> subset = data;
newNode -> link = NULL;
return newNode;
}
ALL_SUBSETS* insertFrontArr(ALL_SUBSETS* head, int* data)
{
ALL_SUBSETS* newNode = createNodeArr(data);
newNode -> link = head;
head = newNode;
return head;
}
void display(ALL_SUBSETS* head)
{
while (head != NULL)
{
int* arr = head -> subset;
int size = head -> size;
for(int loop = 0; loop < size; loop++)
{
printf("%d ", arr[loop]);
}
printf("\n\n");
head = head->link;
}
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
typedef struct subset
{
int data;
struct subset* link;
}SUBSET;
SUBSET* subset; // creating an instance of struct subset
SUBSET* createNodeInt(int data)
{
SUBSET* newNode = (SUBSET*)malloc(sizeof(SUBSET));
newNode -> data = data;
newNode -> link = NULL;
return newNode;
}
SUBSET* insertFrontInt(SUBSET* head, int data)
{
SUBSET* newNode = createNodeInt(data);
newNode -> link = head;
head = newNode;
return head;
}
SUBSET* deleteFrontInt(SUBSET* head)
{
SUBSET* next = head -> link;
free(head);
head = NULL;
head = next;
return head;
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
// initialising a global variable to store the size of the array we calculate here
int size_of_subset = 0;
// we need a function to convert a linked list to an array
int* list_to_array(SUBSET* head)
{
// Parsing through the list to find out the required size of our array
int size = 0;
SUBSET* head_copy = head;
while(head_copy != NULL)
{
head_copy = head_copy -> link;
size++;
}
size_of_subset = size;
// converting list to array
int* arr = (int*)malloc(size*sizeof(int));
int count = 0;
while (head != NULL)
{
arr[count] = head -> data;
head = head->link;
count++;
}
return arr;
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void search(int k)
{
if (k == 4)
{
// Converting current subset from a linked list to an array
int* cur_sub = list_to_array(subset);
// Adding cur_sub to final output ALL_SUBSETS
all_subsets = insertFrontArr(all_subsets, cur_sub);
all_subsets -> size = size_of_subset;
}
else
{
// add k to the subset
subset = insertFrontInt(subset, k);
search(k + 1);
// remove k from the subset
subset = deleteFrontInt(subset);
search(k + 1);
}
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void main()
{
search(1);
display(all_subsets);
}