My first attempt at a linked list

this is a homework assignment

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2964 Views - Last Post: 18 April 2010 - 05:53 PM Rate Topic: -----

#16 cburkovi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 03-February 10

Re: My first attempt at a linked list

Posted 25 March 2010 - 06:50 AM

after all those fixes it looks like this
#include <stdio.h>
#include <stdlib.h>

//self-referential structure
struct listNode {            
   char data; 
   struct listNode *nextPtr; 
   struct listNode *prevPtr;
};// end structure listNode

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

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

int main(void)
{
 ListNodePtr startPtr = NULL;
 int choice;
 char item;
 
 instructions();
 printf("?");
 scanf("%d", &choice );
 
 while (choice != 3 ) {
 	   
		switch ( choice	) {
			   
			   case 1:// Enter an element
			   		printf("Enter a character: ");
					scanf("%c",&item );
					insert( &startPtr, item );
					printList( startPtr );
					break;
			   
			   case 2:// delete 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 ( _delete( &startPtr, item )) {//remove item
					  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 choise.\n\n" );
		         instructions();
		         break ;
		         
		}//end switch
		
		printf( "?" );
		scanf( "%d", &choice );
   }//end while
   
   printf( "End of run.\n" );
   
   system("pause");
   
   return 0;

}//end main

//display program instructions
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
void insert( ListNodePtr **sPtr, char value )
{
 	 ListNodePtr newPtr;
	 ListNodePtr previousPtr;
	 ListNodePtr currentPtr;
	 
	 newPtr = ( ListNode*) malloc( sizeof( ListNode ) );
	 
	 if (newPtr != NULL ) {
	 	newPtr->data + value;
	 	newPtr->nextPtr + NULL;
	 	
	 	previousPtr = NULL;
	 	currentPtr = **sPtr;
		
		//loop to find the correct location in the list
		while (currentPtr != NULL && value > currentPtr->data ) {
			  previousPtr = currentPtr;
			  currentPtr =currentPtr->nextPtr; 
        }//end while
		
		//insert new node at the beginning of the list<-------to be modded<----
		if ( previousPtr == NULL ) {
		   	 newPtr->nextPtr =**sPtr;
		   	 **sPtr = newPtr;
	    }//end if
		else {
			 previousPtr->nextPtr = newPtr;
			 newPtr->nextPtr = currentPtr;
	    }//end else
     
	 }//end if
	 else {
	 	  printf( "%c not inserted. no memory available.\n", value );
     }//end else
     
}//end function

// delete an element from the list			  
char _delete( ListNodePtr *sPtr, char value )		 		  
{
 	 ListNodePtr previousPtr;
 	 ListNodePtr currentPtr;
 	 ListNodePtr tempPtr;
 	 
 	 //Delete first node
 	 if  ( value == ( *sPtr )->data ) {
	 	 tempPtr = *sPtr;
	 	 *sPtr = ( *sPtr )->nextPtr;
	 	 free( tempPtr );
	 	 return value;
     }//end if
	 else {
	 	  previousPtr = *sPtr;
		   currentPtr = ( *sPtr )->nextPtr;
		   
		   //loop to find correct location in the list
		   while ( currentPtr != NULL && currentPtr->data != value ) {
		   		 previousPtr = currentPtr;
				 currentPtr = currentPtr->nextPtr;
	       }//end while
	       
	       //delete node at currentPtr
	       if ( currentPtr != NULL ) {
		   	  	tempPtr = currentPtr;
		   	  	previousPtr->nextPtr = currentPtr->nextPtr;
		   	  	free( tempPtr );
		   	  	return value;
		   }//end if
		   
     }//end else
	 
	 return '\0';

}//end function

//return 1 if the list is empty, 0 otherwise
int isEmpty( ListNodePtr sPtr )
{
 	return sPtr == NULL;
 	
}// end function
	 		   
