From single to Doubly Link

Chapters 10, 12:
This is a two week assignment. Modify the linked list example in the book so that it is a doubly linked
list. Prove that your program works properly by implementing a print backwards function. The book's
code (6th edition) is at the end of the example output. You can just cut and paste it to get started. NOTE:
this is a C program – if you compile it as C++ you cannot use delete as that is a reserved keyword. Also,
since C++ is strongly typed, you must cast pointer allocations to the correct type.
You must understand the singly linked list code before you can start the doubly linked list code.
Your code must store in order.
If you have problems with improperly assigned pointers
You can redefine Node as:
struct listNode {
char data;
struct listNode *nextPtr;
struct listNode *prevPtr;
};
NOTE: I will be testing your code using the following sequence in this exact order –
Insert b a z k g m
Delete a z k g b m
This tests the 4 basic cases. Insert/delete on an empty list, insert/delete at the beginning,
insert/delete at the end, insert/delete at the end.
Example Program Session:
Enter your choice:
1 to insert an element into the list.
2 to delete an element from the list.
3 to end.
? 1
Enter a character: a
The list is:
a --> NULL
The list in reverse is:
a --> NULL
? 1
Enter a character: z
The list is:
a --> z --> NULL
The list in reverse is:
z --> a --> NULL
? 1
Enter a character: n
The list is:
a --> n --> z --> NULL
The list in reverse is:
z --> n --> a --> NULL
? 1
Enter a character: d
The list is:
a --> d --> n --> z --> NULL
The list in reverse is:
z --> n --> d --> a --> NULL
? 2
Enter character to be deleted: x
x not found.
? 2
Enter character to be deleted: n
n deleted.
The list is:
a --> d --> z --> NULL
The list in reverse is:
z --> d --> a --> NULL
? 2
Enter character to be deleted: a
a deleted.
The list is:
d --> z --> NULL
The list in reverse is:
z --> d --> NULL
? 2
Enter character to be deleted: z
z deleted.
The list is:
d --> NULL
The list in reverse is:
d --> NULL
? 2
Enter character to be deleted: d
d deleted.
List is empty.
? 1
Enter a character: s
The list is:
s --> NULL
The list in reverse is:
s --> NULL
? 1
Enter a character: t
The list is:
s --> t --> NULL
The list in reverse is:
t --> s --> NULL
? 3
End of run.
Press any key to continue . . .


Here is the code from the book which you can cut and paste:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
 /* Fig. 12.3: fig12_03.c
 Operating and maintaining a list */
#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*/
}; /* 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 );
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 = 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 */
/* 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 */
I redefined the node as instructed. WHat else am I supposd to ddo?
Error line 41 expression must be a pointer to a complete object type
Error line 14 expected an identifier
Error line 104 expected an identifier
Error line 78 a value of type "void *" cannot be assigned to an entity of type "ListNodePtr"
When I compiled your code using a C compiler, i received no errors. I did get warnings regarding the unsafe use of scanf.

Line 132: Causes a warning regarding the truncation of a character literal. You have an extraneous space in the character literal.

Make sure your compiler is set for C compilation, not C++ compilation.

Line 14,41,104: delete is a keyword in C++ and caused similar errors when I tried to compile as C/C++.

Line 78: C++ requires a cast which is the cause of your error. When compiled with C, this line is okay.


Last edited on
Topic archived. No new replies allowed.