'C - use the struct name as the parameter of function in strcut

I'm trying to implement stack in c. this is my code:

//header file
typedef struct
{
    ElementType Data[MaxSize];
    Position Top;
    
    int Push(SeqStack *L);
}SeqStack;

and this is what the compiler showed:

error: unknown type name 'SeqStack'

I'm not very familiar with c. What is the correct way to achieve such implementation?



Solution 1:[1]

The name SeqStack is yet undefined in this line

int Push(SeqStack *L);

So the compiler issues an error.

Moreover you may not declare a function within a structure in C, You can declare a pointer to a function, Then when an object of the structure type will be defined you can initialize the pointer by some function.

You should write

typedef struct SeqStack
{
    ElementType Data[MaxSize];
    Position Top;
    
    int ( *Push )( struct SeqStack *L );
}SeqStack;

or

typedef struct SeqStack SeqStack;

struct SeqStack
{
    ElementType Data[MaxSize];
    Position Top;
    
    int ( *Push )( SeqStack *L );
};

Solution 2:[2]

What you are looking for is known as non-equi anti-join. This can be done pretty easily with the data.table package. Consider

library(data.table)

mydata <- data.frame(x = 1:25, y = 26:50)
df.remove <- data.frame(min = c(3,10,22,17), max = c(6,13,24,20))
setDT(mydata)[!df.remove, on = .(x >= min, x <= max)] # drop rows where min <= x <= max

Output

     x  y
 1:  1 26
 2:  2 27
 3:  7 32
 4:  8 33
 5:  9 34
 6: 14 39
 7: 15 40
 8: 16 41
 9: 21 46
10: 25 50

Solution 3:[3]

A base R approach -

res <- subset(mydata, !x %in% unlist(Map(`:`, df.remove$min, df.remove$max)))
res

#    x  y
#1   1 26
#2   2 27
#7   7 32
#8   8 33
#9   9 34
#14 14 39
#15 15 40
#16 16 41
#21 21 46
#25 25 50

Using Map we create sequence between min and max values, unlist them in a single vector and drop the rows if x has the same value.


Another option using fuzzyjoin package -

fuzzyjoin::fuzzy_anti_join(mydata, df.remove, 
                           c('x' = 'min', 'x' = 'max'), 
                           match_fun = c(`>=`, `<=`))

Solution 4:[4]

Since you're using dplyr function between, we can use dplyr filter function. For each row of mydata you want to apply between to each row of df.remove to see if value of column x is between. This can be accomplished with mapply (since there are two values to input to the function). This will create a matrix of T/F. Then go through each row and see if any values are returned as T. Do this with apply, across rows. Negative filter for any row that returns a T indicating a value between the target value:

library(dplyr)
mydata %>% 
  filter(
    !mapply(function(left, right) between(mydata$x, left, right), left = df.remove$min, right = df.remove$max) %>% 
      apply(., 1, any)
    )

Returns:

    x  y
1   1 26
2   2 27
3   7 32
4   8 33
5   9 34
6  14 39
7  15 40
8  16 41
9  21 46
10 25 50

Solution 5:[5]

Just because this is an interesting problem which has several possible solutions, here is another approach using meta programming.

The idea is that we turn df.remove into a list of expressions which we then use inside filter(mydata, !!! .) by splicing it with the !!! operator.

One way to get the list of expressions is to use rowwise summarise and create a list of expressions with bquote which allows us to evaluate expressions wrapped in .(). In our case the min and max values.

And although this is possible, I'd probably use either @ekoam's {data.table} or @Ronak's base R approach.

library(dplyr)

df.remove %>% 
  rowwise %>% 
  summarise(x = list(bquote(!x %in% c(.(min):.(max))))) %>%
  pull(x) %>% 
  filter(mydata, !!! .)

#> `summarise()` has ungrouped output. You can override using the `.groups`
#> argument.
#>     x  y
#> 1   1 26
#> 2   2 27
#> 3   7 32
#> 4   8 33
#> 5   9 34
#> 6  14 39
#> 7  15 40
#> 8  16 41
#> 9  21 46
#> 10 25 50

Created on 2022-01-23 by the reprex package (v0.3.0)

Solution 6:[6]

Using data.table::inrange.

library(data.table)
mydata[!mydata$x %inrange% df.remove, ]
#     x  y
# 1   1 26
# 2   2 27
# 7   7 32
# 8   8 33
# 9   9 34
# 14 14 39
# 15 15 40
# 16 16 41
# 21 21 46
# 25 25 50

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ekoam
Solution 3 Ronak Shah
Solution 4 Brian Syzdek
Solution 5
Solution 6 jay.sf