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 = NULL;
for (i = 0; i < number2Add; i++) {
current = (struct node *)malloc(sizeof(struct node));
current->data = i;
if (i == 0) {
// Handle first input (special case).
*head = current;
current->left = NULL;
} else {
// Handle remaining inputs.
current->left = previous;
previous->right = current;
}
current->right = NULL;
*tail = current;
previous = current;
}
printf("\n");
}
/****************************************/
/* DISPLAY_LIST_INORDER. */
/****************************************/
void DISPLAY_LIST_INORDER(struct node *head) {
struct node *current;
current = head;
while (current != NULL) {
printf("Left to right output:\t %d\n", current->data);
current = current->right;
}
printf("\n");
}
/****************************************/
/* DISPLAY_LIST_POSTORDER. */
/****************************************/
void DISPLAY_LIST_POSTORDER(struct node *tail) {
struct node *current;
current = tail;
while (current != NULL) {
printf("Right to left output:\t %d\n", current->data);
current = current->left;
}
printf("\n");
}
/****************************************/
/* REMOVE_FROM_LIST. */
/****************************************/
void REMOVE_FROM_LIST(int number2Delete, int number2Add,
struct node *(*head), struct node *(*tail), struct trash *(*Head)) {
int i;
int link2Delete;
struct node *current;
struct trash *Previous, *Current;
*Head == NULL;
for (i = 0; i < number2Delete; i++) {
// Pick a random node (payload) to delete.
link2Delete = (rand() % number2Add);
current = *head;
while (current != NULL) {
// Look for link with target payoad.
if (current->data == link2Delete) {
// Add link to trash list.
Current = (struct trash *)malloc(sizeof(struct trash));
Current->node = current;
if (i == 0) {
// Handle first input (special case).
*Head = Current;
} else {
// Handle remaining inputs.
Previous->next = Current;
}
Current->next = NULL;
Previous = Current;
// Adjust links to skip over current.
if (current->left == NULL) {
*head = current->right;
if (*head != NULL) current->right->left = NULL; // Must trap case of only 1 node.
break;
} else if (current->right == NULL) {
*tail = current->left;
current->left->right = NULL;
break;
} else {
current->left->right = current->right;
current->right->left = current->left;
break;
}
}
current = current->right;
}
}
}
/****************************************/
/* DISPLAY_TRASH. */
/****************************************/
void DISPLAY_TRASH(struct trash *head) {
struct trash *current;
current = head;
while (current != NULL) {
printf("Trash list:\t\t %d\n", current->node->data);
current = current->next;
}
printf("\n");
}
/****************************************/
/* FREE_LIST. */
/****************************************/
void FREE_LIST(struct node *(*head), struct node *(*tail)) {
struct node *current;
current = *head;
while (current != NULL) {
current = current->right;
if (current != NULL) free(current->left);
}
free(*tail);
*head = NULL;
*tail = NULL;
}
/****************************************/
/* FREE_TRASH. */
/****************************************/
void FREE_TRASH(struct trash *(*Head)) {
struct trash *Current, *Previous;
Current = *Head;
while (Current != NULL) {
Previous = Current;
Current = Current->next;
free(Previous);
Previous = NULL;
}
*Head = NULL;
}
/***********************************************************/
/* Main. */
/***********************************************************/
int main(int argc, char *argv[]) {
int number2Add;
int number2Delete;
struct node *head, *tail;
struct trash *Head;
// Check command line input(s).
if (argc == 2) {
printf("Command line argument:\t %s\n", argv[1]);
} else if (argc > 2) {
printf("Too many arguments supplied.\n");
exit(0);
} else {
printf("One argument expected.\n");
exit(0);
}
// Build bi-directional linked list.
number2Add = atoi(argv[1]);
BUILD_LIST(number2Add, &head, &tail);
// Display bi-directional linked list contents.
DISPLAY_LIST_INORDER(head);
DISPLAY_LIST_POSTORDER(tail);
// Randomly delete nodes.
number2Delete = rand() % number2Add + 3;
REMOVE_FROM_LIST(number2Delete, number2Add, &head, &tail, &Head);
// Display trash linked list contents.
DISPLAY_TRASH(Head);
// Display bi-directional linked list contents.
DISPLAY_LIST_INORDER(head);
DISPLAY_LIST_POSTORDER(tail);
// Free both linked lists.
FREE_LIST(&head, &tail);
FREE_TRASH(&Head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data;
struct node *right;
struct node *left;
};
struct trash {
struct node *node;
struct trash *next;
};
void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) {
int i;
struct node *previous, *current;
*head = NULL;
*tail = NULL;
for (i = 0; i < number2Add; i++) {
current = (struct node *)malloc(sizeof(struct node));
current->data = i;
if (i == 0) {
// Handle first input (special case).
*head = current;
current->left = NULL;
} else {
// Handle remaining inputs.
current->left = previous;
previous->right = current;
}
current->right = NULL;
*tail = current;
previous = current;
}
printf("\n");
}
void DISPLAY_LIST_INORDER(struct node *head) {
struct node *current;
current = head;
while (current != NULL) {
printf("Left to right output:\t %d\n", current->data);
current = current->right;
}
printf("\n");
}
void DISPLAY_LIST_POSTORDER(struct node *tail) {
struct node *current;
current = tail;
while (current != NULL) {
printf("Right to left output:\t %d\n", current->data);
current = current->left;
}
printf("\n");
}
void REMOVE_FROM_LIST(int number2Delete, int number2Add,
struct node *(*head), struct node *(*tail), struct trash *(*Head)) {
int i;
int link2Delete;
struct node *current;
struct trash *Previous, *Current;
*Head == NULL;
for (i = 0; i < number2Delete; i++) {
// Pick a random node (payload) to delete.
link2Delete = (rand() % number2Add);
current = *head;
while (current != NULL) {
// Look for link with target payoad.
if (current->data == link2Delete) {
// Add link to trash list.
Current = (struct trash *)malloc(sizeof(struct trash));
Current->node = current;
if (i == 0) {
// Handle first input (special case).
*Head = Current;
} else {
// Handle remaining inputs.
Previous->next = Current;
}
Current->next = NULL;
Previous = Current;
// Adjust links to skip over current.
if (current->left == NULL) {
*head = current->right;
if (*head != NULL) current->right->left = NULL; // Must trap case of only 1 node.
break;
} else if (current->right == NULL) {
*tail = current->left;
current->left->right = NULL;
break;
} else {
current->left->right = current->right;
current->right->left = current->left;
break;
}
}
current = current->right;
}
}
}
void DISPLAY_TRASH(struct trash *head) {
struct trash *current;
current = head;
while (current != NULL) {
printf("Trash list:\t\t %d\n", current->node->data);
current = current->next;
}
printf("\n");
}
void FREE_LIST(struct node *(*head), struct node *(*tail)) {
struct node *current;
current = *head;
while (current != NULL) {
current = current->right;
if (current != NULL) free(current->left);
}
free(*tail);
*head = NULL;
*tail = NULL;
}
void FREE_TRASH(struct trash *(*Head)) {
struct trash *Current, *Previous;
Current = *Head;
while (Current != NULL) {
Previous = Current;
Current = Current->next;
free(Previous);
Previous = NULL;
}
*Head = NULL;
}
int main(int argc, char *argv[]) {
int number2Add;
int number2Delete;
struct node *head, *tail;
struct trash *Head;
// Check command line input(s).
if (argc == 2) {
printf("Command line argument:\t %s\n", argv[1]);
} else if (argc > 2) {
printf("Too many arguments supplied.\n");
exit(0);
} else {
printf("One argument expected.\n");
exit(0);
}
// Build bi-directional linked list.
number2Add = atoi(argv[1]);
BUILD_LIST(number2Add, &head, &tail);
// Display bi-directional linked list contents.
DISPLAY_LIST_INORDER(head);
DISPLAY_LIST_POSTORDER(tail);
// Randomly delete nodes.
number2Delete = rand() % number2Add + 3;
REMOVE_FROM_LIST(number2Delete, number2Add, &head, &tail, &Head);
// Display trash linked list contents.
DISPLAY_TRASH(Head);
// Display bi-directional linked list contents.
DISPLAY_LIST_INORDER(head);
DISPLAY_LIST_POSTORDER(tail);
// Free both linked lists.
FREE_LIST(&head, &tail);
FREE_TRASH(&Head);
return 0;
need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...
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...
C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...
^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node { int info; struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) { DL *left,*right,*curr; curr = start; if( start == NULL) { printf("\nDoubly...
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");...
I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX]; struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL) { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
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...
Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...