Question

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 = NULL;
return p; // return
}

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

//Add newnode at the head of the list
void add_first(node **head, node *newnode)
{
if(*head == NULL)
{
*head = newnode;
newnode->next = NULL;
}
else
{
newnode->next = *head;
*head = newnode;
}
}

//Remove the first node from the list
node * remove_first(node **head)
{
   node *p;

   p = (*head);
   if((*head)!=NULL) (*head) = (*head)->next;
   return p;
}

//Remove all the nodes whose value is a multiple of k but not k itself from the list, and free
//their allocated memory
void remove_multiple(node **head, int k)
{   
print_list(*head);
printf("Remove multiples of %d\n", k);
  
val = head->next;
  
  
if (*head == NULL;)
{
return;
}
while (*head != NULL)
{
if (*head % k == 0)
{
  
}
}

}

//Remove all the nodes from the list and free the corresponding memory
void free_all(node **head)
{

}

//Return the number of items in the list
int list_length(node *head)
{

}
//Do not change the 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 <= MAX);
node *head, *p;
head = NULL;

int i;

for(i=n; i>=2; i--)
{
p = create_node(i);
add_first(&head, p);
}

for(i = 2; i<n; i++)
{
remove_multiple(&head, i);
}

printf("%d primes between 1 and %d:\n", list_length(head), n);
print_list(head);
free_all(&head);
printf("%d items left in the list after free_all().\n", list_length(head));
return 0;
}

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

#define _CRT_SECURE_NO_DEPRECATE
#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 = NULL;
   return p; // return
}

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

//Add newnode at the head of the list
void add_first(node **head, node *newnode)
{
   if (*head == NULL)
   {
       *head = newnode;
       newnode->next = NULL;
   }
   else
   {
       newnode->next = *head;
       *head = newnode;
   }
}

//Remove the first node from the list
node * remove_first(node **head)
{
   node *p;

   p = (*head);
   if ((*head) != NULL) (*head) = (*head)->next;
   return p;
}

//Remove all the nodes whose value is a multiple of k but not k itself from the list, and free
//their allocated memory
void remove_multiple(node **head, int k)
{
   int first = 0;
   node *cur = *head,*prev=NULL,*next=NULL;
   print_list(*head);
   printf("Remove multiples of %d\n", k);
   while (cur != NULL)
   {
      
       if (cur->next != NULL)
       {
           if (cur->v%k == 0 )
           {
               if (first == 0)
               {
                   ++first;
               }
               else
               {
                   //delete this node
                   if(prev!=NULL)
                   if (prev->next != NULL)
                   {
                       next = prev->next->next;
                       prev->next = cur->next;
                       free(cur);
                       //cur = NULL;
                       cur = next;
                       ++first;
                   }
               }
           }
       }
       else
       {
           //++first;
           if (first != 0 && cur->v%k == 0)
           {
               next = prev->next->next;
               prev->next = cur->next;
               free(cur);
               //cur = NULL;
               cur = next;
           }
          
       }
       prev = cur;
       if(cur!=NULL)
       cur = cur->next;
   }
  

}

//Remove all the nodes from the list and free the corresponding memory
void free_all(node **head)
{
   node *cur = *head,*tmp;
   while (cur != NULL)
   {
       tmp = cur->next;
       free(cur);
       cur = tmp;
   }
   *head = NULL;
}

//Return the number of items in the list
int list_length(node *head)
{
   node *cur = head;
   int count = 0;
   while (cur != NULL)
   {
       ++count;
       cur = cur->next;
   }
   return count;
}
//Do not change the 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 <= MAX);
   //int n = 30;
   node *head, *p;
   head = NULL;

   int i;

   for (i = n; i >= 2; i--)
   {
       p = create_node(i);
       add_first(&head, p);
   }

   for (i = 2; i<n; i++)
   {
       remove_multiple(&head, i);
   }

   printf("%d primes between 1 and %d:\n", list_length(head), n);
   print_list(head);
   free_all(&head);
   printf("%d items left in the list after free_all().\n", list_length(head));
   return 0;
}

==========================

//output

2 3 4 5 6 7 8 9 10 11 12 13 14 15                                                                                                

Remove multiples of 2                                                                                                            

2 3 5 7 9 11 13 15                                                                                                               

Remove multiples of 3                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 4                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 5                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 6                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 7                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 8                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 9                                                                                                            

2 3 5 7 11 13                                                                                                                    

Remove multiples of 10                                                                                                           

2 3 5 7 11 13                                                                                                                    

Remove multiples of 11                                                                                                           

2 3 5 7 11 13                                                                                                                    

Remove multiples of 12                                                                                                           

2 3 5 7 11 13                                                                                                                    

Remove multiples of 13                                                                                                           

2 3 5 7 11 13                                                                                                                    

Remove multiples of 14                                                                                                           

6 primes between 1 and 15:                                                                                                       

2 3 5 7 11 13                                                                                                                    

0 items left in the list after free_all().

Add a comment
Know the answer?
Add Answer to:
Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...
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
  • 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...

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

  • Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the l...

    Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the list is empty. * * Parameter other points to the first node in a linked list, or is * NULL if the list is empty. * * Extend the linked list pointed to by head so that it contains * copies of the values stored in the linked list pointed to by other. *...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • 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");...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove...

    C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. (i.e. n is greater than 0) Follow up: Could you do this in one pass? Hint: Maintain two pointers and update one with...

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