-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintVector-ll.c
More file actions
154 lines (142 loc) · 3.57 KB
/
intVector-ll.c
File metadata and controls
154 lines (142 loc) · 3.57 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
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} node;
typedef struct List {
int size; //number of nodes
node *firstNode; //first node
node *lastNode; //last node
} list;
void initialise(list *head) {
head->size=0;
head->firstNode=NULL;
head->lastNode=NULL;
}
void print(list *head) {
node *current = head->firstNode;
while(current != head->lastNode) {
printf("%d -> ", current->data);
current = current->next;
}
if (current != NULL) {
printf("%d\n", current->data);
}
}
void push(list *head, int value) {
if(head->size == 0) {
head->lastNode = (node*) malloc(sizeof(node));
head->firstNode=head->lastNode;
}
else {
head->lastNode->next = (node*) malloc(sizeof(node));
head->lastNode = head->lastNode->next;
}
head->lastNode->data = value;
head->lastNode->next = NULL;
head->size++;
}
int call(list *head, int index) {
if (index > head->size-1) {
printf("Error: Trying to access %d index! Structure only has %d elements.", index, head->size);
exit(1);
}
else {
node *current;
current = head->firstNode;
for (int i = 0; i < index; i++) {
current = current->next;
}
return current->data;
}
}
void change(list *head, int index, int value) {
if (index > head->size-1) {
printf("Error: Trying to access %d index! Structure only has %d elements.", index, head->size);
}
else {
node *current;
current = head->firstNode;
for (int i = 0; i < index; i++) {
current = current->next;
}
current->data = value;
}
}
void pop(list *head) {
if (head->size == 0) {
printf("Warning: Trying to pop from an empty list!\n");
return;
}
else if (head->size == 1) {
free(head->firstNode);
initialise(head);
return;
}
head->size--;
node *current = head->firstNode;
while(current->next != head->lastNode) {
current = current->next;
}
free(head->lastNode);
head->lastNode = current;
current->next = NULL;
}
void empty(list *head) {
node *current = head->firstNode;
for (int i = head->size; i >0; i--) {
head->firstNode = head->firstNode->next;
free(current);
current = head->firstNode;
head->size--;
}
head->lastNode = NULL;
}
void destroy(list *head) {
empty(head);
}
void reverseSlow(list *head) {
if (head->size == 0 || head->size == 1) {
return;
}
node *current = head->firstNode;
int k = head->size-2;
while (current->next != head->lastNode) {
current = current->next;
}
head->lastNode->next = current;
for (int i = 0; i < head->size-2; i++) {
node *currentR = current;
current = head->firstNode;
for (int j = k; j > 1; j--) {
current = current->next;
}
currentR->next = current;
k--;
}
head->firstNode->next = NULL;
node *temp;
temp = head->firstNode;
head->firstNode = head->lastNode;
head->lastNode = temp;
}
void reverse(list *head) {
if (head->size == 0 || head->size == 1) {
return;
}
node *x = head->firstNode;
node *y = x->next;
node *z = y->next;
for(int i = 0; i < head->size-2; i++) {
y->next = x;
x = y;
y = z;
z = z->next;
}
y->next = x;
head->firstNode->next = NULL;
z = head->firstNode;
head->firstNode = head->lastNode;
head->lastNode = z;
}