#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");*/

New Topic/Question
Reply



MultiQuote




|