-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlists.c
171 lines (148 loc) · 3.69 KB
/
lists.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
#include <stdio.h>
#include <stdlib.h>
typedef struct {
void *next;
int data;
} Node;
Node *head = NULL;
// add a node to the list
Node *addNode(int data)
{
Node *new = NULL;
// two cases:
// if the list is empty.
if (head == NULL)
{
new = malloc(sizeof(Node));
if (new == NULL)
return NULL;
new->data = data;
head = new;
new->next = NULL;
} else {
new = malloc(sizeof(Node));
if (new == NULL)
return NULL;
new->data = data;
new->next = head;
head = new;
}
return new;
}
// remove a node from the list
int removeNode(int data)
{
Node *current = head;
Node *prev = head;
while (current != NULL)
{
if (current->data == data)
{
// if current node is the list head
if (current == head)
{
head = current->next;
} else {
prev->next = current->next;
free(current);
current = NULL;
}
return 1;
}
prev = current;
current = current->next;
}
return 0;
}
// insert a node into a position in the list
Node *insertNode(int data, int position)
{
Node *current = head;
while (current != NULL && position != 0)
{
position--;
}
if (position != 0)
{
printf("Requested position too far into list\n");
return NULL;
}
Node *new = malloc(sizeof(Node));
if (new == NULL)
return NULL;
new->data = data;
new->next = current->next;
current->next = new;
return new;
}
// print operation
void printList()
{
Node *current = head;
while (current != NULL)
{
printf("%d->", current->data);
current = current->next;
}
printf("\n");
return;
}
void printMenu()
{
printf("You have the following options:\n");
printf("\t1. Add a node to the list.\n");
printf("\t2. Remove a node from the list.\n");
printf("\t3. Insert a node to the list.\n");
printf("\t4. Print your list\n");
printf("\t5. Quit.\n");
return;
}
int main(int argc, char **argv)
{
int option = -1;
int arg1 = 0;
int arg2 = 0;
while (option != 5)
{
printMenu();
int num_received = scanf("%d", &option);
if (num_received == 1 && option > 0 && option <= 5)
{
switch(option)
{
case 1:
// add operation
printf("What data should I insert?:\n");
scanf("%d", &arg1);
Node *new = addNode(arg1);
break;
case 2:
// remove operation
printf("What data should I remove?:\n");
scanf("%d", &arg1);
int success = removeNode(arg1);
if (!success)
printf("Element not found\n");
break;
case 3:
// insert operation
// remove operation
printf("What data should I insert?:\n");
scanf("%d", &arg1);
printf("What position?:\n");
scanf("%d", &arg2);
new = insertNode(arg1, arg2);
if (new == NULL)
printf("Failed to insert into list\n");
break;
case 4:
// print the list
printList();
break;
case 5:
break;
}
}
}
return 0;
}