//print list
void printList( ListNodePtr currentPtr )
{
 	 
	  //if list is empty
	  if ( currentPtr == NULL ) {
	  	   printf( "The list is empty.\n\n" );
      }
	  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


this I still need to add but I am doing one step at a time
/* aux->info = value;
*aux->next = *list;
*aux->prev = NULL:
*(*list)->prev = aux;
**list = aux;
*printf("The list is:\n");
*a --> NULL
*printf("The list in reverse is:\n");*/


Was This Post Helpful? 0
  • +
  • -

#17 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: My first attempt at a linked list

Posted 25 March 2010 - 06:54 AM

Please look at my above post, where I posted your fixed code.
Was This Post Helpful? 1
  • +
  • -

#18 cburkovi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 03-February 10

Re: My first attempt at a linked list

Posted 25 March 2010 - 07:15 AM

I found the errors from sarmanu thanks but now the outcome is not what I predicted would happen
case 1:// Enter an element                                         printf("Enter a character: ");                                         scanf("%c",&item );                                         insert( &startPtr, item );                                         printList( startPtr );                                         break;
should produce an out come like
Enter your choise:
1 to insert....
2 to delete....
3 to end.
?1
Enter a character: b
The list is:
b --> NULL

but my outcome is now
?1
Enter a character: The list is:
8 --> NULL

What in the heck did I do wrong I am totally lost
Was This Post Helpful? 0
  • +
  • -

#19 cburkovi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 03-February 10

Re: My first attempt at a linked list

Posted 01 April 2010 - 07:44 PM

okay now I have had the whole week to complete this assignment and I am struggling to get it to work correctly I found many errors over the past few weeks and found a big error
while (choice != 3 ) {
 	   
		switch ( choice	) {
			   
			   case 1:// Enter an element
			   		printf("Enter a character: ");
					scanf("\n%c",&item );
					insert( &startPtr, item );
					printList( startPtr );
					break;

when I debug it It fails on the outcome of the startPtr could this be the problem
void insert( ListNodePtr *sPtr, char value )
{
 	 ListNodePtr newPtr;
	 ListNodePtr previousPtr;
	 ListNodePtr currentPtr;
	 
	 newPtr = ( ListNode*) malloc( sizeof( ListNode ) );
	 
	 if (newPtr != NULL ) {
	 	newPtr->data + value;
	 	newPtr->nextPtr + NULL;
	 	
	 	previousPtr = NULL;
	 	currentPtr = *sPtr;
		
		//loop to find the correct location in the list
		while (currentPtr != NULL && value > currentPtr->data ) {
			  previousPtr = currentPtr;
			  currentPtr =currentPtr->nextPtr; 
        }//end while
		
		//insert new node at the beginning of the list<-------to be modded<----
		if ( previousPtr == NULL ) {
		   	 newPtr->nextPtr =*sPtr;
		   	 *sPtr = newPtr;
	    }//end if
		else {
			 previousPtr->nextPtr = newPtr;
			 newPtr->nextPtr = currentPtr;
	    }//end else
     
	 }//end if
	 else {
	 	  printf( "%c not inserted. no memory available.\n", value );
     }//end else
     
}//end function
and how can I fix it
Was This Post Helpful? 0
  • +
  • -

#20 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: My first attempt at a linked list

Posted 01 April 2010 - 10:11 PM

<yawn> well, I've read somewhere "Variable names should be descriptive" because it's somewhat hard to follow all that. Also, be sure to give a brief description of what each function does when you declare it.

I'm messing with your code and am unsure as to what you're trying to do! Is your linked list sorted? Are you supplying functions to add nodes to the beginning, end, or anywhere in the middle?

This post has been edited by taylorc8: 01 April 2010 - 10:30 PM

Was This Post Helpful? 0
  • +
  • -

#21 cburkovi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 03-February 10

Re: My first attempt at a linked list

Posted 18 April 2010 - 09:16 AM

a reattempt at repairing this
#include <stdio.h>
#include <stdlib.h>

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

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

/* prototypes */
void insert( ListNodePtr *sPtr, char value );
char _delete( ListNodePtr *sPtr, char value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
void revList( ListNodePtr prevPtr );
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:

            /* 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 ( _delete( &startPtr, item ) ) { /* remove item */
                  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" );
   
   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 = (ListNode*)malloc(sizeof(ListNodePtr)); /* 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 */

/* Delete a list element */
char _delete( 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 */

   /* delete 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 */

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

   return '\0';

} /* end function delete */

/* 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 printList */

/* Reversion of list */
void revList( ListNodePtr prevPtr )
{
 	 if ( prevPtr == Null ) {
	 	  return ;
		  }
		  else { 
		  	   printf("The list in reverse is:\n");
		  	   
		  	   while ( currentPtr != Null ) {
			   		 printf( "%c <-- ", previousPtr->data );
			   		 previousPtr = nextPtr->prevPtr;
					 }
					 
			  }
}
and these are my current errors
     In function `void revList(ListNode*)': 
203  `Null' undeclared (first use this function) 
  (Each undeclared identifier is reported only once for each function it appears in.) 
209  `currentPtr' undeclared (first use this function) 
210  `previousPtr' undeclared (first use this function) 
211  `nextPtr' undeclared (first use this function) 

please help I need the credit to get a "C" in the class
The program is supposed to let the user add to the nodes and print out the list forward and backwards
Was This Post Helpful? 0
  • +
  • -

#22 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: My first attempt at a linked list

Posted 18 April 2010 - 09:20 AM

Use "NULL" instead of "Null". And the Compiler is complaining about "currentPtr" and "previousPtr" not being recognized inside that function.
Was This Post Helpful? 1
  • +
  • -

#23 cburkovi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 03-February 10

Re: My first attempt at a linked list

Posted 18 April 2010 - 05:53 PM

View Postsarmanu, on 18 April 2010 - 08:20 AM, said:

Use "NULL" instead of "Null". And the Compiler is complaining about "currentPtr" and "previousPtr" not being recognized inside that function.

I am learning that C is very case sensitive and I misspell a lot of stuff so anyway this is the list as I wrote it. It almost works but when I run the program the List in reverse reverts to all NULL and then stops working in normal direction it works as advertized
#include <stdio.h>
#include <stdlib.h>

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

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

/* prototypes */
void insert( ListNodePtr *sPtr, char value );
char _delete( ListNodePtr *sPtr, char value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
void revList( ListNodePtr prevPtr );
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 );
            revList( startPtr );
            break;

         case 2:

            /* 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 ( _delete( &startPtr, item ) ) { /* remove item */
                  printf( "%c deleted.\n", item );
                  printList( startPtr );
                  revList( 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" );
   
   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 = (ListNode*)malloc(sizeof(ListNodePtr)); /* 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 */

/* Delete a list element */
char _delete( 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 */

   /* delete 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 */

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

   return '\0';

} /* end function delete */

/* 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 printList */

/* Reversion of list */
void revList( ListNodePtr prevPtr )
{
    ListNodePtr newPtr;      /* pointer to new node */
    ListNodePtr previousPtr; /* pointer to previous node in list */
    ListNodePtr currentPtr;  /* pointer to current node in list */ 
    
    ListNodePtr startPtr = prevPtr; 
 	
    if ( prevPtr == NULL ) {
	 	  return ;
		  } 
		  
		  else { 
		  	   printf("The list in reverse is:\n");
		  	   
		  	   currentPtr = prevPtr;//walk to...
		  	   
		  	   while ( currentPtr != NULL ) {
			   		 printf( "%c <-- ", previousPtr->data );
		   		     
                        ListNodePtr tempPtr;
			   		    tempPtr = currentPtr->nextPtr;//...previous node
			   		    currentPtr->nextPtr = currentPtr->prevPtr;
			   		    currentPtr->prevPtr = tempPtr;
			   		    currentPtr = currentPtr->nextPtr;
                  }
					 
					 
			  }// end else
              printf( "NULL\n\n" );
			  
			  
}// end function


This post has been edited by cburkovi: 18 April 2010 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2