-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lista Enlazada
71 lines (64 loc) · 1.23 KB
/
Lista Enlazada
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
};
void borrar(struct node *head, int key)
{
struct node *cur, *prev, *temp;
prev = head;
cur = head -> next;
while (cur != NULL)
{
if (head ->val== key)
{
temp = head;
head = head -> next;
free(temp);
break;
}
else {
if (cur -> val == key)
{
prev -> next = cur -> next;
free(cur);
break;
}
}
prev = cur;
cur = cur -> next;
}
}
void crearList(struct node *head, int n)
{
struct node *prev, *cur;
head -> val = 1;
int i;
prev = head;
for(i=2;i<= n;i++)
{
cur = malloc(sizeof(struct node));
cur ->val = i;
prev ->next = cur;
prev = cur;
}
prev -> next=NULL;
}
main()
{
struct node *head;
head = malloc(sizeof(struct node));
head -> val = 1;
crearList(head,3); /*Crea la lista enlazada con Head */
struct node *t;
int key=3;
borrar(head,key); /*Borra el elemento */
t=head;
while (t!=NULL)
{
printf("%d\n",t->val);
t = t -> next;
}
}