Question

  • Creator
    Topic
  • #2150415

    Structured Linked List

    Locked

    by samathacole ·

    I had to do this assignment right? Okay, all I supposedly had to do was copy it right out of the book and then change like two things. So then why doesn’t it work when I copy it verbatim from the book? Here is the code.

    /*
    This is the structured linked list program that was asked for week 5 that are due in 2 weeks.
    It operates and maintains a list. */
    #include
    #include

    /* self-referential structure */
    struct listNode {
    char data; /*each ListNode contains a character */
    struct listNode *nextPtr; /* pointer to nest node */
    }; /* end structure listNode */

    //***struct Node{ int data;/ this can be any type data Node *next; Node *prev; }***//

    typedef struct listNode ListNode; /* synonmy for struct listNode */
    typedef ListNode *ListNodePtr; /* synonmy for listNode */

    /* prototypes */
    void insert( ListNodePtr *sPtr, char value );
    char myDelete( ListNodePtr *sPtr, char value );
    int isEmpty( ListNodePtr *sPtr );
    void printList( ListNodePtr currentPtr );
    void instructions( void );

    int main( void )
    {
    ListNodePtr startPtr = NULL; /* initially there are no nodes */
    int choice; /* user’s choice */
    char item; /* char entered by user */

    instructions(); /* display the menu */
    printf( “? ” );
    scanf( “%d”, &choice );

    /* loop while user does not choose 3 */
    while( choice != 3 )
    {
    switch( choice )
    {
    case 1:
    printf( “Enter a character: ” );
    scanf( “\n%c”, &item );
    insert( &startPtr, item ); /* insert item in list */
    printList( startPtr );
    break;

    case 2: /* myDelete an element */

    /* if list is not empty */
    if ( !isEmpty( startPtr ) )
    {
    printf( “Enter character to be myDeleted: ” );
    scanf( “\n%c”, &item );

    /* if character is found, remove it */
    if ( myDelete ( &startPtr, item ) ); /* remove it */
    {
    printf( “%c myDeleted. \n”, item );
    printList( startPtr );
    } /* end if */
    else
    {
    printf( “%c not found. \n\n “, item );
    } /* end else */

    } /* end if */
    else
    {
    printf( “List is empty.\n\n” );
    } /*end else */

    break;

    default:
    printf( “Invalid choice.\n\n” );
    instructions();
    break;

    } /* end switch */

    printf( “? ” );
    scanf( “%d”, &choice );
    } /* end while */

    printf( “End of run.\n” );

    system(“PAUSE”);
    return 0; /* indicates successful termination */

    } /* end main */

    /* display program instructions to user */
    void instructions( void )
    {
    printf( “Enter your choice:\n”
    ” 1 to insert an element into the list.\n”
    ” 2 to myDelete an element from the list.\n”
    ” 3 to end.\n” );
    } /* end function instructions */

    /* Insert a new value into the list in sorted order */
    void insert( ListNodePtr *sPtr, char value );
    {
    ListNodePtr newPtr; /* pointer to new node */
    ListNodePtr previousPtr; /* pointer to previous node in list */
    ListNodePtr currentPtr; /* pointer to current node in list */

    newPtr = malloc( sizeof( ListNode ) ); /* create node */

    if ( newPtr != NULL ) /* is space available */
    {
    newPtr->data = value; /* place value in node */
    newPtr->nextPtr = NULL; /* node does not link to another node */

    previousPtr = NULL;
    currentPtr = *sPtr;

    /* loop to find the correct location in the list */
    while( currentPtr != NULL && value > currentPtr->data )
    {
    previousPtr = currentPtr; /* walk to… */
    currentPtr = currentPtr->nextPtr; /* …next node */
    } /* end while */

    /* insert new node at beginning of list */
    if ( previousPtr == NULL )
    {
    newPtr->nextPtr = *sPtr;
    *sPtr = newPtr;
    } /* end if */
    else /* insert new node between previousPtr and currentPtr */
    {
    previousPtr->nextPtr = newPtr;
    newPtr->nextPtr = currentPtr;
    } /* end else */

    } /* end if */
    else
    {
    printf( “%c not inserted. No memory available.\n”, value );
    } /* end else */

    } /* end function insert */

    /* myDelete a list element */
    char myDelete( ListNodePtr *sPtr, char value )
    {
    ListNodePtr previousPtr; /* pointer to previous node in list */
    ListNodePtr currentPtr; /* pointer to current node in list */
    ListNodePtr tempPtr; /* temporary node pointer */

    /* myDelete first node */
    if ( value == ( *sPtr )->data )
    {
    tempPtr = *sPtr; /* hold onto node being removed */
    *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
    free (tempPtr ); /* free the de-threaded node */
    return value;
    } /* end if */
    else
    {
    previousPtr = *sPtr;
    currentPtr = (*sPtr )->nextPtr;

    /* loop to find the correct location in the list */
    while( currentPtr != NULL && currentPtr->data != value )
    {
    previousPtr = currentPtr; /* walk to… */
    currentPtr = currentPtr->nextPtr; /* …next node */
    } /* end while */

    /* myDelete node at currentPtr */
    if ( currentPtr != NULL )
    {
    tempPtr = currentPtr;
    previousPtr = currentPtr;
    previousPtr->nextPtr = currentPtr->nextPtr;
    free (tempPtr );
    return value;
    } /* end if */

    } /* end else */

    return ‘\0’;

    } /* end function myDelete */

    /* Return 1 if the list is empty, 0 otherwise */
    int isEmpty ( ListNodePtr sPtr )
    {
    return sPtr == NULL;

    } /* end function isEmpty */

    /* Print the list */
    void printList ( ListNodePtr currentPtr )
    {

    /* if list is empty */
    if ( currentPtr == NULL )
    {
    printf( “List is empty.\n\n” );
    } /* end if */
    else
    {
    printf( “The list is:\n” );

    /* while not the end of the list */
    while( currentPtr != NULL )
    {
    printf( “%c –> “, currentPtr->data );
    currentPtr = currentPtr->nextPtr;
    } /* end while */

    printf( “NULL\n\n” );
    } /* end else */

    }/* end function function printList */

