Question

(In C Programming) Your program should start by asking the user how many nodes will be...

(In C Programming) Your program should start by asking the user how many nodes will be in the network. Nodes should be implemented by a node struct and maintained in a Linked List. •Each node should maintain all packets that have been generated and are ready to be be transmitted (one at a time). Packets should be implemented by a packet struct and maintained in a Linked List. •The protocol you will implement should run for 100 rounds. The first round is round #1. •During each round, each one of the nodes decides if it will create a packet for the other based on the following rule: •Each Node will produce a packet in every round that is a multiple of 2*node_number, two packets in every round that is a multiple of 3*node_num, and three packets in each round that is a multiple of 5*node_num. The numbering of nodes starts with node 1 •The destination of each packet is: –((node number of node generating the packet + round_number) % (total_number_of_nodes))+1 - if the destination calculated is the same as the source, then increase the destination by 1. •In each round: –Check which nodes created packets for other nodes. –Each node will transmit one packet to its destination. –Each nodes must keep track of the number of packets that it created and of the number of packets it received and from which other node •At the end of all rounds, the program must print out how many packets each node produced and how many packets each node received and from whom. It must also print out how many packets each node did nor deliver and are left each node’s the packet queue (its linked list).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

struct test_struct

{

int val;

struct test_struct *next;

};

struct test_struct *head = NULL;

struct test_struct *curr = NULL;

struct test_struct* create_list(int val)

{

printf("\n creating list with headnode as [%d]\n",val);

struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));

if(NULL == ptr)

{

printf("\n Node creation failed \n");

return NULL;

}

ptr->val = val;

ptr->next = NULL;

head = curr = ptr;

return ptr;

}

struct test_struct* add_to_list(int val, bool add_to_end)

{

if(NULL == head)

{

return (create_list(val));

}

if(add_to_end)

printf("\n Adding node to end of list with value [%d]\n",val);

else

printf("\n Adding node to beginning of list with value [%d]\n",val);

struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));

if(NULL == ptr)

{

printf("\n Node creation failed \n");

return NULL;

}

ptr->val = val;

ptr->next = NULL;

if(add_to_end)

{

curr->next = ptr;

curr = ptr;

}

else

{

ptr->next = head;

head = ptr;

}

return ptr;

}

struct test_struct* search_in_list(int val, struct test_struct **prev)

{

struct test_struct *ptr = head;

struct test_struct *tmp = NULL;

bool found = false;

printf("\n Searching the list for value [%d] \n",val);

while(ptr != NULL)

{

if(ptr->val == val)

{

found = true;

break;

}

else

{

tmp = ptr;

ptr = ptr->next;

}

}

if(true == found)

{

if(prev)

*prev = tmp;

return ptr;

}

else

{

return NULL;

}

}

int delete_from_list(int val)

{

struct test_struct *prev = NULL;

struct test_struct *del = NULL;

printf("\n Deleting value [%d] from list\n",val);

del = search_in_list(val,&prev);

if(del == NULL)

{

return -1;

}

else

{

if(prev != NULL)

prev->next = del->next;

if(del == curr)

{

curr = prev;

}

else if(del == head)

{

head = del->next;

}

}

free(del);

del = NULL;

return 0;

}

void print_list(void)

{

struct test_struct *ptr = head;

printf("\n -------Printing list Start------- \n");

while(ptr != NULL)

{

printf("\n [%d] \n",ptr->val);

ptr = ptr->next;

}

printf("\n -------Printing list End------- \n");

return;

}

int main(void)

{

int i = 0, ret = 0;

struct test_struct *ptr = NULL;

print_list();

for(i = 5; i<10; i++)

add_to_list(i,true);

print_list();

for(i = 4; i>0; i--)

add_to_list(i,false);

print_list();

for(i = 1; i<10; i += 4)

{

ptr = search_in_list(i, NULL);

if(NULL == ptr)

{

printf("\n Search [val = %d] failed, no such element found\n",i);

}

else

{

printf("\n Search passed [val = %d]\n",ptr->val);

}

print_list();

ret = delete_from_list(i);

if(ret != 0)

{

printf("\n delete [val = %d] failed, no such element found\n",i);

}

else

{

printf("\n delete [val = %d] passed \n",i);

}

print_list();

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
(In C Programming) Your program should start by asking the user how many nodes will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

  • Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes....

    Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes. Then, the user will tell us whether to print the “odd-placed” nodes or the “even-placed” nodes. For example, if I had a list of 5 nodes like below, and the user specifies for the odd nodes to be printed, my program would print the values in nodes nl and n3. If the user instead indicated for the even nodes to be printed, my program...

  • 1. How many pointers are contained in the nodes of a circularly doubly-linked list with a...

    1. How many pointers are contained in the nodes of a circularly doubly-linked list with a sentinel node of five data nodes? 2.given a circularly doubly-linked list with a sentinel node where each node has two references(forw and back); one that points to the next node and another that points to the previous node. Assume the linked list below: SENTINEL - 30 - 70 - 90 - 50 - 10 Provide the output for the following statement Print( head->forw->forw->forw->forw->back->data) 3.given...

  • ****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){    ...

    ****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){       struct node *linked_list = NULL;    linked_list = add_to_list(linked_list, 5, 'a');    linked_list = add_to_list(linked_list, 10, 'b');    linked_list = add_to_list(linked_list, 4, 'c');    linked_list = add_to_list(linked_list, 10, 'd');    linked_list = add_to_list(linked_list, 5, 'e');    linked_list = add_to_list(linked_list, 7, 'f');    linked_list = add_to_list(linked_list, 5, 'g');    linked_list = add_to_list(linked_list, 3, 'h');    int search_number;    printf("Enter number you want to search for:");    scanf("%d", &search_number);    struct node *last_node = find_last(linked_list, search_number);    if (last_node != NULL)    {        printf("Node found: value = %d and...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT