Question

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 = 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;
}
 
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#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;
Add a comment
Know the answer?
Add Answer to:
need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...
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...

  • ^^^ Q3. I am trying to implement double linked list but I was failed to write...

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

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

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

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

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

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

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

  • 1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum,...

    1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum, courseName, & grade) 2) Change the type for the 'data' member variable in the node struct to CourseInfo (see #1) and rename it to 'courseData' 3) Modify the createNode function to receive a CourseInfo struct as a parameter. It should also display the address of the new node that is created. Display the address in both hex and decimal form. 4) Modify the display...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

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