-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACM2050q1skeleton.c
192 lines (131 loc) · 4.21 KB
/
ACM2050q1skeleton.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*Challenge
* Write a program that prints out all the words in a string in reverse order
* For this challenge, a word is defined to be anything delimited by whitespace
* I.E. "Hello World!" Hello and World! are the two words. & the output should be World! Hello
* Note, the program should preserve the same amount of white space
* I.E. "h i" is different from "h i"
* Fill in the reverseWords function
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 100
typedef struct list{
char data[MAX_LINE + 1];
struct list *next;
}ListT;
//Linked List Utility Functions
/*Note the parameters and be sure to pass by ref and val when appropriate */
void pop( ListT **head );
void push( ListT **head, char *data );
void popTail( ListT **head);
void pushTail( ListT **head, char *data );
void printList( ListT *head );
// Strip newline from inputted string
char *removeNewline(char *s);
// Function you'll fill out.
char *reverseWords(char *s);
int main ( void ){
char *myString = malloc(sizeof(char) * MAX_LINE);
printf("\nPlease enter a string: ");
fgets(myString, MAX_LINE - 1, stdin);
myString = removeNewline(myString);
printf("\nReverse the following string:\n%s\n", myString);
myString = reverseWords(myString);
printf("The string with the words reversed is:\n%s\n", myString);
free(myString);
printf("\n\n\n");
return 0;
}
void pop( ListT **head ){
//Temp variable used for node deletion.
ListT *temp = NULL;
//Check to see if list is empty
if( *head == NULL) return;
//Set the temp variable equal to the head and the head equal to its .next field.
temp = *head;
*head = (*head)->next;
//Print and free the previous head.
printf("%s", temp->data);
free(temp);
return;
}
void push( ListT **head, char *data ){
//Malloc space for the new node to be inserted.
ListT * newNode = malloc(sizeof(struct list));
//Copy data into the new node's data field, and set its .next field to NULL.
strcpy(newNode->data, data);
newNode->next = NULL;
//If the list is empty, set the head equal to the new node.
if( *head == NULL ){
*head = newNode;
return;
}
//Insert the new node into the front of the list.
newNode->next = *head;
*head = newNode;
return;
}
void pushTail( ListT **head, char *data ){
ListT * currentNode = ( *head );
//Malloc space for the new node
ListT * newNode = malloc(sizeof(struct list));
//Copy data into the new node's data field and set its .next field to NULL
strcpy(newNode->data, data);
newNode->next = NULL;
//If the list is empty, set the head equal to the new node.
if( *head == NULL ){
*head = newNode;
return;
}
//Iterate to the end of the list
//When the end is reached, append the new node
while( currentNode->next != NULL )
currentNode = currentNode->next;
currentNode->next = newNode;
return;
}
void popTail( ListT **head){
//Temp variable used for node deletion & current variable for iteration.
ListT * temp = NULL;
ListT * currentNode = *head;
ListT * previousNode = NULL;
//If the list is empty
if( *head == NULL ) return;
//If the list consist of only the head
if( (*head)->next == NULL ){
printf("%s" ,(*head)->data);
*head = NULL;
return;
}
//Set previous to head since it was already checked
//Set current to head->next
previousNode = *head;
currentNode = (*head)->next;
//Iterate to the end of the list
while( currentNode->next != NULL ){
previousNode = currentNode;
currentNode = currentNode->next;
}
//free the current node and set the previous .next field to NULL
temp = currentNode;
previousNode->next = NULL;
printf("%s", temp->data);
free(temp);
}
void printList( ListT *head ){
while(head != NULL){
printf("%s\n", head->data);
head = head->next;
}
return;
}
char *removeNewline(char *s) {
int len = strlen(s);
if (len > 0 && s[len-1] == '\n') // if there's a newline
s[len-1] = '\0'; // truncate the string
return s;
}
// Fill in this function
char *reverseWords(char *s) {
}