-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.cpp
75 lines (62 loc) · 1.55 KB
/
linkedList.cpp
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
#include <iostream>
using namespace std;
struct Node{
int val;
Node *next = NULL;
};
void insert(int value, Node *cursor){
//insere na lista logo após o elemento do cursor
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->val = value;
newNode->next = cursor->next;
cursor->next = newNode;
}
Node* getElementByIndex(Node *head, int pos){
Node *cursor = head;
int i = 0;
while(i < pos && cursor->next != NULL){
cursor = cursor->next;
i++;
}
return cursor;
}
Node* getElementByValue(Node *head, int value){
Node *cursor = head;
while(cursor->next != NULL && cursor->next->val != value){
cursor = cursor->next;
}
return cursor->next;
}
void removeElement(Node *cursor){
//remove o elemento do cursor->next
Node *p = cursor->next;
cursor->next = p->next;
int value = p->val;
free(p);
}
void printList(Node *init){
while(init != NULL){
cout << init->val << " ";
init = init->next;
}
cout << endl;
}
int main(){
int pos = 3;
Node *head = (Node*) malloc(sizeof(Node));
head->val = 1;
head->next = NULL;
insert(2, head);
insert(7, head);
insert(5, head);
insert(8, head);
insert(3, head);
printList(head);
Node *test = getElementByIndex(head, pos);
cout << test->val << " at Index: " << pos << endl;
Node *test2 = getElementByValue(head, 8);
cout <<"Value: " << test2->val << " &: " << test2 << endl;
removeElement(getElementByIndex(head, 1));
printList(head);
return 0;
}