Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. // Inserting and deleting nodes in a list
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. // self-referential structure
  6. struct listNode {
  7.  char data;  // each listNode contains a character
  8.  
  9.  struct listNode* nextPtr;      // pointer to next node
  10. };                                 // end structure listNode
  11. typedef struct listNode ListNode;  // synonym for struct listNode
  12. typedef ListNode* ListNodePtr;     // synonym for ListNode*
  13.  
  14. // prototypes
  15. void insert(ListNodePtr* sPtr, char value);
  16. char deleted(ListNodePtr* sPtr, char value);
  17. int isEmpty(ListNodePtr sPtr);
  18. void printList(ListNodePtr currentPtr);
  19.  
  20. void instructions(void);
  21.  
  22. int main(void) {
  23.  ListNodePtr startPtr = NULL;  // initially there are no nodes
  24.  unsigned int choice;          // user's choice
  25.  
  26.  char item;       // char entered by user
  27.  instructions();  // display the menu
  28.  printf("%s", "? ");
  29.  scanf("%u", &choice);
  30.  while (choice != 3) {
  31.   switch (choice) {
  32.    case 1:
  33.     printf("%s", "Enter a character: ");
  34.     scanf("\n%c", &item);
  35.     insert(&startPtr, item);  // insert item in list
  36.     printList(startPtr);
  37.     break;
  38.  
  39.    case 2:  // delete an element
  40.     // if list is not empty
  41.  
  42.     if (!isEmpty(startPtr)) {
  43.      printf("%s", "Enter character to be deleted: ");
  44.      scanf("\n%c", &item);
  45.      // if character is found, remove it
  46.  
  47.      if (deleted(&startPtr, item)) {  // remove item
  48.       printf("%c deleted.\n", item);
  49.       printList(startPtr);
  50.      }  // end if
  51.  
  52.      else {
  53.       printf("%c not found.\n\n", item);
  54.      }  // end else
  55.     }      // end if
  56.  
  57.     else {
  58.      puts("List is empty.\n");
  59.     }  // end else
  60.     break;
  61.    default:
  62.     puts("Invalid choice.\n");
  63.     instructions();
  64.     break;
  65.   }  // end switch
  66.   printf("%s", "? ");
  67.   scanf("%u", &choice);
  68.  }  // end while
  69.  puts("End of run.");
  70. }  // end main
  71.  
  72. // display program instructions to user
  73. void instructions(void) {
  74.  puts(
  75.      "Enter your choice:\n"
  76.      " 1 to insert an element into the list.\n"
  77.      " 2 to delete an element from the list.\n"
  78.      " 3 to end.");
  79. }  // end function instructions
  80. // insert a new value into the list in sorted order
  81. void insert(ListNodePtr* sPtr, char value) {
  82.  ListNodePtr newPtr = NULL;       // pointer to new node
  83.  ListNodePtr previousPtr = NULL;  // pointer to previous node in list
  84.  ListNodePtr currentPtr = NULL;   // pointer to current node in list
  85.  
  86.  newPtr = (ListNode*)malloc(sizeof(ListNode));  // create node
  87.  if (newPtr != NULL) {                          // is space available
  88.   newPtr->data = value;                      // place value in node
  89.   newPtr->nextPtr = NULL;  // node does not link to another node
  90.   previousPtr = NULL;
  91.   currentPtr = *sPtr;
  92.   // loop to find the correct location in the list
  93.   while (currentPtr != NULL && value > currentPtr->data) {
  94.    previousPtr = currentPtr;          // walk to ...
  95.    currentPtr = currentPtr->nextPtr;  // ... next node
  96.   }                                      // end while
  97.  
  98.   // insert new node at beginning of list
  99.   if (previousPtr == NULL) {
  100.    newPtr->nextPtr = *sPtr;
  101.    *sPtr = newPtr;
  102.   }       // end if
  103.   else {  // insert new node between previousPtr and currentPtr
  104.  
  105.    previousPtr->nextPtr = newPtr;
  106.    newPtr->nextPtr = currentPtr;
  107.   }  // end else
  108.  }      // end if
  109.  else {
  110.   printf("%c not inserted. No memory available.\n", value);
  111.  }  // end else
  112. }
  113. // delete a list element
  114. char deleted(ListNodePtr* sPtr, char value) {
  115.  ListNodePtr previousPtr;  // pointer to previous node in list
  116.  ListNodePtr currentPtr;   // pointer to current node in list
  117.  ListNodePtr tempPtr;      // temporary node pointer
  118.  // delete first node
  119.  if (value == (*sPtr)->data) {
  120.   tempPtr = *sPtr;           // hold onto node being removed
  121.   *sPtr = (*sPtr)->nextPtr;  // de-thread the node
  122.   free(tempPtr);             // free the de-threaded node
  123.   return value;
  124.  }  // end if
  125.  else {
  126.   previousPtr = *sPtr;
  127.   currentPtr = (*sPtr)->nextPtr;
  128.  
  129.   // loop to find the correct location in the list
  130.   while (currentPtr != NULL && currentPtr->data != value) {
  131.    previousPtr = currentPtr;          // walk to ...
  132.    currentPtr = currentPtr->nextPtr;  // ... next node
  133.   }                                      // end while
  134.   // delete node at currentPtr
  135.   if (currentPtr != NULL) {
  136.    tempPtr = currentPtr;
  137.    previousPtr->nextPtr = currentPtr->nextPtr;
  138.    free(tempPtr);
  139.    return value;
  140.   }  // end if
  141.  }      // end else
  142.  return '\0';
  143. }  // end function delete
  144.  
  145. // return 1 if the list is empty, 0 otherwise
  146. int isEmpty(ListNodePtr sPtr) {
  147.  return sPtr == NULL;
  148. }  // end function isEmpty
  149. // print the list
  150. void printList(ListNodePtr currentPtr) {
  151.  // if list is empty
  152.  if (isEmpty(currentPtr)) {
  153.   puts("List is empty. \n");
  154.  }  // end if
  155.  
  156.  else {
  157.   puts("The list is:");
  158.   // while not the end of the list
  159.   while (currentPtr != NULL) {
  160.    printf("%c --> ", currentPtr->data);
  161.    currentPtr = currentPtr->nextPtr;
  162.   }  // end while
  163.   puts("NULL\n");
  164.  }  // end else
  165. }  // end function printList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement