I'm trying to change this program into a doubly linked list but i'm having a difficult time. Please help me out thank you.

#include <stdio.h>
#include <stdlib.h>

struct Node
{ 
	char data;
    Node *next; 
    Node *prev; 
};
//typedef struct node Node;
typedef Node *NodePtr;

void insert( NodePtr *dPtr, int value );
int Delete(  NodePtr *dPtr, int value );
int isEmpty( NodePtr dPtr );
void printList( NodePtr currentPtr );
void instructions(void);

int main(void)
{
	NodePtr startPtr = NULL;
	int choice;
	int item;

	instructions();
	printf("? ");
	scanf( "%d", &choice );

	while ( choice != 3)
	{
		switch ( choice )
		{
		   case 1:
			   printf( "Enter a number: ");
			   scanf( "\n%d", &item );
			   insert( &startPtr, item );
			   printList( startPtr );
			   break;

		   case 2:

			   if (!isEmpty( startPtr ) )
			   {
				   printf( "Enter nuber to be deleted: " );
				   scanf( "\n%d", &item );

				   if ( Delete( &startPtr, item ) )
				   {
					   printf( "%d delete.\n", item );
					   printList( startPtr );
				   }
				   else
				   {
					   printf( "%d not found.\n\n", item );
				   }
			   }
			   else
			   {
				   printf( "List is empty.\n\n" );
			   }
			   break;

		   default:
			   printf( "Invalid choice.\n\n" );
			   instructions();
			   break;
		}//end switch

		printf( "? " );
		scanf( "%d", &choice );
	}//end while

	printf( "End of run.\n" );

	return 0;

}//end main

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 instrution

void insert( NodePtr *dPtr, int value )
{
	NodePtr newPtr;
	//NodePtr previousPtr;
	NodePtr currentPtr;

	newPtr = malloc( sizeof( Node ) );

	if ( newPtr != NULL ) 
	{
		newPtr->data = value;
		newPtr->nextPtr = NULL;
		newPtr->prevPtr = NULL;

//		previousPtr = NULL;
		currentPtr = *dPtr;

		while ( currentPtr != NULL && value > currentPtr->data )
		{
			previousPtr = currentPtr;
			currentPtr = currentPtr->nextPtr;
			currentPtr = currentPtr->prevPtr;
		}//end while

		if ( previousPtr == NULL )
		{
			newPtr->nextPtr = *dPtr;
			//newPtr->prevPtr = *dPtr;
			*dPtr = newPtr;
		}//end if
		else
		{
			previousPtr->nextPtr = newPtr;
			newPtr->nextPtr = currentPtr;
		}//end else

	}//end if
	else
	{
		printf( "%d not inserted. No memory available.\n", value );
	}//end else

}//end function insert

int Delete( NodePtr *dPtr, int value )
{
	//NodePtr previousPtr;
	NodePtr currentPtr;
	NodePtr tempPtr;

	if ( value == ( *dPtr )->data )
	{
		tempPtr = *dPtr;
		*dPtr = ( *dPtr )->nextPtr;
		*dPtr = ( *dPtr )->prevPtr;
		free( tempPtr );
		return value;
	}//end if
	else
	{
		previousPtr = *dPtr;
		currentPtr = ( *dPtr )->nextPtr;
		currentPtr = ( *dPtr )->prevPtr;

		while ( currentPtr != NULL && currentPtr->data != value )
		{
			previousPtr = currentPtr;
			currentPtr = currentPtr->nextPtr;
			currentPtr = currentPtr->prevPtr;
		}//end while

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

	}//end else

	return 0;

}//end function delete

int isEmpty( NodePtr dPtr )
{
	return dPtr == NULL;
}//end of function isEmpty

void printList( NodePtr currentPtr )
{
	if ( currentPtr == NULL )
	{
		printf( "List is empty.\n\n" );
	}//end if
	else
	{
		printf( "The list is:\n" );

		while ( currentPtr != NULL )
		{
			printf( "%d --> ", currentPtr->data );
			currentPtr = currentPtr->nextPtr;
		}//end while

		printf( "NULL\n\n" );
	}//end else
}//end funstion printList

Recommended Answers

All 4 Replies

>Please help me out thank you.
Sorry dude, but you didn't ask a question. I'm not inclined to help you when your request for help smells like you're saying "please do it for me!!!11".

why are you using a structure? Use object oriented programming with classes lot easier and use recursion. May take more time to come up with but it is easier to understand

>Use object oriented programming with classes lot easier
Actually, when you do both right, the non-class version is vastly simpler to implement.

p.s. This is the C forum.

thnx everyone i figure it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.