-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad1jc.cpp
93 lines (77 loc) · 1.6 KB
/
zad1jc.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include<stdlib.h>
typedef struct _node {
int val;
struct _node *prev, *next;
} node;
typedef struct _lista {
node *head, *tail;
} lista;
lista nowaLista(){
lista wynik = {NULL, NULL};
return wynik;
}
bool czyPusta(lista l){
return (!l.head && !l.tail);
}
void push_back(int val, lista &l){
node *nowy = (node*)malloc(sizeof(node));
nowy->prev = l.tail;
nowy->next = NULL;
nowy->val = val;
if(!czyPusta(l)){
l.tail->next = nowy;
} else {
l.head = nowy;
}
l.tail = nowy;
}
int front(lista l){
return l.head->val;
}
void pop_front(lista &l){
if(!czyPusta(l)){
node *nast = l.head->next;
free(l.head);
l.head = nast;
if(l.head) l.head->prev = NULL;
else l.tail = NULL;
}
}
void printList(lista l){
node *it = l.head;
bool koniec = false;
while(!koniec && !czyPusta(l)){ //uwzględnia null, null
if(it == l.tail) koniec = true;
printf("%i ", it->val);
it = it->next;
}
printf("\n");
}
void dubluj(lista &l){
if(czyPusta(l)) return;
node *nowy = NULL;
node *it = l.head;
while(it){
nowy = (node*)malloc(sizeof(node));
nowy->val = it->val;
nowy->prev = it;
nowy->next = it->next;
if(it->next){
it->next->prev = nowy;
}
it->next = nowy;
it = nowy->next;
}
l.tail = nowy;
}
int main(){
lista l = nowaLista();
push_back(1, l);
push_back(2, l);
push_back(3, l);
push_back(4, l);
dubluj(l);
printList(l);
return 0;
}