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;
}
#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().
Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...
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...
Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> 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...
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> 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 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. *...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
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 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 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...
can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <string.h> #include <dirent.h> #include <sys/wait.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> char *pathLog; char *pathRep; struct stat attr; int fileNum, curNum; char *create_logPath(char *directory); void *funcChecker(void *pathSub); void *funcprintTimeAndChanges(void *pathSub); void main(int argc, char *argv[]) { if(argc < 3) { printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]); printf("Typed count is %d\n", argc); printf("Aborted...