This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySqList.h
111 lines (99 loc) · 2.25 KB
/
mySqList.h
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: SciZeal
* email: SciZeal@outlook.com
* time: 2020-05-13
*
* MIT LICENCE
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct SqList sqlist;
void lprintf(sqlist list);
sqlist initSqList(int size);
int findElement(sqlist list, int element);
sqlist addElement(sqlist list, int element, int index);
sqlist removeElement(sqlist list, int element);
sqlist amendElement(sqlist list, int element, int newElement);
typedef struct SqList
{
int *head; // array of list elements
int length; // list length
int size; // list size
} sqlist;
// initialize a sequence list
sqlist initSqList(int size)
{
sqlist list;
list.head = (int *)malloc(size * sizeof(int));
// if failed
if (!list.head)
{
printf("Initialization failed!\n");
}
list.length = 0;
list.size = size;
return list;
}
// print the sequence list
void lprintf(sqlist list)
{
printf("%d", *list.head);
for (int i = 1; i < list.length; i++)
{
printf(" %d", list.head[i]);
}
printf("\n");
}
// find element in the sequence list by sequential search
int findElement(sqlist list, int element)
{
for (int i = 0; i < list.length; i++)
{
if (element == list.head[i])
{
return i;
}
}
return -1;
}
// add element into sequence list
sqlist addElement(sqlist list, int element, int index)
{
// if invalid index
if (index > list.length || index < 0)
{
printf("Invalid index!\n");
return list;
}
for (int i = list.length - 1; i >= index; i--)
{
list.head[i + 1] = list.head[i];
}
list.head[index] = element;
list.length++;
return list;
}
// remove element out of sequence list
sqlist removeElement(sqlist list, int element)
{
while (findElement(list, element) >= 0)
{
int index = findElement(list, element);
for (int i = index + 1; i < list.length; i++)
{
list.head[i - 1] = list.head[i];
}
list.length--;
}
return list;
}
// change element in the sequence list
sqlist amendElement(sqlist list, int element, int newElement)
{
int index = findElement(list, element);
if (index >= 0)
{
list.head[index] = newElement;
}
return list;
}