-
Notifications
You must be signed in to change notification settings - Fork 0
/
nshRemove.c
46 lines (42 loc) · 1.02 KB
/
nshRemove.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
#include <string.h>
#include <stdio.h>
#include "node.h"
#include "defines.h"
#include <stdlib.h>
int nshRemove(struct node *head, char *varName){
// create nodes to track the place
struct node *place;
struct node *previous;
place = head;
previous = place;
// check if no nodes in the list
if (head == NULL) {
return onlyOneNode;
}
// if head needs to be deleted its the only node in the list
else if (strcmp(head->name, varName)==0 && head->next == NULL) {
return onlyOneNode;
}
// if head needs to be deleted and its NOT the only node in the list
else if (strcmp(head->name, varName)==0){
return popHead;
}
// for all other nodes that would need to be deleted
else {
while(place != NULL){
if (strcmp(place->name,varName) == 0 && place->next == NULL) {
previous->next = NULL;
return success;
}
else if (strcmp(place->name,varName)==0) {
previous->next = place->next;
return success;
}
else {
previous = place;
place = (node*) nshNext(place);
}
}
}
return success;
}