forked from programmer-ke/Algorithms-in-C-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
72 lines (64 loc) · 1.32 KB
/
linkedlist.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
#include <stdio.h>
#include <stdlib.h>
struct node { int key; struct node *next; };
struct node *head, *z, *t;
void listinitialize() {
head = (struct node *) malloc(sizeof(*head));
z = (struct node *) malloc(sizeof(*z));
head->next = z; z->next = z;
}
void deletenext(struct node *x)
{
t = x->next;
x->next = x->next->next;
free(t);
}
struct node* insertafter(int v, struct node* t)
{
struct node* x = (struct node*) malloc(sizeof(struct node));
x->key = v;
x->next = t->next;
t->next = x;
return x;
}
void printkeys()
{
t = head->next;
while(t->next != t) {
printf("%d ", t->key);
t = t->next;
}
}
void movenexttofront(struct node* x)
{
t = x->next;
x->next = x->next->next;
t->next = head->next;
head->next = t;
}
void exchange(struct node *t, struct node *u)
{
struct node *x, *y;
x = t->next;
y = u->next;
t->next = t->next->next;
u->next = u->next->next;
y->next = t->next; t->next = y;
x->next = u->next; u->next = x;
}
int main(int argc, char* argv[])
{
struct node *ptr1, *ptr2;
listinitialize();
ptr1 = insertafter(1, head);
ptr2 = insertafter(2, ptr1);
ptr1 = insertafter(3, ptr2);
printkeys(); printf("\n");
exchange(head, ptr2);
printkeys(); printf("\n");
movenexttofront(ptr2);
printkeys(); printf("\n");
deletenext(head);
printkeys();
return 0;
}