-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentOne.c
More file actions
227 lines (211 loc) · 5.47 KB
/
Copy pathAssignmentOne.c
File metadata and controls
227 lines (211 loc) · 5.47 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// Assignment 1
// COP3502
//
// Created by Joseph Orlando on 1/26/15.
// Copyright (c) 2015 Joseph Orlando. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// define booleans
typedef int bool;
#define true 1
#define false 0
struct node {
char name[50];
int id;
struct node *next;
}*head;
// compare two chars
bool compare(char a[50], char b[50])
{
if (strlen(a) != strlen(a))
// strings are different
return false;
for (int i = 0; i < strlen(a); i++)
{
if (a[i] != b[i]){
// strings are different
return false;
}
}
// strings are the same
return true;
}
// drop console lines for asthetic purposes
void dropLines(){
printf("\n\n\n\n");
}
// add node to linked list
int addNode(char name[50], int id){
struct node *temp;
temp =(struct node *)malloc(sizeof(struct node));
strcpy(temp->name, name);
temp->id = id;
if (head == NULL){
head = temp;
head->next = NULL;
}else{
temp->next = head;
head = temp;
}
return 1;
}
// display all nodes in linked list
int displayNode(){
if(head->name == NULL){
// no data
printf("No data has been saved to the node(s).");
}else{
// data has been saved to the node(s)
// output head info first
printf("Student Name:%50s\n", head->name);
printf("Student ID:%52d\n", head->id);
struct node *temp = head->next;
while(1){
if(temp->name == NULL){
// no more data
break;
}else{
printf("Student Name:%50s\n", temp->name);
printf("Student ID:%52d\n", temp->id);
temp = temp->next;
}
}
}
return 1;
}
// delete node by student id
bool deleteByID(int ID){
struct node *temp, *prev;
temp = head;
while(temp != NULL)
{
// temp is not empty
if(temp->id == ID)
{
if(temp == head)
{
head = temp->next;
free(temp); // free existing memory
printf("Student has been deleted!");
return true;
}
else
{
// push the list back one node
prev->next = temp->next;
free(temp);
printf("Student has been deleted!");
return true;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
printf("Student ID was not found!");
return true;
}
// delete node by student name, exact match
bool deleteByName(char name[50]){
struct node *temp, *prev;
temp = head;
while(temp != NULL)
{
// temp is not empty
if(compare(temp->name, name))
{
if(temp == head)
{
head = temp->next;
free(temp); // free existing memory
printf("Student has been deleted!");
return true;
}
else
{
// push the list back one node
prev->next = temp->next;
free(temp);
printf("Student has been deleted!");
return true;
}
}
else
{
prev = temp;
temp = temp->next;
}
}
printf("Student Name was not found!");
return true;
}
// populate nodes with pre-populated assignment data
int prepopulate(){
char line[50];
FILE datafile;
datafile = *fopen ("AssignmentOneData.txt", "r");
while(fgets(line, 50, &datafile) != NULL)
{
addNode(strtok(line, ","), atoi(strtok(NULL, ",")));
}
fclose(&datafile);
return true;
}
int main(int argc, const char * argv[]) {
head = NULL;
prepopulate();
while(1){
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Delete by ID\n");
printf("4. Delete by Name\n");
printf("5. Exit\n\n");
printf("Select an option: ");
int menu;
scanf("%d", &menu);
if(menu == 1){
dropLines();
char inputName[50];
int inputID;
printf("\nStudent Name: ");
fseek(stdin,0,SEEK_END); // clear input buffer
fgets(inputName, sizeof(inputName), stdin);
printf("\nStudent ID: ");
scanf("%d", &inputID);
addNode(inputName, inputID);
dropLines();
}else if(menu == 2){
dropLines();
displayNode();
dropLines();
}else if(menu == 3){
dropLines();
int deleteID;
printf("\nDelete ID: ");
fseek(stdin,0,SEEK_END); // clear input buffer
scanf("%d", &deleteID);
deleteByID(deleteID);
dropLines();
}else if(menu == 4){
dropLines();
char delName[50];
printf("\nDelete Name (EXACT): ");
fseek(stdin,0,SEEK_END); // clear input buffer
fgets(delName, sizeof(delName), stdin);
deleteByName(delName);
dropLines();
}else if(menu == 5){
printf("Have a great day!");
return 1; // exit program
}else{
printf("%d is not a valid choice! Please try again\n\n", menu);
menu = 0; // so while loop doesnt go out of control
}
}
return 0;
}