c – Linked list: addition / deletion of nodes according to the value

I am relatively new to programming since I have just started university. My mission was to develop a program (using a function and a linked list with pointers with whole elements) that does the following:

  • The function visits each element from the start. If the current item plus one is <= to the following element (e.g. if the first one is i and the second one is j, j>= i + 1), you must complete the list by adding all the values ​​between i and j-1.
  • If the current element is followed by an <= element, it must be deleted by the list and added to an array declared in the formal parameters. You must also put in the formal parameters a counter for the deleted elements.

I had already asked a question about this function because I couldn't make it work, but using recursive functions I finally did it. I would now like to know how I could improve it! Sorry if I made grammar mistakes, English is not my native language.

I have left all the comments I have made on the operation of each function.

#include 
#include 
#include 

// list declaration

struct list {
    int value;
    struct list * next_ptr; };

// pre_insert function.

void pre_insert(struct list ** ptrptr, int value) {

    struct list * tmp_ptr;

    tmp_ptr = *ptrptr;
    *ptrptr = malloc(sizeof(struct list));
    (*ptrptr)->value = value;
    (*ptrptr)->next_ptr = tmp_ptr;
}

// if the following item is bigger than the current one, a call to this function inserts all the items missing between the two values.
// the cost of this function is T(N) = c * (N-1) (because of the while loop).

void succ_value_bigger(struct list * ptr) {

    while (ptr->value + 1 < ptr->next_ptr->value)
        pre_insert(&ptr->next_ptr, ptr->next_ptr->value-1);
}

// if succ_value_smaller is called by complete_list_to_array, this function deletes the item following the one pointed by ptr.
// the function has linear cost as there are no iterations.

int delete_element (struct list * succ_ptr, int * V, int k) {

    V(k) = succ_ptr->value;
    free(succ_ptr);

    return k;
}

// if i value is bigger than the following one, complete_list_to_array calls this function.
// the function deletes the following element using a call to delete_element function, then increases both count and k.
// the first one counts the deleted elements, the second one is an index for the array V collecting all the deleted elements.
// the function has a recursive behaviour, calling main function complete_list_to_array for the following element to be confronted with i.
// the function has cost T(N) = c1*N + c2 (since the else guard only works for the last item).

int succ_value_smaller(struct list * ptr, int * V, int * count, int k) {

    struct list *succ_ptr = ptr->next_ptr;

    // the guard sees if the current item isn't the last one. if so, it sends the flow to else, below.
    // if it is not the case, it deletes the following element and increases the indexes.

    if (succ_ptr->next_ptr != NULL)
    {
        ptr->next_ptr = succ_ptr->next_ptr;
        delete_element(succ_ptr, V, k);
        k++;
        (*count)++;

        // after doing so, it calls recursively to complete_list_to_array,
        // so that the current item is confronted with its new following element.
        // the returned value is *count.

        (*count) = complete_list_to_array(ptr, V, count, k);
    }

    else {

        // if the flow is sent here, it means the list has arrived at its last item, after the current one.
        // the succ_ptr->next_ptr pointer will so point to NULL, indicating the list has reached its end.
        // as above, the following item is deleted and count increased. The k index, though, has no need to
        // as it's the last time an element will be inserted in the array V.

        ptr->next_ptr = NULL;
        delete_element(succ_ptr, V, k);
        (*count)++;

    }
    return (*count);
}

// complete_list_to_array is the main recursive function of the program. Getting its data directly from main(),
// it uses while both as a loop and as a guard, checking each time if the list has reached its end or its last element.
// the function has cost T(N) = c1 if N == 2 || c1*N + T(N-1) if N > 2

int complete_list_to_array(struct list * ptr, int * V, int * count, int k) {

    struct list * succ_ptr;

    while (ptr != NULL)
    {
        if(ptr->next_ptr != NULL)
        {
            succ_ptr = ptr->next_ptr;

            // if the following element is bigger than the current element plus one, it sends
            // the flow to function succ_value_bigger and slides ptr to its following pointer.

            if(ptr->value + 1 <= succ_ptr->value) {
                succ_value_bigger(ptr);
                ptr = ptr->next_ptr;
            }

            // if the following element is smaller than the current, it increases the index *count as return value
            //and sends the flow to succ_value_smaller, then slides ptr to the following pointer.

            else if (succ_ptr->value <= ptr->value)
            {
                (*count) = succ_value_smaller(ptr, V, count, k);
                if (ptr->next_ptr != NULL)
                    ptr = ptr->next_ptr;

                else
                    ptr->next_ptr = NULL;
            }
        }

        // if the list only has one item, the flow is sent here. The break instruction sends the flow to the following
        // instruction, and is widely used to change the flow in switch iterations.

        else break;
    }
    return (*count);
}


