CS117 Programming Methodology
Fall 1999

 

Code Reading in C

 

Instructions:

Examine the program on the following pages and answer these questions about the different tokens and constructs in the program listing. Hand your answers to me (RTH) before, or during class on Monday (Oct 18th).

 

1.        How many functions are declared globally?

2.        What are the names of the included files?

3.        On what line does the main function start?

4.        On what line does the main function end?

5.        How many local declarations does function delete contain?

6.        On what lines (note the plural) in function insert is there a unary operator?

7.        On what lines in function insert is there a while loop?

8.        On what line in function insert does the FIRST if-else statement end?

9.        On what line is there a character constant?

10.     On what line does an integer constant take part in a test?

11.     On what line is there a variable definition (declaration plus initialization)?

12.     On what lines in function delete is there a function call?

 


 

1                                /* Operating and maintaining a list */

2                                #include <stdio.h>

3                                #include <stdlib.h>

4                                 

5                                struct listNode {  /* self-referential structure */

6                                  char data;

7                                  struct listNode *nextPtr;

8                                };

9                                 

10                            typedef struct listNode LISTNODE;

11                            typedef LISTNODE *LISTNODEPTR;

12                             

13                            void insert(LISTNODEPTR *, char);

14                            char delete(LISTNODEPTR *, char);

15                            int isEmpty(LISTNODEPTR);

16                            void printList(LISTNODEPTR);

17                            void instructions(void);

18                             

19                            main()

20                            {

21                              LISTNODEPTR startPtr = NULL;

22                              int choice;

23                              char item;

24                             

25                              instructions();  /* display the menu */

26                              printf("? ");

27                              scanf("%d", &choice);

28                             

29                              while (choice != 3) {

30                             

31                                switch (choice) {

32                                  case 1:

33                                    printf("Enter a character: ");

34                                    scanf("\n%c", &item);

35                                    insert(&startPtr, item);

36                                    printList(startPtr);

37                                    break;

38                                  case 2:

39                                    if (!isEmpty(startPtr)) {

40                                       printf("Enter character to be deleted: ");

41                                       scanf("\n%c", &item);

42                                       if (delete(&startPtr, item)) {

43                                          printf("%c deleted.\n", item);

44                                          printList(startPtr);

45                                       }

46                                       else

47                                         printf("%c not found.\n\n", item);

48                                    }

49                                    else

50                                      printf("List is empty.\n\n");

51                                    break;

52                                  default:

53                                     printf("Invalid choice.\n\n");

54                                     instructions();

55                                     break;

56                                }

57                             

58                                printf("? ");

59                                scanf("%d", &choice);

60                              }

61                             

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

63                              return 0;

64                            }

65                             

66                            /* Print the instructions */

67                            void instructions(void)

68                            {

69                              printf("Enter your choice:\n"

70                                     "   1 to insert an element into the list.\n"

71                                     "   2 to delete an element from the list.\n"

72                                     "   3 to end.\n");

73                            }

74                             

75                            /* Insert a new value into the list in sorted order */

76                            void insert(LISTNODEPTR *sPtr, char value)

77                            {

78                              LISTNODEPTR newPtr, previousPtr, currentPtr;

79                             

80                              newPtr = malloc(sizeof(LISTNODE));

81                             

82                              if (newPtr != NULL) {    /* is space available */

83                                newPtr->data = value;

84                                newPtr->nextPtr = NULL;

85                                previousPtr = NULL;

86                                currentPtr = *sPtr;

87                             

88                                while (currentPtr != NULL && value > currentPtr->data) {

89                                  previousPtr = currentPtr;          /* walk to ...   */

90                                  currentPtr = currentPtr->nextPtr;  /* ... next node */

91                                }

92                             

93                                if (previousPtr == NULL) {

94                                   newPtr->nextPtr = *sPtr;

95                                   *sPtr = newPtr;

96                                }

97                                else {

98                                   previousPtr->nextPtr = newPtr;

99                                   newPtr->nextPtr = currentPtr;

100                            }

101                          }

102                          else

103                            printf("%c not inserted. No memory available.\n", value);

104                        }

105                         

106                        /* Delete a list element */

107                        char delete(LISTNODEPTR *sPtr, char value)

108                        {

109                           LISTNODEPTR previousPtr, currentPtr, tempPtr;

110                         

111                           if (value == (*sPtr)->data) {

112                             tempPtr = *sPtr;

113                             *sPtr = (*sPtr)->nextPtr;  /* de-thread the node */

114                             free(tempPtr);             /* free the de-threaded node */

115                             return value;

116                           }

117                           else {

118                             previousPtr = *sPtr;

119                             currentPtr = (*sPtr)->nextPtr;

120                         

121                             while (currentPtr != NULL && currentPtr->data != value) {

122                               previousPtr = currentPtr;          /* walk to ...   */

123                               currentPtr = currentPtr->nextPtr;  /* ... next node */

124                             }

125                         

126                             if (currentPtr != NULL) {

127                               tempPtr = currentPtr;

128                               previousPtr->nextPtr = currentPtr->nextPtr;

129                               free(tempPtr);

130                               return value;

131                             }                                                        

132                           }

133                         

134                           return '\0';

135               }

136                         

137                        /* Return 1 if the list is empty, 0 otherwise */

138                        int isEmpty(LISTNODEPTR sPtr)

139                        {

140                          return sPtr == NULL;

141                        }

142                         

143                        /* Print the list */

144                        void printList(LISTNODEPTR currentPtr)

145                        {

146                          if (currentPtr == NULL)

147                            printf("List is empty.\n\n");

148                          else {

149                            printf("The list is:\n");

150                         

151                          while (currentPtr != NULL) {

152                            printf("%c --> ", currentPtr->data);

153                            currentPtr = currentPtr->nextPtr;

154                          }

155                         

156                          printf("NULL\n\n");

157                        }