Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this C programming Assignment: Here is the Hw8 that needs to be

ID: 3817430 • Letter: I

Question

I need help with this C programming Assignment:

Here is the Hw8 that needs to be modified:

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


struct node {
int data;
struct node *next;
struct node *prev;
};

// this link points to the first link
struct node *head = NULL;

//this link is poiting to the last link
struct node *last = NULL;

struct node *current = NULL;

int length() {
int length = 0;
struct node *temp;
temp = head;
while( temp != NULL) {
temp = temp->next;
length++;
}

return length;
}

//display left to right
void displayLeftToRight() {

struct node *temp = head;

while(temp != NULL) {
printf(" Left to right output: %d ",temp->data);
temp = temp->next;
}

}

//display right to left
void displayRightToLeft() {


struct node *temp = last;

while(temp != NULL) {

printf(" Right to Left output: %d ",temp->data);
temp = temp ->prev;
}
}

//this will insert will insert link
void insert(int data) {

struct node *dataNode = (struct node*) malloc(sizeof(struct node));
dataNode->data = data;
dataNode->next = NULL;
dataNode->prev = NULL;

if(head == NULL) {
last = dataNode;
head = dataNode;
} else {
  
last->next = dataNode;

  
dataNode->prev = last;
}

last = dataNode;
}


int main(int argc, char *argv[] ) {

if( argc >= 2 ) {
int max = atoi(argv[1]);
int i = 0;
for(i = 0; i <=max; i++)
insert(i);

displayLeftToRight();
printf(" ");
displayRightToLeft();
}
else {
printf("Command line argument . ");
}


return 0;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct node
{
   int data;
   struct node *next;
   struct node *prev;
};

void tooManyError()
{
   printf("****************************************************** ");
   printf("Too many command line arguments given, exiting program. ");
   printf("****************************************************** ");
}

void tooFewError()
{
   printf("****************************************************** ");
   printf("Too few command line arguments given, exiting program. ");
   printf("****************************************************** ");
}

void insertNode(struct node *item, struct node **head, struct node **tail)
{
   if(!*head)
   {
       (*head) = item;
       (*head)->prev = NULL;
       (*tail) = item;
       (*tail)->next = NULL;
       return;
   }
  
   struct node *curr = *head;
   struct node *temp = malloc(sizeof(*temp));

   while(curr->next != NULL)
   {
       curr = curr->next;
   }

   temp = item;
   curr->next = temp;
   temp->prev = curr;
   (*tail) = temp;
}

void DISPLAY_INORDER(struct node *head, struct node *tail)
{
   if(head == NULL)
   {
       return;
   }

   struct node *curr = head;

    while(curr != NULL)
    {
       printf("%i ", curr->data);
       curr = curr->next;
    }
}

void DISPLAY_POSTORDER(struct node *head, struct node *tail)
{
   if(head == NULL)
   {
       return;
   }

   struct node *curr = tail;

    while(curr != NULL)
    {
       printf("%i ", curr->data);
       curr = curr->prev;
    }
}

void FREE_INORDER(struct node *head, struct node *tail)
{
    if(head == NULL)
   {
       return;
   }

   printf(" ");

   struct node *curr = head;

    while(curr != NULL)
    {
       if(curr != tail)
       {
           curr = curr->next;
       }

       if(curr != NULL && curr != tail)
       {
           free(curr->prev);
       }

       if(curr == tail)
       {
           free(curr->prev);
           free(curr);
           break;
       }
    }
}

void DELETE_NODE(int numToDelete, struct node **head, struct node **tail, struct node **trashHead, struct node **trashTail)
{
   int i;
   int j;
   struct node *curr = malloc(sizeof(curr));
   struct node *temp = malloc(sizeof(temp));

   for(i = numToDelete; i > 0; i--)
   {
       int nodeToDelete = rand() % i;
printf(" Deleting node at index: %i", nodeToDelete);
       j = 0;
       curr = *head;

       while(curr != NULL)
       {
           if(j == nodeToDelete)
           {
               if(curr == (*head))
               {
                   if(curr->next != NULL)
                   {
                       (*head) = curr->next;
                       (*head)->prev = NULL;
                       curr->next = NULL;
                       insertNode(curr, &*trashHead, &*trashTail);
                       break;
                   }
                   else
                   {
                       (*head) = NULL;
                       curr->next = NULL;
                       insertNode(curr, &*trashHead, &*trashTail);
                       break;
                   }
               }
               else if(curr == (*tail))
               {
                   (*tail) = curr->prev;
                   (*tail)->next = NULL;
                   curr->prev = NULL;
                   insertNode(curr, &*trashHead, &*trashTail);
                   break;
               }
               else
               {
                   temp = curr->prev;
                   temp->next = curr->next;
                   temp = curr->next;
                   temp->prev = curr->prev;
                   curr->prev = NULL;
                   curr->next = NULL;
                   insertNode(curr, &*trashHead, &*trashTail);
                   break;
               }
           }          
           j++;
           curr = curr->next;
       }
   }
}

void DISPLAY_TRASH(struct node *head, struct node *tail)
{
    printf(" Trash Inorder: ");
    DISPLAY_INORDER(head, tail);
   printf(" Trash Postorder: ");
    DISPLAY_POSTORDER(head, tail);

    FREE_INORDER(head, tail);
}

int main(int argv, char **argc)
{
    if(argv > 2)
    {
          tooManyError();
       return 0;
   }
  
   if(argv < 2)
   {
       tooFewError();
       return 0;
   }

   int n = atoi(argc[1]);
   int i;
   struct node *head = NULL;
   struct node *tail = NULL;
    struct node *trashHead = NULL;
    struct node *trashTail = NULL;

   for(i = 0; i <= n; i++)
   {
        struct node *item = malloc(sizeof(*item));
        item->data = i;
        item->next = NULL;
        item->prev = NULL;

       insertNode(item, &head, &tail);
      
       printf("Input data: %i ", i);      
   }

   srand(time(NULL));
   int numToDelete = rand() % (atoi(argc[1]) + 2);
   printf(" Deleting %i node(s)... ", numToDelete);

   DELETE_NODE(numToDelete, &head, &tail, &trashHead, &trashTail);

   if(trashHead != NULL)
   {
       DISPLAY_TRASH(trashHead, trashTail);
   }

    printf(" Inorder: ");
   DISPLAY_INORDER(head, tail);
    printf(" Postorder: ");
    DISPLAY_POSTORDER(head, tail);
   printf(" ");

    FREE_INORDER(head, tail);
   return 0;
}