-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
90 lines (78 loc) · 2.1 KB
/
linked_list.c
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
#include <stdio.h>
#include <stdlib.h>
/*
L in k e d L i s t . h
TAD para r e p r e s e n t a r uma L i s t a Encadeada .
*/
typedef struct node{ // define o tipo nó;
int item;
void *next;
}Node; //o apelido desse tipo é Node
Node* createLinkedList()
{
return NULL;//pq tem que ser null????
}
int isEmpty( Node *head)
{
return (head == NULL);
}
/*
malloc retorna um endereço de um espaço de memória alocado
ponteiro é um referenciador, se ele não for inicializado, ele aponta para qualquer lugar
quando eu crio um novo ponteiro, ele precisa de um lugar para apontar
se um faço um ponteiro que nao aponta pra lugar nenhum.... memory leak
(Node*) malloc(sizeof(Node)) é um endereço para tipo Node
*/
Node* insertNode (Node *head, int item) //insere um nó na cabeça
{
Node* new_node = (Node*) malloc(sizeof(Node)); //crio um novo ponteiro(endereço do tipo Node) com o nome de new_node... esse endereço é igual a outro endereço
new_node->item = item;
new_node->next = head;
return new_node; //aqui new_node para de existir
}
void printLinkedList(Node *head)
{
while (isEmpty(head) != 1)
{
printf("%d\n", head->item);
head = head->next;
}
return;
}
Node* search(Node *head, int item) //retorna o nó do item
{
while (head != NULL)
{
if (head->item == item)
{
return head;
}
head = head->next;
}
return NULL;
}
Node* removeNode(Node *head, int item)
{
Node* itemplace = search(head,item); //aqui eu encontro em qual nó está o item
if(itemplace == NULL)
{
printf("This item is not stored on the list.");
return head;
}
else
{
//TODO #1 criar um struct pra me dar o previous e o current;
}
free(itemplace);
return NULL;
}
int main()
{
Node* lista1 = createLinkedList();
/* insertNode(Node *head , int item );
void printLinkedList( Node *head );
int isEmpty (Node *head );
search(Node *head , int item );
removeNode( Node *head , int item ); */
return 0;
}