Basic C++ Constructs





  • C++ Reptitions





  • C++ Functions

















    Generic C++ Linked Lists



    General Components

















    Manipulator Functions

















    Linked Lists vs Arrays



























    Some Generic Linked List Functions



    Using this

    Generic C++ struct Data Type




    struct std_struct
      {
          int firstmember;
          float secondmember;
          std_struct *next;
      };







    Generic C++ Linked List Insert at Front Function



    std_struct * insert_at_front (std_struct *current, std_struct *newitem)

    /* insert_at_front inserts the record pointed to by its second parameter newitem in the beginning of the
      linked list pointed to by its first parameter, parameter, current. Note that the values of the fields for
      the record pointed to by newitem have been initialized before the call to insert_at_front. */


      {
        std_struct *temp;

        temp = current;
        if (empty (temp)) current = newitem;

        else
          {
            newitem->next = temp;
            current = newitem;
          };
      return current;
      }








    Generic C++ Linked List Insert at End Function



    std_struct * insert_at_end (std_struct *head, std_struct *newitem)

    /* insert_at_end inserts the record pointed to by its second parameter, newitem, at the end of the linked list
      pointed to by its first parameter, head. Note that the values of the fields for the record pointed to by newitem
      have been initialized before the call to insert_at_end.   The function returns the pointer to the linked list of
    elements with the new element inserted. */



      {
        std_struct *current;

        current = head;
        if (empty (current)) head = newitem;

        else
          {
            while (current->next != NULL)
              current = current->next;

            current->next = newitem;
          }
        return head;
      }








    Generic C++ Linked List Insert In Order Function



    std_struct * insert_in_order (std_struct *head, std_struct *newitem)

    /* insert_in_order inserts the record pointed to by its second parameter, newitem, into the list
      pointed to by its first parameter, head. It maintains the list's lowest to highest order of the firstmember.
      insert_in_order returns the pointer to the first element of the linked list with the new element inserted in order. */


      {
        std_struct * current, dummy;

        current = head;
        if (empty (current)) current = insert_at_front (head, newitem);

        else if (newitem->firstmember <= current->firstmember)
          current = insert_at_front (head, newitem);

          else
            {
              dummy = *newitem;
              current = insert_at_end (head, &dummy);
              while ( newitem->firstmember > current->next->firstmember)
                current = current->next;

              newitem->next = current->next;
              current->next = newitem;
              current = head;
              current = delete_last (head);
            }
        return current;
      }









    Generic C++ Linked List Delete First Function



    std_struct * delete_first (std_struct *current)

    /* delete_first deletes the first element of the list pointed to by its parameter, current.
      delete_first returns the pointer to the linked list of elements with the first element removed */



      {
        std_struct *temp;

        temp = current;
        if (empty (temp)) temp = NULL;
        else if (temp->next == NULL)
          {
            delete temp;
            temp = NULL;
          }
          else
            {
              current = temp->next;

              delete temp;
              temp = current;
            }
        return temp;
      }














    Generic C++ Linked List Empty Function



    int empty (std_struct *temp)

    /* empty tests its pointer variable parameter, temp,
        for NULL and returns the integer value of the result of the test */


      {
        return (temp == NULL);
      }








    Generic C++ Linked List Delete Last Function



    std_struct * delete_last (std_struct *head)

    /* delete_last deletes the last element of the list pointed to by its parameter, head.
      delete_last returns the pointer to the linked list of elements with the last element removed */



      {
        std_struct *current;


        current = head;
        if (empty (current)) current = NULL;
          else if (current->next == NULL)
            {
              delete current;
              current = NULL;
            }
              else
                {
                while (current->next->next != NULL)
                  current = current->next;


                delete current->next;
                current->next = NULL;
                current = head;
                }
        return current;
      }






    Generic C++ Linked List Search Function



    std_struct * search (std_struct * head, int id)


    /* search traverses the linked list pointed to by its first parameter ( head)
      until it locates an element that matches the value of its second parameter (id).
      search returns the pointer to the matched element of the linked list or null if the match fails.
      search inserts a dummy element to facilitate the traversal process. */


      {
        std_struct * temp, * current;

        current = head;
        temp = new std_struct;
        temp -> next = NULL; temp -> firstmember = id;
        temp -> secondmember = -99.99;
        current = insert_at_end (current, temp);
        while (current->firstmember != id)     current = current->next;

        if ( current -> next == NULL)
          {
            current = head;
            delete_last (current);
            return NULL;
          }
          else
            {
              temp = current;
              current = head;
              delete_last (current);
              return temp;
            }
      }
















    Generic C++ Linked List Element Display Function



    int writeall (std_struct *current)

      /*     Writeall displays the members of each element of the linked list of structures of type std_struct
      pointed to by its pointer variable argument (current). It displays the values to the output stream.
      Writeall returns the number of elements displayed */

        {
          int counter = 0; // counter counts the # displayed

          while (current != NULL)
            {
              cout << "firstmember's value is: " << current->firstmember
              << " secondmember's value is: " << current->secondmember << endl;
              counter++;
              current = current->next;
            }
          cout << endl;
          return counter;
        }





















    C++ OOP Program Structure



















    C++ Declaration Modifiers















    CS 361 - C++ Critical Bindings



    Problem Solution               Programming Elements

    Time                         That are Bound


































    © 2009 G. H. Price
    All Rights Reserved