// the following function creates the list which will then be used in the function. It uses two pointers, one to the
// first item, which is the return value (so that the main() function can begin its work from the first item), and
// a general pointer which is slid each time a value is inserted, according to the number N of items in the list.


struct list * create_list(int N) {
    struct list * first_ptr, * ptr;
    int i;

    // if the list contains 0 elements (N == 0) the first pointer will point to NULL, contained in the memory.h library.

    if(N == 0) {
        first_ptr = NULL;
    }
    else {
        first_ptr = malloc(sizeof(struct list));
        printf("Insert the first value:n");
        scanf("%d", &first_ptr->value);
        ptr = first_ptr;

        for(i = 2; i <= N; i++) {
            ptr->next_ptr = malloc(sizeof(struct list));
            ptr = ptr->next_ptr;
            printf("Insert element number %d:n", i);
            scanf("%d", &ptr->value);
        }

        ptr->next_ptr = NULL;
    }

    return(first_ptr);
}

// this simple function prints all the items contained in the list. It uses a while loop until the list
// has reached its last element.

void visit(struct list * ptr) {
    int i = 1;
    while(ptr != NULL) {
        printf("Item number %d in the list has value %d.n", i, ptr->value);
        ptr = ptr->next_ptr;
        i++;
    }
}

// main function

int main(void) {

    // variables declaration and dynamic allocation of the array.

    struct list * first_ptr;
    int N, k = 0;
    int i;
    int * V, count = 0;

    V = (int *)malloc(sizeof(int)*count);

    // choice of the dimension of the list.

    printf("Insert the number of elements in the list.n");
    scanf("%d", &N);

    // call to create_list function.

    first_ptr = create_list(N);

    printf("n----------------------------nn");

    // call to visit function in order to print the items initally contained in the list.

    printf("At the beginning, the list contained the following items:nn");
    visit(first_ptr);

    printf("n----------------------------nn");

    // call to complete_list_to_array function, using first_ptr so that it begins from the first item
    // and count's address, as the list requires in its formal parameters a pointer to int.

    count = complete_list_to_array(first_ptr, V, &count, k);

    // results after the flow returns from the function to main(). It first tells the user which and how many items
    // have been removed (if count == 0 it skips the first step), then prints the modified list. 

    printf("After the function call, the following items have been removed from the list and put into the array:nn");

    if(count == 0) {
        printf("No item has been removed from the list.n");
    }
    else {
    for(i = 0; i < count; i++) {
        printf("The value of V(%d) is: %d.n", i, V(i)); }

        printf("nThe total number of removed elements is %d.n", count);
    }

    // using function free(void *), the memory section occupied by array V, which was first allocated with
    // library 's function malloc. This way, the allocated section for V is freed from the memory.

    free(V);

    printf("n----------------------------nn");

    printf("After the function call, the list contains the following items:nn");
    visit(first_ptr);

    return 0;
}

dnd 5e – What are all creatures linked to the far kingdom that have statistics blocks in 5e?

I'm looking for all creatures – especially those that have statistics blocks in 5th (so I exclude things from previous editions that don't have statistics blocks in 5th, largely just to restrict the scope of this question) – that somehow relate to the distant realm.

(What I'm actually looking for are all Lovecraftian creatures, but "what do you think has Lovecraftian influences?" Is based on opinion, while the concrete links in the history of the distant kingdom are objective, so I ask that instead In addition, in the adventure I plan, the creatures will have a link to the far kingdom anyway, so the far kingdom link is still relevant to what I'm looking for.)

The ones I already know (with what evidence I could find from 5th sources of knowledge) are:

Are there other creatures that I missed? Note that this last quote shows that not all "Elder Evils" are related to the distant realm, so a link to an Elder Evil is not sufficient in itself (although it may still deserve an honorable mention ); I'm looking for links to the distant realm in particular, even if the link is really weak or indirect, like my Aboleth quote above.


If the parameter is important, suppose the realms forgotten, although if the Greyhawk parameter or anything has something relevant, I will accept it too (but it may be that the far realm is specifically only 39; one thing under forgotten realms anyway, in which case this paragraph is redundant).

The link to the far realm need not necessarily come from 5th (for example, if it has been established that, for some reason, the gnolls were somehow related to the far realm in, say, 3rd, then they are valid for this question because there are blocks of statistics for gnolls in 5th, even if there is no link between gnolls and the far realm in 5th; however, if 3rd says that the Bladelings were linked to the distant kingdom, this would not be a valid answer, because AFAIK, there are no official statistics for Bladelings in 5th).

By the way, the links to the far realm that I found are pretty weak, but that's all 5th has to offer, it seems (at least from DMG, MM, VGtM and MToF), so if someone wants to include stronger links in the distant realm of traditions from previous editions for creatures that I have already included, it would be appreciated, but not if it is all that the answer contains, because that would not really answer the question asked, which is "what other are the creatures related to the distant kingdom? "(unless your answer also says" No, you found them all ", in which case it would still be a valid answer).

c – Add or remove items from a linked list

I am relatively new to programming since I have just started college. My mission is to develop a program (using a function and a linked list with pointers with whole elements) that does the following:

  • the function visits each element from the start. if the current item plus one is <= to the following element (eg. if the first one is i and the second one is j, j>= i + 1), you must complete the list by adding all the values ​​between i and j-1.

  • if the current element is followed by an <= element, it must be deleted by the list and added to an array declared in the formal parameters. you must also put in the formal parameters a counter for the deleted elements.

i have absolutely no idea what i am doing wrong. sorry if i made grammar mistakes, english is not my native language.

#include 
#include 

struct list {
    int value;
    struct list * next_ptr; };

void pre_insert(struct list ** ptrptr, int value) {

    struct list * tmp_ptr;

    tmp_ptr = *ptrptr;
    *ptrptr = (struct list*)malloc(sizeof(struct list));
    (*ptrptr)->value = value;
    (*ptrptr)->next_ptr = tmp_ptr;
}

int complete_list_array(struct list * ptr, int * V, int * count) {

    struct list * succ_ptr;
    int i, k = 0;

    while (ptr != NULL) {  
        if (ptr->next_ptr != NULL) { 
            succ_ptr = ptr->next_ptr; 
            if (ptr->value + 1 <= succ_ptr->value) { 
                for(i = 1; ptr->value + i <= succ_ptr->value; i++) { //
                    succ_ptr = succ_ptr->next_ptr;
                    ptr = ptr->next_ptr;
                    pre_insert(&ptr, ptr->value+ i); } 
                 ptr = ptr->next_ptr; }
            else if (ptr->value >= succ_ptr->value) { 
            ptr->next_ptr = succ_ptr->next_ptr; 
            V(k) = succ_ptr->value;
            free(succ_ptr);
            k++;
            (*count)++;
            ptr = ptr->next_ptr; }  } }

    return (*count);}

struct list * create_list(int N) {
    struct list * first_ptr, * ptr;
    int i;

    if(N == 0) {
        first_ptr = NULL;
    }
    else {

        first_ptr = (struct list *)malloc(sizeof(struct list)); 
        printf("Insert the first value:n");
        scanf("%d", &first_ptr->value);
        ptr = first_ptr; 

        for(i = 2; i <= N; i++) {
            ptr->next_ptr = (struct list *)malloc(sizeof(struct list));
            ptr = ptr->next_ptr;
            printf("Insert element number %d:n", i);
            scanf("%d", &ptr->value); }

        ptr->next_ptr = NULL; }

        return(first_ptr);
    }

void visit(struct list * ptr) {
    int i = 1;
    while(ptr != NULL) {
        printf("The element number %d in the list has value %d.n", i, ptr->value);
        ptr = ptr->next_ptr;
    i++;} }


