/
singlyLinkedList.c
197 lines (181 loc) · 5.56 KB
/
singlyLinkedList.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
193
194
195
196
197
#include<stdio.h>
#include<stdlib.h>
//Sari Stissi
//Assigment 2
//Collaborated with Ethan Rountree, Laurence, Sara, and Jordan on this homework
struct student{
char *fname;
char *lname;
int age;
char *dob;
};
struct studentNode{
struct student *data;
struct studentNode *nextStudent;
};
struct singlyLinkedList{
struct studentNode *head;
struct studentNode *tail;
};
typedef struct student *student;
typedef struct studentNode *node;
typedef struct singlyLinkedList *linkedList;
student createStudent(char *fname, char *lname, int age, char *dob){
student temp;
temp = (student)malloc(sizeof(struct student));
temp->fname = fname;
temp->lname = lname;
temp->age = age;
temp->dob = dob;
return temp;
}
node createNode(student studentData){
node temp;
temp = (node)malloc(sizeof(struct studentNode));
temp->data = studentData;
temp->nextStudent = NULL;
return temp;
}
linkedList createList(){
linkedList temp;
temp = (linkedList)malloc(sizeof(struct singlyLinkedList));
temp->head = NULL;
temp->tail = NULL;
return temp;
}
void add_head(linkedList LinkedList, student studentToAdd){
node newNode = createNode(studentToAdd);
if(LinkedList->head == NULL){
LinkedList->head = newNode;
LinkedList->tail = newNode;
}
else{
newNode->nextStudent = LinkedList->head;
LinkedList->head = newNode;
}
}
void add_tail(linkedList LinkedList, student studentToAdd){
node newNode = createNode(studentToAdd);
if(LinkedList->tail == NULL){
LinkedList->head = newNode;
LinkedList->tail = newNode;
}
else{
LinkedList->tail->nextStudent = newNode;
LinkedList->tail = newNode;
}
}
void add_after(linkedList LinkedList, student findStudent, student studentToAdd){
node addAfter = createNode(findStudent);
node newNode = createNode(studentToAdd);
node currentNode = LinkedList->head;
node nextNode = LinkedList->head->nextStudent;
if(LinkedList->head == NULL && LinkedList->tail == NULL){
printf("%s\n", "Error: please add a head or a tail first");
}
while(currentNode->data != addAfter->data){
currentNode = nextNode;
nextNode = currentNode->nextStudent;
if(currentNode->nextStudent == NULL && currentNode->data != addAfter->data){
printf("%s\n", "Error: node not found");
break;
}
}
if(currentNode->nextStudent == NULL){
add_tail(LinkedList, newNode->data);
}
else{
currentNode->nextStudent = newNode;
newNode->nextStudent = nextNode;
}
}
int searchStudents(linkedList LinkedList, student findStudent){
node findStudentNode = createNode(findStudent);
node currentNode = LinkedList->head;
while(currentNode->data != findStudentNode->data){
currentNode = currentNode->nextStudent;
if(currentNode->nextStudent == NULL && currentNode->data != findStudentNode->data){
printf("%s\n", "Error: The student you're looking for was not found.");
return 0;
}
}
printf("%s\n", "The student is in the list!");
return 1;
}
void removeStudent(linkedList LinkedList, student studentToRemove){
node nodeToRemove = createNode(studentToRemove);
node currentNode = LinkedList->head;
node prevNode = NULL;
if(LinkedList->head == NULL && LinkedList->tail == NULL){
printf("%s\n", "Error: There are no nodes to remove");
}
else{
while(currentNode->data != nodeToRemove->data){
prevNode = currentNode;
currentNode = currentNode->nextStudent;
if(currentNode->nextStudent == NULL && currentNode->data != nodeToRemove->data){
printf("%s\n", "Error: The Student you were looking to remove was not found");
break;
}
}
if(prevNode == NULL){
if(currentNode->nextStudent == NULL){
LinkedList->head = NULL;
LinkedList->tail = NULL;
}
else{
LinkedList->head = currentNode->nextStudent;
currentNode->nextStudent = NULL;
}
}
else if(currentNode->nextStudent == NULL){
prevNode->nextStudent = NULL;
LinkedList->tail = prevNode;
}
else{
prevNode->nextStudent = currentNode->nextStudent;
currentNode->nextStudent = NULL;
}
}
}
void printIndivStudent(student StudentToPrint){
printf("First Name: %s, Last Name: %s, Age: %d, Date of Birth: %s\n",
StudentToPrint->fname,
StudentToPrint->lname,
StudentToPrint->age,
StudentToPrint->dob);
}
void printList(linkedList LinkedList){
node currentNode = LinkedList->head;
printf("%s:\n", "List of Students");
if(LinkedList->head == NULL && LinkedList->tail == NULL){
printf("%s\n", "There are no Students in this list.");
}
else{
while(currentNode->nextStudent != NULL){
student StudentToPrint = currentNode->data;
printIndivStudent(StudentToPrint);
currentNode = currentNode->nextStudent;
}
printIndivStudent(LinkedList->tail->data);
}
}
void main(){
student Student1 = createStudent("Sari", "Stissi", 20, "02/27/1998");
student Student2 = createStudent("Sara", "Salloum", 21, "03/24/1997");
student Student3 = createStudent("Bob", "Billy", 35, "06/23/1976");
student Student4 = createStudent("Jessica", "Star", 65, "10/05/1959");
student Student5 = createStudent("Bitch Boy", "Cluster Fuck", 69, "06/06/6666");
linkedList FirstList = createList();
add_head(FirstList, Student1);
add_head(FirstList, Student3);
add_tail(FirstList, Student2);
add_after(FirstList, Student1, Student4);
searchStudents(FirstList, Student3);
searchStudents(FirstList, Student5);
add_after(FirstList, Student2, Student5);
searchStudents(FirstList, Student5);
removeStudent(FirstList, Student5);
searchStudents(FirstList, Student5);
printList(FirstList);
}