-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw1_linked_list.c
More file actions
145 lines (134 loc) · 3.33 KB
/
hw1_linked_list.c
File metadata and controls
145 lines (134 loc) · 3.33 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
#include <stdio.h>
#include <stdlib.h>
typedef struct listItem
{
int someInteger;
struct listItem* next;
} list_Item;
list_Item* deleteItem (list_Item* lead, int ridListOf);
void printLinkedList (list_Item* begin);
void sortLinkedList (list_Item* head);
list_Item* insert(list_Item* head, int addStuff);
int main ()
{
list_Item* start = NULL;
int remove = 0;
int addStuff = 0;
char action = '\0';
do
{
printf("Enter action ");
printf("[i for insert, r for remove s for sort, q for quit]: ");
scanf (" %c",&action);
if (action == 'i')
{
printf("Enter data to be inserted: ");
scanf("%d",&addStuff);
start = insert(start,addStuff);
printLinkedList(start);
}
else if (action == 'r')
{
printf("Enter data to be removed: ");
scanf("%d",&remove);
start = deleteItem(start,remove);
printLinkedList(start);
}
else if (action == 's')
{
sortLinkedList(start);
printLinkedList(start);
}
else if (action == 'q')
{
printf("Bye.\n");
}
else
{
printf("Invalid Input.\n");
}
} while (action != 'q');
return 0;
}
list_Item* deleteItem (list_Item* lead, int ridListOf)
{
list_Item* deletePtr = lead;
list_Item* temp = lead;
if (lead == NULL)
{
printf("You can't delete anything from an empty list, dummy.\n");
return lead;
}
else if (lead->someInteger == ridListOf)
{
deletePtr = lead;
lead = lead->next;
free(deletePtr);
return lead;
}
else
{
while (deletePtr != NULL && deletePtr->someInteger != ridListOf)
{
temp = deletePtr;
deletePtr = deletePtr->next;
}
if(temp != NULL)
{
temp->next = deletePtr->next;
free(deletePtr);
return lead;
}
else
{
printf("Data not in list. \n");
return lead;
}
}
}
void printLinkedList (list_Item* begin)
{
list_Item* traverseList = begin;
printf("List is: ");
if (begin == NULL)
traverseList = NULL;
else
while (traverseList != NULL)
{
if (traverseList->next == NULL)
printf("%d\n", traverseList->someInteger);
else
printf("%d -> ", traverseList->someInteger);
traverseList = traverseList->next;
}
}
void sortLinkedList (list_Item* head)
{
list_Item* checkPtr = head;
list_Item* trailPtr = head;
int placeHolder = 0;
while (checkPtr != NULL)
{
if (checkPtr->someInteger < trailPtr->someInteger)
{
placeHolder = trailPtr->someInteger;
trailPtr->someInteger = checkPtr->someInteger;
checkPtr->someInteger = placeHolder;
checkPtr = trailPtr;
}
checkPtr = checkPtr->next;
if (checkPtr == NULL)
{
trailPtr = trailPtr->next;
checkPtr = trailPtr;
}
}
}
list_Item* insert(list_Item* head, int addStuff)
{
list_Item* temp = malloc(sizeof(list_Item));
temp->someInteger = addStuff;
temp->next = head;
head = temp;
return head;
}