-
Notifications
You must be signed in to change notification settings - Fork 1
/
test1.c
182 lines (158 loc) · 3.76 KB
/
test1.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* src/tutorial/complex.c
*
******************************************************************************
This file contains routines that can be bound to a Postgres backend and
called by the backend in the process of processing queries. The calling
format for these routines is dictated by Postgres architecture.
******************************************************************************/
#include "postgres.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "fmgr.h"
#include "libpq/pqformat.h" /* needed for send/recv functions */
PG_MODULE_MAGIC;
typedef struct node* Lnode;
typedef struct list* List;
hhhh
// Create a new list
List newList(void);
// Create a new node
Lnode new_node(int val);
// Append a node to the end of the list
void append(List l, int val);
// Print current list
void printList(List l);
//Destroy list
void destroy(List l);
//Get number of nodes in the list
int num_nodes(List l);
//print list in reverse order
void print_reverse(List l);
// Extracts unique elements of the list
List extract_unique(List l);
struct node{
int val;
Lnode next;
};
struct list{
Lnode head;
int num_nodes;
};
Lnode new_node(int val){
Lnode new = malloc(sizeof(struct node));
if(new == NULL){
printf("Out of memory:(\n");
exit(1);
}
new->val = val;
new->next = NULL;
return new;
}
List newList(void){
List new = malloc(sizeof(struct list));
if(new == NULL){
printf("Out of memory:(\n");
exit(1);
}
new->head = NULL;
new->num_nodes = 0;
return new;
}
void append(List l, int val){
assert(l != NULL);
Lnode cur = l->head;
Lnode new = new_node(val);
if(cur == NULL){
l->head = new;
}else{
while(cur->next != NULL){
cur = cur->next;
}
cur->next = new;
}
l->num_nodes++;
}
void printList(List l){
assert(l!= NULL);
Lnode cur = l-> head;
while(cur != NULL){
printf("[%d]->",cur->val);
cur = cur->next;
}
printf("X\n");
}
void destroy(List l){
assert(l!= NULL);
Lnode cur = l->head;
Lnode prev = NULL;
// free every single element of the list
while(cur!= NULL){
prev = cur;
cur = cur->next;
free(prev);
}
// free the list structure itself
free(l);
}
int num_nodes(List l){
return l->num_nodes;
}
void print_reverse(List l){
if(l != NULL){
int i = l->num_nodes;
while(i > 0){
Lnode cur = l->head;
int j = 0;
while(cur->next != NULL && j < i){
cur = cur->next;
j++;
}
printf("[%d]->",cur->val);
i--;
}
printf("X\n");
}
}
List extract_unique(List l){
if(l->head != NULL){
int array[10] = {0};
Lnode cur = l->head;
while(cur->next != NULL){
array[cur->val]++;
cur = cur->next;
}
int i = 0;
while(i < 10){
if(array[i] > 0){
printf("%d->",i);
}
i++;
}
printf("X\n");
}
return l;
}
typedef struct intSet
{
List x;
} intSet;
/*****************************************************************************
* Input/Output functions
*****************************************************************************/
PG_FUNCTION_INFO_V1(intSet_in);
Datum
intSet_in(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
List x;
char *getString;
if (sscanf(str, " (%s)", &getString) != 1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for complex: \"%s\"",
str)));
printf("The length of the getString is: %lu", strlen(getString));
}