int main() {

    struct list * first_ptr;
    int N;

    printf("Insert the number N of elements in the list.n");
    scanf("%d", &N);

    first_ptr = create_list(N);

    printf("Elements of the list.n");
    visit(first_ptr);

    int * V, count = 0;
    V = (int *)malloc(sizeof(int)* N);

    count = complete_list_array(first_ptr, V, &count);

    printf("count = %d", count);


}

Python linked list execution issues

I have tried to implement an individually linked list in Python. When running the program, I am faced with issues like infinite loops and function calls that don't work.

I am quite confident in the logic of the program.

Can anyone let me know how can i run the program below with display, delete, and get_length functions working as expected.

I would also appreciate any other feedback.

class Node(object):
def __init__(self, value):
    """
    Initialize a new node, which has a value associated with it and a
    variable that points to the next element in the linked list
    """
    self.value = value
    self.next  = None

class LinkedList(object):
    def __init__(self, head=None):
        """
        Initialize a linked list that has `head element`, which is the first
        element in the list.
        A new linked list without a head will default to None
        """
        self.head = head

    def append(self, new_element):
        """
        Add a new element to the end of the linked list
        """
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = new_element
        else:                                # if there are no elements present
            self.head = new_element

    def prepend(self, new_element):
        """
        Add a new element to the begining of the linked list
        """
        new_node = Node(new_element)
        new_node.next = self.head
        self.head = new_node

    def get_element(self, position):
        """
        Return an element from a particular position
        """
        counter = 1
        current = self.head
        if position < 1:
            return None
        while current and counter <= position:
            if counter == position:
                return current.value
            current = current.next
            counter += 1
        return None

    def insert(self, new_element, position):
        """
        Insert an element at a given position
        """
        counter = 1
        current = self.head
        if position == 1:
            ll = LinkedList()
            ll.prepend(new_element)
        else:
            while current and counter < position:
                if counter == (position - 1):
                    new_element.next = current.next    # change pointers
                    current.next = new_element         # assign node/memory location
                current = current.next
                counter += 1

    def delete(self, value):
        """
        Delete the pointer to the first occurrence of a particular value
        """
        previous = None
        current = self.head
        while current.value != value and current.next:
            previous = current
            current = current.next
        if current.value == value:
            if previous:
                previous.next = current.next
            else:
                self.head = current.next

    def empty(self):
        """
        Clear ALL contents of the linked list
        """
        self.__init__()

    def search(self, value):
        current = self.head
        try:    # to prevent exception (AttributeError: 'NoneType' object has no attribute 'value')
            while current.value != value:
                current = current.next
            return True
        except:
            return False

    def get_length(self):
        """ DIY """
        counter = 0
        current = self.head
        while current:
            counter += 1
            current = current.next
        return counter

    def display(self):
        if self.head is None:
            print("Linked list is empty !!")
        else:
            current = self.head
            while current:
                print(current.value , " ")
                current = current.next

# Initialize elements
element1 = Node(1)
element2 = Node(2)
element3 = Node(3)
element4 = Node(4)

# Test display
linked_list = LinkedList()
linked_list.display()                 # display works here

# Test append
linked_list.append(element2)
linked_list.append(element3)
linked_list.append(element4)
linked_list.display()                 # display works here

# Test prepend
linked_list.prepend(5)
linked_list.prepend(6)
linked_list.prepend(7)
linked_list.display()

# Test search
print(linked_list.search(2))
print(linked_list.search(3))
print(linked_list.search(4))
print(linked_list.search(9))

# Test get_element
print(linked_list.get_element(1))
print(linked_list.get_element(2))
print(linked_list.get_element(3))
print(linked_list.get_element(4))

# Test insert
linked_list.insert(element4, 4)
linked_list.insert(element4, 3)
linked_list.insert(element4, 2)
linked_list.insert(element4, 1)
# linked_list.display()               # infinite loop
print(linked_list.get_element(1))
print(linked_list.get_element(2))
print(linked_list.get_element(3))
print(linked_list.get_element(4))

# Test get_length
print("Number of lements in linked list:")
# print(linked_list.get_length())     # function doesn't work

# Test delete
linked_list.delete(4)                 # function doesn't work
print(linked_list.get_element(1))
print(linked_list.get_element(2))
print(linked_list.get_element(3))
print(linked_list.get_element(4))