All Answers

  • Author
    Replies
    • #2922903

      Clarifications

      by samathacole ·

      In reply to Structured Linked List

      Clarifications

    • #2922860

      In what way doesn’t it work?

      by bizzo ·

      In reply to Structured Linked List

      Does it not compile?
      Or does it error in some way?

      • #2923447

        It doesn’t compile

        by samathacole ·

        In reply to In what way doesn’t it work?

        Im sorry but it just doesn’t work and my teacher told me to go on here.

        • #2923397

          If it doesn’t compile

          by bizzo ·

          In reply to It doesn’t compile

          It may have a syntax error in it somewhere, it might be useful if you can post the output from the compiler.

        • #2907933

          Uh sorry

          by samathacole ·

          In reply to If it doesn’t compile

          I am using a thin client and a laptop seperately cause I am inside of a dorm that won’t let me connect to the internet so I am a bit mixed up. Thank you for your patience. I tried to fix all the errors. I got a couple that said that the scanf was not right. I will post the output below the new code.

          new code:
          /* Crystal Jones IN72 CIT145 This is the structured linked list program that is assignment 5. It operates and maintains a list. */
          #include
          #include

          /* self-referential structure */
          struct listNode {
          char data; /*each ListNode contains a character */
          struct listNode *nextPtr; /* pointer to nest node */
          }; /* end structure listNode */

          typedef struct listNode ListNode; /* synonmy for struct listNode */
          typedef ListNode *ListNodePtr; /* synonmy for listNode */

          /* prototypes */
          void insert( ListNodePtr *sPtr, char value );
          char myDelete( ListNodePtr *sPtr, char value );
          int isEmpty( ListNodePtr *sPtr );
          void printList( ListNodePtr currentPtr );
          void instructions( void );

          int main( void )
          {
          ListNodePtr startPtr = NULL; /* initially there are no nodes */
          int choice; /* user’s choice */
          char item; /* char entered by user */

          instructions(); /* display the menu */
          printf( “? ” );
          scanf( “%d”, &choice );

          /* loop while user does not choose 3 */
          while( choice != 3 )
          {
          switch( choice )
          {
          case 1:
          printf( “Enter a character: ” );
          scanf( “\n%c”, &item );
          insert( &startPtr, item ); /* insert item in list */
          printList( startPtr );
          break;

          case 2: /* myDelete an element */

          /* if list is not empty */
          if ( !isEmpty( startPtr ) )
          {
          printf( “Enter character to be deleted: ” );
          scanf( “\n%c”, &item );

          /* if character is found, remove it */
          if ( myDelete ( &startPtr, item ) ) /* remove it */
          { printf( “%c deleted.\n”, item );
          printList( startPtr );
          } /* end if */
          else
          { printf( “%c not found. \n\n “, item );
          } /* end else */

          } /* end if */
          else
          { printf( “List is empty.\n\n” );
          } /*end else */

          break;

          default:
          printf( “Invalid choice.\n\n” );
          instructions();
          break;

          } /* end switch */

          printf( “? ” );
          scanf( “%d”, &choice );
          } /* end while */

          printf( “End of run.\n” );

          system(“PAUSE”);
          return 0; /* indicates successful termination */

          } /* end main */

          /* display program instructions to user */
          void instructions( void )
          {
          printf( “Enter your choice:\n”
          ” 1 to insert an element into the list.\n”
          ” 2 to delete an element from the list.\n”
          ” 3 to end.\n” );
          } /* end function instructions */

          /* Insert a new value into the list in sorted order */
          void insert( ListNodePtr *sPtr, char value )
          {
          ListNodePtr newPtr; /* pointer to new node */
          ListNodePtr previousPtr; /* pointer to previous node in list */
          ListNodePtr currentPtr; /* pointer to current node in list */

          newPtr = malloc( sizeof( ListNode ) ); /* create node */

          if ( newPtr != NULL ) /* is space available */
          {
          newPtr->data = value; /* place value in node */
          newPtr->nextPtr = NULL; /* node does not link to another node */

          previousPtr = NULL;
          currentPtr = *sPtr;

          /* loop to find the correct location in the list */
          while( currentPtr != NULL && value > currentPtr->data )
          {
          previousPtr = currentPtr; /* walk to… */
          currentPtr = currentPtr->nextPtr; /* …next node */
          } /* end while */

          /* insert new node at beginning of list */
          if ( previousPtr == NULL )
          { newPtr->nextPtr = *sPtr;
          *sPtr = newPtr;
          } /* end if */
          else /* insert new node between previousPtr and currentPtr */
          { previousPtr->nextPtr = newPtr;
          newPtr->nextPtr = currentPtr;
          } /* end else */

          } /* end if */
          else
          { printf( “%c not inserted. No memory available.\n”, value );
          } /* end else */

          } /* end function insert */

          /* myDelete a list element */
          char myDelete( ListNodePtr *sPtr, char value )
          {
          ListNodePtr previousPtr; /* pointer to previous node in list */
          ListNodePtr currentPtr; /* pointer to current node in list */
          ListNodePtr tempPtr; /* temporary node pointer */

          /* myDelete first node */
          if ( value == ( *sPtr )->data )
          {
          tempPtr = *sPtr; /* hold onto node being removed */
          *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
          free (tempPtr ); /* free the de-threaded node */
          return value;
          } /* end if */
          else
          {
          previousPtr = *sPtr;
          currentPtr = (*sPtr )->nextPtr;

          /* loop to find the correct location in the list */
          while( currentPtr != NULL && currentPtr->data != value )
          {
          previousPtr = currentPtr; /* walk to… */
          currentPtr = currentPtr->nextPtr; /* …next node */
          } /* end while */

          /* myDelete node at currentPtr */
          if ( currentPtr != NULL )
          {
          tempPtr = currentPtr;
          previousPtr = currentPtr;
          previousPtr->nextPtr = currentPtr->nextPtr;
          free (tempPtr );
          return value;
          } /* end if */

          } /* end else */

          return ‘\0’;

          } /* end function myDelete */

          /* Return 1 if the list is empty, 0 otherwise */
          int isEmpty ( ListNodePtr sPtr )
          {
          return sPtr == NULL;

          } /* end function isEmpty */

          /* Print the list */
          void printList ( ListNodePtr currentPtr )
          {

          /* if list is empty */
          if ( currentPtr == NULL )
          {
          printf( “List is empty.\n\n” );
          } /* end if */
          else
          {
          printf( “The list is:\n” );

          /* while not the end of the list */
          while( currentPtr != NULL )
          {
          printf( “%c –> “, currentPtr->data );
          currentPtr = currentPtr->nextPtr;
          } /* end while */

          printf( “NULL\n\n” );
          } /* end else */

          }/* end function function printList */

          output:
          —— Build started: Project: ProjectOne, Configuration: Debug Win32 ——
          Compiling…
          Struct_Link.cpp
          d:\summer2008\programming in c\five\struct_link.cpp(29) : warning C4996: ‘scanf’ was declared deprecated
          c:\program files\microsoft visual studio 8\vc\include\stdio.h(295) : see declaration of ‘scanf’
          Message: ‘This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.’
          d:\summer2008\programming in c\five\struct_link.cpp(38) : warning C4996: ‘scanf’ was declared deprecated
          c:\program files\microsoft visual studio 8\vc\include\stdio.h(295) : see declaration of ‘scanf’
          Message: ‘This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.’
          d:\summer2008\programming in c\five\struct_link.cpp(46) : error C2664: ‘isEmpty’ : cannot convert parameter 1 from ‘ListNodePtr’ to ‘ListNodePtr *’
          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
          d:\summer2008\programming in c\five\struct_link.cpp(101) : error C2440: ‘=’ : cannot convert from ‘void *’ to ‘ListNodePtr’
          Conversion from ‘void*’ to pointer to non-‘void’ requires an explicit cast
          Build log was saved at “file://c:\Documents and Settings\CrystalJones\My Documents\Visual Studio 2005\Projects\ProjectOne\ProjectOne\Debug\BuildLog.htm”
          ProjectOne – 2 error(s), 2 warning(s)
          ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Viewing 1 reply thread