Question

program in C - Starter code below //In this assignment, we practice call by reference. //Below...

program in C - Starter code below

//In this assignment, we practice call by reference.

//Below description of call by reference is from the following link
//https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm

//The call by reference method of passing arguments to a function copies
//the address of an argument into the formal parameter. Inside the function,
//the address is used to access the actual argument used in the call.
//It means the changes made to the parameter affect the passed argument.

//We use an example of linked list for this purpose.
//In the example, an implementation of how to add a node to the head of the list
//is given. We need to implement how to remove a node from the head of the list.
//Both of the functions should keep track of the length of the list.
//If we successuflly added a node to the list, the length should be incremented by one.
//Also, if we successfully rmoeved a node from the list, the length should be decrementd
//by one.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct node_tag
{
int v; // data
struct node_tag * next; // A pointer to this type of struct
} node; // Define a type. Easier to use.


node * create_node(int v)
{
node * p = malloc(sizeof(node)); // Allocate memory
assert(p != NULL); // you can be nicer

// Set the value in the node.
p->v = v;
p->next = NULL;
return p; // return
}

//This function show us how to add a new node to the font of a linked list,
//at the same time, how to update the length of the list.
//Note the ** in front of head
//Note the * in front of length
//Think why this is call by reference for a point and an integer
void add_first(node **head, node *newnode, int *length)
{
if(*head == NULL)
{
*head = newnode;
newnode->next = NULL;
}
else
{
newnode->next = *head;
*head = newnode;
}
   (*length) ++;
}

//Now we need to implement the remove_first function
node * remove_first(node **head, int *length)
{
   //Add your code below

}

void print_list(node *head)
{   
while(head!=NULL)
{   
printf("%d ", head->v);
head = head->next;
}
printf("\n");
}

//Do not change the code in main function

int main(int argc, char *argv[])
{
   if(argc != 2)
   {
       printf("Usage: %s n\n", argv[0]);
       return -1;
   }
   int n = atoi(argv[1]);
   assert(n>=1 && n<=25);
   node *head, *p;
   int length = 0;
   //Do not forget the initilization of head
   head = NULL;

   for(int i=1; i<=n; i++)
   {
       p = create_node(i);
       //Note how we call by reference for head and length
       //Note the & in front of head and length
       add_first(&head, p, &length);
       printf("length=%d\n", length);
       print_list(head);
   }
   while(head!=NULL)
   {
       //Note how we call by reference for head and length
       //Note the & in front of head and length
       p = remove_first(&head, &length);
       printf("length=%d\n", length);
       print_list(head);
       //remember we need to free all the memory allocated from heap
       free(p);
   }
   return 0;
}

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

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct node_tag
{
int v; // data
struct node_tag * next; // A pointer to this type of struct
} node; // Define a type. Easier to use.


node * create_node(int v)
{
node * p = (node *)malloc(sizeof(node)); // Allocate memory
assert(p != NULL); // you can be nicer

// Set the value in the node.
p->v = v;
p->next = NULL;
return p; // return
}

//This function show us how to add a new node to the font of a linked list,
//at the same time, how to update the length of the list.
//Note the ** in front of head
//Note the * in front of length
//Think why this is call by reference for a point and an integer
void add_first(node **head, node *newnode, int *length)
{
if(*head == NULL)
{
*head = newnode;
}
else
{
newnode->next = *head;
*head = newnode;
}
(*length) ++;
}

//Now we need to implement the remove_first function
node * remove_first(node **head, int *length)
{
//Add your code below
if(*head != NULL)
{
// store the old value of head pointer
node *temp = *head;
// make the head point to next node in the list
*head = temp->next;

(*length)--;   
// return the previous head node
return temp;
}
}

void print_list(node *head)
{
while(head!=NULL)
{
printf("%d ", head->v);
head = head->next;
}
printf("\n");
}

//Do not change the code in main function

int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Usage: %s n\n", argv[0]);
return -1;
}
int n = atoi(argv[1]);
assert(n>=1 && n<=25);
node *head, *p;
int length = 0;
//Do not forget the initilization of head
head = NULL;

for(int i=1; i<=n; i++)
{
p = create_node(i);
//Note how we call by reference for head and length
//Note the & in front of head and length
add_first(&head, p, &length);
printf("length=%d\n", length);
print_list(head);
}
while(head!=NULL)
{
//Note how we call by reference for head and length
//Note the & in front of head and length
p = remove_first(&head, &length);
printf("length=%d\n", length);
print_list(head);
//remember we need to free all the memory allocated from heap
free(p);
}
return 0;
}

Output:

length=1                                                                                                                         

1                                                                                                                                

length=2                                                                                                                         

2 1                                                                                                                              

length=3                                                                                                                         

3 2 1                                                                                                                            

length=4                                                                                                                         

4 3 2 1                                                                                                                          

length=5                                                                                                                         

5 4 3 2 1                                                                                                                        

length=4                                                                                                                         

4 3 2 1                                                                                                                          

length=3                                                                                                                         

3 2 1                                                                                                                            

length=2                                                                                                                         

2 1                                                                                                                              

length=1                                                                                                                         

1                                                                                                                                

length=0

Add a comment
Know the answer?
Add Answer to:
program in C - Starter code below //In this assignment, we practice call by reference. //Below...
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
  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Following changes need to be made, and the code for each is provided: (1) Modify newMovieReviewNode(),...

    Following changes need to be made, and the code for each is provided: (1) Modify newMovieReviewNode(), this time the newly allocated review should get an empty linked list of scores ReviewNode *newMovieReviewNode() { /* * This function allocates a new, empty ReviewNode, and initializes the * contents of the MovieReview for this node to empty values. * The fields in the MovieReview should be set to: * movie_title="" * movie_studio="" * year = -1 * BO_total = -1 * score...

  • 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...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT...

    PLEASE FILL THE CODE ACCORDINGY AND THE REQUIRED OUTPUT IS GIVEN BELOW ALONG WITH THE INPUT Problem: The following function, buildListForward, builds a linked list in a forward manner and returns the pointer of the built list: Q1: The bulidListForward function to create a linked List structure with the keyboard input( cin >> num). Change this function to receive the values stored in the array from the main function( use int type pointer variable). eg. nodeType* buildListForward(int *arrayPrt, int Size)...

  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

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