Is the database linked list a good architecture for the responses and the notification system when using mongodb?

I want to create a system of user reviews and responses to reviews from a website. There may be answers to answers. I am using the mongodb database which, I think, is an important detail.

The review document looks roughly like this:

{
  review: {
    reviewid: String,
    userid: String,
    reviewRating: Int,
    reviewContent: String,
    replies: (Reply)
  }

}

The response object looks like this:

{
  reply: {
    userid: String,
    reviewid: String,
    replyid: String,
    replyContent: String
  }
}

I don't know which route to take with how to store user responses. On the one hand, it will be very convenient to store an array of response objects in the review document (1). On the other hand, I think it will be more stable and organized to save the responses as separate documents in the database (2).

In case 1, I get the flexibility of mongodb and I won't need to do db searches.

In case 2, I thought about adding previous: ObjectId and next: ObjectId fields for each response. Then the exam will only be linked to the first response in the chain. In case 2, there will be more searches for answers because I will have to repeat next All the time. However, it will not take long to get the following response, as mongodb creates a default index on the id fields. Another downside is that to count the number of responses per exam, I will have to perform as many database searches to db as there are responses, unless I keep the number as a review and standby field. to increment / decrement it each time. (more complexity).

So, at first glance, the first route is better in terms of performance. The main drawback is that the order of the responses is a bit volatile in the sense that it is simply a table. I also instinctively feel that the first route is "fragile" because the answers are not a real document in the database without their own identifier.

I'm looking for suggestions on what would be best practice here.

complex analysis – Concerning a doubt in the calculation of the poles of the Laurent series linked to the cot (πz)

When studying the analytic number theory in Tom M Apostol's book, I couldn't think of how to calculate the poles of this function.

The function is – $ F_n (z) $ =
enter description of image here

enter description of image here

The main problem I face is that I don't know how to find the expansion of the Laurent series from cot (πz).

Can anyone tell how to find their poles?

Storage of linked server in stored procedure variable

I have the following configuration:

Server 1: CGENMONOSAMP01

Server 2: CGENMONOSAMP02

I have server 2 linked to server 1 and the link works, I can make requests like this on server 1:

SELECT * FROM (CGENMONOSAMP02).master.dbo.sysdatabases

And it retrieves the list of databases on server 2.

I am trying to create the following:

    DECLARE @SourceDatabase varchar(50) = '(CGENMONOSAMP02).Sample_DB'
    DECLARE @SourceHost varchar(50) = (SELECT SUBSTRING(@SourceDatabase, 2, 14))
    DECLARE @SourceDB varchar(50) = (SELECT SUBSTRING(@SourceDatabase, 18, 100))

--@SourceHost is CGENMONOSAMP02

--@SourceDB is Sample_DB

    IF NOT EXISTS (SELECT (name) FROM master.dbo.sysdatabases WHERE (name) = @SourceDatabase)
    BEGIN
        IF NOT EXISTS (SELECT (name) FROM @SourceHost.master.dbo.sysdatabases WHERE (name) = @SourceDB) 
        BEGIN
        RAISERROR ('Error: Source Database(%s) is not existing.', 10, 1,@SourceDatabase)
        RETURN
        END
    END

This is called on server 1, it is supposed to take the name of the database which can be either the name of the database located

on server 1 like: "Sample_Table"

or it can be the name of the database located

on server 2 like: "(CGENMONOSAMP02) .Sample_Table"

It is supposed to first check if the past name of the database exists on the local server, if not – it should check if it exists on the linked server.

I have tried the above and this gives me:

Incorrect syntax near & # 39;. & # 39 ;.

I also tried:

IF NOT EXISTS (SELECT (name) FROM (@SourceHost).master.dbo.sysdatabases WHERE (name) = @SourceDB) 

But that gives me:

Could not find server & # 39; @ SourceHost & # 39; in sys.servers. Check that the
the correct server name has been specified.

Is it possible to make such a reference?

c – Implement the function in a doubly linked list

The following is the entire function for implementing a doubly linked list. Can someone show me if there is a better way to write these functions, delete_at function please?

#include
#include
typedef struct linked_list
{
    int point;
    struct linked_list *p_node;
    struct linked_list *n_node;
}node;

