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

Please help. Don\'t know where to start with this program. This assignment deals

ID: 3572933 • Letter: P

Question

Please help. Don't know where to start with this program. This assignment deals with the combination of dynamic memory allocation and structures to create a common data structure known as a doubly-linked list.

My programming assignment is in this link

http://mjgeiger.github.io/eece2160/programs/EECE.2160_prog9_DLList.pdf

The main program and the header are already written, the links:

http://mjgeiger.github.io/eece2160/programs/prog9_main.c

http://mjgeiger.github.io/eece2160/programs/DLList.h

I must complete the following code:

This is what the output should look like:

Enter command print List is enpty Enter comnand add Enter word to be added test Enter command print Contents of list: test Enter command add Enter word to be added math Enter comnand add Enter word to be added Western Enter command: print Contents of list: nath test Western Enter command delete Enter word to be deleted test Enter command find Enter word to be found math nath found in list Enter command find Enter word to be found. test test not found in list Enter command print Contents of list: math Western Enter command: delete Enter word to be deleted Western Enter command: delete Enter word to be deleted math Enter command print List is enpty Enter comnand exit

Explanation / Answer

//Tested on ubuntu,Linux

/*********************DLList.h***********************/


/*************************************************
   16.216: ECE Application Programming
   UMass Lowell
   M. Geiger
   Header file for 10th programming assignment
   Contains structure definitions, function prototypes

********DO NOT MODIFY THIS FILE ********

**************************************************/

// Structure definitions
// Node in doubly-linked list
typedef struct DLN {
   struct DLN *prev;       // Pointer to previous list element
   struct DLN *next;       // Pointer to next list element
   char *word;
} DLNode;

// Actual doubly-linked list
typedef struct {
   DLNode *firstNode;   // Pointer to first node in list
   DLNode *lastNode;   // Pointer to last node in list
} DLList;

// Function prototypes
DLNode *findNode(DLList *list, char *str);       // Find node containing str
void addNode(DLList *list, char *str);           // Add new node to list containing str
void delNode(DLList *list, char *str);           // Delete node containing str from list
void printList(DLList *list);                   // Print contents of list


/****************************prog9_main.c**************************/

