-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListAssignment3.c
More file actions
111 lines (96 loc) · 2.43 KB
/
LinkedListAssignment3.c
File metadata and controls
111 lines (96 loc) · 2.43 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
/**
*
* @author Nash Muñoz
*/
#include <stdio.h>
#define MAX 50
typedef char String[100];
typedef struct {
char Elem[MAX];
int count;
} List;
/* Displays the list */
void displayList(String msg, List L){
int x;
printf("%s: (", msg);
for(x=0; x<L.count; x++){
if(x==L.count-1)
printf("%c", L.Elem[x]);
else
printf("%c, ", L.Elem[x]);
}
printf(")\n");
}
/* Inserts a character at a certain position */
void insert(char elem, int p, List *L) {
if(L->count<MAX && (p<=L->count && p>=0)){
if(p!=L->count){
int x;
for(x=L->count; x>p; x--){
L->Elem[x] = L->Elem[x-1];
}
}
L->Elem[p] = elem;
L->count++;
}
}
/* Deletes an element at a certain position */
void delete(int p, List *L) {
if(p<L->count && p>=0) {
if(p!=L->count-1){
int x;
for(x=p; x<L->count-1; x++){
L->Elem[x] = L->Elem[x+1];
}
}
L->count--;
}
}
/* Finds the given element and returns the position */
int locate(char elem, List L){
int x;
for(x=0; x<L.count && L.Elem[x]!=elem; x++){}
return (x<L.count) ? x : -1;
}
/* Version 1 of inserting an element in a sorted list (With function call) */
void insertSortedV1(char elem, List *L){
if(L->count<MAX) {
int x;
for(x=0; x<L->count && elem>L->Elem[x]; x++){}
insert(elem, x, L);
}
}
/* Version 2 of inserting an element in a sorted list (Without function call) */
void insertSortedV2(char elem, List *L){
if(L->count<MAX){
int x;
for(x=0; x<L->count && elem>L->Elem[x]; x++){}
if(x!=L->count){
int y;
for(y=L->count; y>x; y--){
L->Elem[y] = L->Elem[y-1];
}
}
L->Elem[x] = elem;
L->count++;
}
}
int main() {
List myList = {
{},
0
};
displayList("Before Insertion", myList);
insert('E', 0, &myList);
displayList("After Insertion", myList);
delete(0, &myList);
displayList("After Deletion", myList);
insert('D', 0, &myList);
displayList("After Insertion", myList);
printf("Location of F: %d\n", locate('F', myList));
insertSortedV1('A', &myList);
displayList("After Insertion(SortedV1)", myList);
insertSortedV2('C', &myList);
displayList("After Insertion(SortedV2)", myList);
return 0;
}