node *create_head_node(node *, int);
node *insert_head(node *, int);
void insert_tail(node *, int);
node *insert_at(node *, int, int);
int count_node(node *);
void print_list(node *);
node *delete_tail(node *);
node *delete_head(node *);
node *delete_at(node *, int);

node *create_head_node(node *head, int point){
    head->point=point;
    head->p_node=NULL;
    head->n_node=NULL;
    return head;
}
node *insert_head(node *head, int point){
    if (head==NULL){
        printf("no head exist");
        return 0;
    }
    node *temp=(node*)malloc(sizeof(node));
    temp->point=point;
    temp->p_node=NULL;
    temp->n_node=head;
    head->p_node=temp;
    head=temp;
    return head;
}
void insert_tail(node *head, int point){
    if (head==NULL){
        printf("no head exist");
        return;
    }
    node *p=head;
    node *temp=(node*)malloc(sizeof(node));
    temp->point=point;
    temp->n_node=NULL;
    while (p->n_node!=NULL)
    {
        p=p->n_node;
    }
    p->n_node=temp;
    temp->p_node=p;
}
node *insert_at(node *head, int point, int pos){
    if (head==NULL){
        printf("no head exist");
        return 0;
    }
    int count=count_node(head);
    while (pos>count)
    {
        printf("choose %d positions to add. choose again: ", count); scanf("%d", &pos);
    }
        node *p=head;
        for (int i = 0; i < pos; i++)
        {
            p=p->n_node;
        }
        node *temp=(node*)malloc(sizeof(node));
    temp->point=point;
    temp->n_node=p;
    temp->p_node=p->p_node;
    if(p->p_node!=NULL) p->p_node->n_node=temp;
    p->p_node=temp;
    return head;
}
int count_node(node *head){
    node *p=head;
    int count=0;
    while (p!=NULL)
    {
        count++;
        p=p->n_node;
    }
    free(p);
    return count;
}
void print_list(node *head){
    if (head==NULL){ printf("nothing to print"); return;}
    node *p=head;
    while (p!=NULL)
    {
        printf("%d ", p->point);
        p=p->n_node;
    }
}
node *delete_head(node *head){
    if (head==NULL){
        printf("no head existn");
        return 0;
    }
    node *p=head;
    // head->p_node=NULL;
    if(p->n_node!=NULL){
        head=p->n_node;
        free(p);
    }
    else head=NULL;
    return head;
}
node *delete_tail(node *head){
    if (head==NULL){
        printf("no head existn");
        return 0;
    }
    node *p=head;
    while (p->n_node!=NULL)
    {
        p=p->n_node;
    }
    if (p->p_node!=NULL){
        p->p_node->n_node=NULL;
        free(p);
    }
    else head=NULL;
    return head;
}
node *delete_at(node *head, int pos){
    int count=count_node(head);
    if(head==NULL){
        printf("no head exist");
        return 0;
    }
    while (countn_node;
        }
        p->p_node->n_node=p->n_node;
        p->n_node->p_node=p->p_node;
        free(p);
    }
    return head;
}

linux – Sending redirected socket to an interface different from that to which it is linked

There are 2 NICs (network interface cards) with IP IP0 and IP1 as primary addresses given to them, as seen in the kernel routing table via ip route. Suppose a TCP socket is bound to IP0 (and a port P0). It will therefore receive packets intended for IP0:P0 and this part I understand. When he wants to send data to destination IP-Dest:Port-Dest, the kernel routing table indicates that this should ultimately go through the other NIC with the primary IP as IP1. I want to know what the rule is in such cases. Does the kernel drop the packet because the sender (the socket) was linked to a different network card with a different IP address (IP0) or send it via the other network card, but ignore the IP associated with it (IP1) and instead buffer the source IP as IP0 to which the socket was bound or (in the other way) ignore IP0 and stamp the source as IP1 while sending the other NIC or are there other rules that govern the behavior here and the results may vary?

A follow-up question: when connecting to all interfaces via 0.0.0.0 and port 0, is the same port number (regardless of the choice of kernel) chosen for all interfaces for this socket or there is no such guarantee and it could choose IP0:P0 and IP1:P1 or P0 != P1 or does it depend on the protocol or some other factor?