/*************************************************
   16.216: ECE Application Programming
   UMass Lowell
   M. Geiger
   Main program for 10th programming assignment
   Working with dynamic memory allocation

********DO NOT MODIFY THIS FILE ********

**************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DLList.h"               // User-defined header file

char *readLine();               // Reads string from input and dynamically allocates space
//Adding new Node at front position
void addNode(DLList *list, char *str) {
   if(list->firstNode==NULL) {
       //getting memory for firstNode      
       list->firstNode=(DLNode*)malloc(sizeof(DLNode));  
          
       list->firstNode->word=str;
       list->firstNode->next=NULL;
       list->firstNode->prev=NULL;
      
       list->lastNode=list->firstNode;
   }else{
       //getting memory for firstNode      
       DLNode *temp=(DLNode*)malloc(sizeof(DLNode));      
       temp->word=str;
       DLNode *test=list->firstNode;
       while(test->next!=NULL) {
       if(strcmp(str,test->word)>0&&strcmp(str,test->word)<0){
           break;
       }else {
           test=test->next;
       }
   }      
       if(test->prev==NULL) {/*add in front*/
              temp->prev=NULL;
               temp->next=list->firstNode;
               list->firstNode->prev=temp;
               list->firstNode=temp;
           }else if(test->next==NULL) {/*add in End*/
               test->next=temp;
               temp->prev=test;
               temp->next=NULL;
           }else {/* add in middle */
               test->next->prev=temp;
               temp->next=test->next;
               temp->prev=test;
               test->next=temp;
              
           }  
      
      
      
   }
  
}
/*This function is used to find the node*/
DLNode *findNode(DLList *list, char *str) {

      DLNode *temp=list->firstNode;
   if(temp!=NULL) {
   while(temp!=NULL) {
       if(strcmp(str,temp->word)==0){
           return temp;
       }
       temp=temp->next;
   }
}else{
   printf("List is Empty ");
}
  
        return NULL;
}
// Delete node containing word from list if it is present
void delNode(DLList *list, char *str) {
     
      DLNode *temp=list->firstNode;
   if(temp!=NULL) {
   while(temp!=NULL) {
       if(strcmp(str,temp->word)==0){          
           if(temp->prev==NULL) {/*Remove from beginning*/
               list->firstNode=temp->next;
           }else if(temp->next==NULL) {
               temp->prev->next=NULL;
           }else {/* Remove from middle */
               temp->prev->next=temp->next;
               temp->next->prev=temp->prev;
              
           }
           free(temp);
           break;
       }
      
       temp=temp->next;
   }
}else{
   printf("List is Empty ");
}

          
}
/*Printing list*/
void printList(DLList *list) {
   DLNode *temp=list->firstNode;
   if(temp!=NULL) {
   while(temp!=NULL) {
       printf("%s ",temp->word);
       temp=temp->next;
   }
}else{
   printf("List is Empty ");
}
  
}
void main() {
   DLList myList = {NULL, NULL};   // Doubly-linked list used throughout program
                                   // Since list is empty, firstNode and lastNode are
                                   //    both initialized to NULL
   char *cmd = NULL;               // Input command
   char *word = NULL;               // Input word
  

   do {
       printf(" Enter command: ");
       cmd = readLine();       // Read string from standard input
  
       // Add word to list
       if (strcmp(cmd, "add") == 0) {
           printf("Enter word to be added: ");
           word = readLine();
           addNode(&myList, word);
       }

       // Delete word from list
       else if (strcmp(cmd, "delete") == 0) {
           printf("Enter word to be deleted: ");
           word = readLine();
           delNode(&myList, word);
       }

       // Find word in list
       else if (strcmp(cmd, "find") == 0) {
           printf("Enter word to be found: ");
           word = readLine();
           if (findNode(&myList, word) == NULL)
               printf("%s not found in list ", word);
           else
               printf("%s found in list ", word);
       }

       // Print contents of entire list
       else if (strcmp(cmd, "print") == 0)
           printList(&myList);

       // Invalid command
       else if (strcmp(cmd, "exit") != 0)
           printf("Invalid command %s ", cmd);

   } while (strcmp(cmd, "exit") != 0);
}

// Reads string from standard input and dynamically allocates space
// Assumes string terminates with ' '
char *readLine() {
   int i = 0;           // Loop index
   char *inStr;       // Input string
   char *tempStr;       // Temp string
   char inCh;           // Input character
   i = 0;

   inStr = (char *)malloc(1);   // Start with 1-character string
   if (inStr == NULL) {
       fprintf(stderr, "Could not allocate ");
       return NULL;
   }

   // Repeatedly read input characters and reallocate space to store string
   while ((inCh = fgetc(stdin)) != ' ') {
       inStr[i++] = inCh;
       tempStr = (char *) realloc(inStr, (i + 1) * sizeof(char));
      
       // Reallocation failed
       if (tempStr == NULL) {
           fprintf(stderr, "Could not reallocate ");
           return inStr;
       }

       // Reallocation successful; assign tempStr to inStr
       else inStr = tempStr;
   }

   inStr[i] = '';
   return inStr;
}

/*********************output*****************/

lalchand@lalchand:~/Desktop/chegg$ gcc prog9_main.c
lalchand@lalchand:~/Desktop/chegg$ ./a.out

Enter command: print
List is Empty

Enter command: add
Enter word to be added: test

Enter command: print
test
Enter command: add
Enter word to be added: math

Enter command: add
Enter word to be added: western

Enter command: print
math
test
western
Enter command: delete
Enter word to be deleted: test

Enter command: find
Enter word to be found: math
math found in list

Enter command: find
Enter word to be found: test
test not found in list

Enter command: print
math
western
Enter command: delete
Enter word to be deleted: western

Enter command: delete
Enter word to be deleted: math

Enter command: print
List is Empty

Enter command: exit

Thanks a lot