Hi thanks. Sorry about that, maybe I should have posted all the code in the first place.
Here is the code that I have manipulated so far:
Code:
#include <stdio.h>
#include <stdlib.h>
/*self referential structure*/
struct listnode{
char data;
struct listnode *prevptr, *nextptr; /*"prevptr" was not part of the original code, I had to make this program a doubly linked list.*/
};
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 printreverse(listnodeptr currentptr);/*basically a copy of the original "printlist" but in reverse.
void instructions(void);
int main(void)
{
listnodeptr startptr = NULL; /*no initial nodes*/
int choice; /*user choice*/
char item; /*entered by user*/
instructions(); /*display menu*/
printf("? ");
scanf("%d", &choice);
/*loop for user choice*/
while(choice != 3){
switch(choice){
case 1:
printf("Enter a character: ");
scanf("\n%c", &item);
insert(&startptr, item); /*enter item into linked list*/
printlist(startptr);
printreverse(startptr);
break;
case 2: /*delete an element*/
/*check list, if not empty*/
if(!isEmpty(startptr)){
printf("Enter character to be deleted: ");
scanf("\n%c", &item);
/*if character is found, delete*/
if(delete(&startptr, item)){
printf("%c deleted.\n", item);
printlist(startptr);
printreverse(startptr);
}
else{
printf("%c not found.\n\n", item);
}
}
else{
printf("List is empty.\n\n");
instructions();
}
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;
}
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 instructions*/
/*Insert a new element and sort*/
void insert(listnodeptr *sptr, char value)
{
listnodeptr newptr; /*pointer to new node*/
listnodeptr currentptr; /*pointer to current node in list*/
/*"listnode previousptr" was declared here, but I removed it since it was declared earlier on*/
newptr = malloc(sizeof(listnode)); /*allocate new space*/
if(newptr != NULL){ /*is space available*/
newptr->data = value; /*place choice in node*/
newptr->nextptr = NULL; /*New node points to NULL*/
newptr->prevptr = NULL;/*this replaced "previousptr"*/
currentptr = *sptr;
/*loop to find correct location in list*/
while(currentptr != NULL && value > currentptr->data){
newptr->prevptr = currentptr; /*walk through list*//*this replaced "previousptr"*/
currentptr = currentptr->nextptr;
}/*end while*/
/*insert new node at start of list*/
if(currentptr->prevptr == NULL){/*was "previousptr"*/
newptr->nextptr = *sptr;
if(*sptr != NULL){
(*sptr)->prevptr = newptr; /*new code*/
}
*sptr = newptr;
}/*end if*/
else{
newptr->nextptr->prevptr = newptr;
newptr->prevptr->nextptr = newptr;
newptr->nextptr = currentptr;
if (currentptr != NULL){
currentptr->prevptr = newptr;
}
}/*end else*/
}/*end if*/
else{
printf("%c not inserted. No memory available.\n", value);
}
}/*end function insert*/
char delete(listnodeptr *sptr, char value)
{
listnodeptr currentptr; /*pointer to current element in list*/
listnodeptr tempptr; /*temporary pointer*/
/*"listnode previousptr" was declared here, but I removed it since it was declared earlier on*/
if(value == (*sptr)->data){
tempptr = *sptr; /*place element in temp location*/
*sptr = (*sptr)->nextptr; /*take element out of list*/
free(tempptr); /*remove element from memory*/
return value;
}/*end if*/
else{
*sptr = (*sptr)->prevptr;
currentptr = (*sptr)->nextptr; /*I changed this, but it didn't work*/
/*loop to find correct location in list*/
while(currentptr != NULL && currentptr->data != value){
(*sptr)->prevptr = currentptr; /*go through list*/
currentptr = currentptr->nextptr; /*this didn't work either*/
}/*end while*/
/*insert element at start of list*/
if(currentptr == NULL){
tempptr = currentptr;
currentptr->prevptr = currentptr->nextptr;/*again, didn't work*/
free(tempptr);
return value;
}/*end if*/
}/*end else*/
return '\0';
}/*end function delete*/
/*return 1 if the list is empty*/
int isEmpty(listnodeptr sptr)
{
return sptr == NULL;
}/*end function isEmpty*/
/*print function*/
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");
/*run through list*/
while(currentptr != NULL){
printf("%c --> ", currentptr->data);
currentptr = currentptr->nextptr;
}/*end while*/
printf("NULL\n\n");
}/*end else*/
}/*end function printlist*/
void printreverse (listnodeptr currentptr)
{
listnodeptr temp = NULL;
while (currentptr != NULL ) {
temp = currentptr;
currentptr = currentptr->nextptr;
}
printf( "\nThe list in reverse is:\n" );
printf( "NULL" );
currentptr = temp;
while ( currentptr != NULL) {
printf( " <-- %c", currentptr->data );
currentptr = currentptr->prevptr;
}
printf("\n\n");
}
I've highlighted the changes. I hope it makes it easier to read.