Commit 455e0054be9b9fe36a69238278b1be6d76774523
0 parents
Initial version
Showing
100 changed files
with
1999 additions
and
0 deletions
Too many changes to show.
To preserve performance only 100 of 477 files are displayed.
examples01-en/01-firstcpp/hellocpp.cpp
0 → 100644
examples01-en/01-firstcpp/makefile
0 → 100644
examples01-en/02-string/makefile
0 → 100644
examples01-en/02-string/string.cpp
0 → 100644
1 | +++ a/examples01-en/02-string/string.cpp | ||
1 | +#include <iostream> | ||
2 | +#include <string> | ||
3 | +using namespace std; | ||
4 | + | ||
5 | +string getword() | ||
6 | +{ | ||
7 | + string s; | ||
8 | + cin >> s; | ||
9 | + return s; | ||
10 | +} | ||
11 | + | ||
12 | + | ||
13 | +int main() | ||
14 | +{ | ||
15 | + cout << "Podaj slowo" << endl; | ||
16 | + cout << "Podane slowo to:" << getword() << endl; | ||
17 | +} |
examples01-en/03-vector/makefile
0 → 100644
examples01-en/03-vector/vector.cpp
0 → 100644
1 | +++ a/examples01-en/03-vector/vector.cpp | ||
1 | +#include <iostream> | ||
2 | +#include <vector> | ||
3 | +#include <string> | ||
4 | +using namespace std; | ||
5 | + | ||
6 | +int main() | ||
7 | +{ | ||
8 | + string word; | ||
9 | + vector<string> v; | ||
10 | + while(cin) | ||
11 | + { | ||
12 | + if(cin>>word) | ||
13 | + v.push_back(word); | ||
14 | + } | ||
15 | + for(unsigned int i=0;i<v.size();i++) | ||
16 | + cout << v[i]<<" "; | ||
17 | + cout << endl; | ||
18 | +} |
examples01-en/04-qt-1/hello.cpp
0 → 100755
examples01-en/04-qt-1/prepare.sh
0 → 100755
examples01-en/05-qt-2/hello.cpp
0 → 100755
1 | +++ a/examples01-en/05-qt-2/hello.cpp | ||
1 | +#include <QApplication> | ||
2 | +#include <QPushButton> | ||
3 | + | ||
4 | +int main(int argc, char *argv[]) | ||
5 | +{ | ||
6 | + QApplication app(argc, argv); | ||
7 | + | ||
8 | + QPushButton hello("Hello world!"); | ||
9 | + hello.resize(100, 30); | ||
10 | + hello.setFont(QFont("Arial", 18, QFont::Bold)); | ||
11 | + hello.setGeometry(400, 200 , 180, 40); | ||
12 | + | ||
13 | + hello.show(); | ||
14 | + return app.exec(); | ||
15 | +} |
examples01-en/05-qt-2/prepare.sh
0 → 100755
examples01-en/06-qt-3/hello.cpp
0 → 100755
1 | +++ a/examples01-en/06-qt-3/hello.cpp | ||
1 | +#include <QApplication> | ||
2 | +#include <QPushButton> | ||
3 | +#include <QDial> | ||
4 | + | ||
5 | +int main(int argc, char *argv[]) | ||
6 | +{ | ||
7 | + QApplication app(argc, argv); | ||
8 | + | ||
9 | + QPushButton hello("Hello world!"); | ||
10 | + hello.resize(100, 30); | ||
11 | + hello.setFont(QFont("Arial", 18, QFont::Bold)); | ||
12 | + hello.setGeometry(400, 200 , 180, 40); | ||
13 | + | ||
14 | + QDial dial; | ||
15 | + dial.show(); | ||
16 | + | ||
17 | + hello.show(); | ||
18 | + return app.exec(); | ||
19 | +} |
examples01-en/06-qt-3/prepare.sh
0 → 100755
examples01-en/07-stackc/makefile
0 → 100644
1 | +++ a/examples01-en/07-stackc/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + gcc -g $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.c stack.h | ||
5 | + gcc -c -g -Wall -pedantic $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.c stack.h | ||
8 | + gcc -c -g -Wall -pedantic $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-en/07-stackc/stack.c
0 → 100644
1 | +++ a/examples01-en/07-stackc/stack.c | ||
1 | +#include <assert.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include "stack.h" | ||
5 | + | ||
6 | +void init(struct stack_handle* s) | ||
7 | +{ | ||
8 | + s->top=0; | ||
9 | +} | ||
10 | + | ||
11 | +void finalize(struct stack_handle* s) | ||
12 | +{ | ||
13 | +} | ||
14 | + | ||
15 | +void clear(struct stack_handle* s) | ||
16 | +{ | ||
17 | + s->top=0; | ||
18 | +} | ||
19 | + | ||
20 | +void push(struct stack_handle* s,int a) | ||
21 | +{ | ||
22 | + assert(s->top<STACKSIZE); | ||
23 | + s->data[s->top++]=a; | ||
24 | +} | ||
25 | + | ||
26 | +int pop(struct stack_handle* s) | ||
27 | +{ | ||
28 | + assert(s->top>0); | ||
29 | + return s->data[--s->top]; | ||
30 | +} | ||
31 | + |
examples01-en/07-stackc/stack.h
0 → 100644
1 | +++ a/examples01-en/07-stackc/stack.h | ||
1 | +#define STACKSIZE 20 | ||
2 | + | ||
3 | +struct stack_handle | ||
4 | +{ | ||
5 | +int top; /* index of first free slot on stack */ | ||
6 | +int data[STACKSIZE]; | ||
7 | +}; | ||
8 | + | ||
9 | +void push(struct stack_handle* s,int a); | ||
10 | +int pop(struct stack_handle* s); | ||
11 | +void clear(struct stack_handle* s); | ||
12 | +void init(struct stack_handle* s); | ||
13 | +void finalize(struct stack_handle* s); | ||
14 | + |
examples01-en/07-stackc/teststack.c
0 → 100644
1 | +++ a/examples01-en/07-stackc/teststack.c | ||
1 | +#include <stdio.h> | ||
2 | +#include "stack.h" | ||
3 | + | ||
4 | +int main() | ||
5 | +{ | ||
6 | + struct stack_handle s1; | ||
7 | + struct stack_handle s2; | ||
8 | + init(&s1); | ||
9 | + init(&s2); | ||
10 | + push(&s1,1); | ||
11 | + push(&s1,2); | ||
12 | + push(&s1,3); | ||
13 | + push(&s2,5); | ||
14 | + push(&s2,6); | ||
15 | + printf("%d %d\n",pop(&s1),pop(&s2)); | ||
16 | + printf("%d\n",pop(&s1)); | ||
17 | + finalize(&s1); | ||
18 | + finalize(&s2); | ||
19 | + return 0; | ||
20 | +} |
examples01-en/08-stackcpp/makefile
0 → 100644
1 | +++ a/examples01-en/08-stackcpp/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + g++ -g $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.cpp stack.h | ||
5 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.cpp stack.h | ||
8 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-en/08-stackcpp/stack.cpp
0 → 100644
1 | +++ a/examples01-en/08-stackcpp/stack.cpp | ||
1 | +#include <assert.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include "stack.h" | ||
5 | +#include <iostream> | ||
6 | +using namespace std; | ||
7 | + | ||
8 | +stack::stack() | ||
9 | +{ | ||
10 | + this->top=0; | ||
11 | + cout << "stack::stack() called\n"; | ||
12 | + | ||
13 | +} | ||
14 | + | ||
15 | +stack::~stack() | ||
16 | +{ | ||
17 | + cout << "stack::~stack() called\n"; | ||
18 | +} | ||
19 | + | ||
20 | +void stack::clear() | ||
21 | +{ | ||
22 | + this->top=0; | ||
23 | +} | ||
24 | + | ||
25 | +void stack::push(int a) | ||
26 | +{ | ||
27 | + assert(this->top<STACKSIZE); | ||
28 | + this->data[this->top++]=a; | ||
29 | +} | ||
30 | + | ||
31 | +int stack::pop() | ||
32 | +{ | ||
33 | + assert(this->top>0); | ||
34 | + return this->data[--this->top]; | ||
35 | +} | ||
36 | + |
examples01-en/08-stackcpp/stack.h
0 → 100644
examples01-en/08-stackcpp/teststack.cpp
0 → 100644
1 | +++ a/examples01-en/08-stackcpp/teststack.cpp | ||
1 | +#include <stdio.h> | ||
2 | +#include "stack.h" | ||
3 | + | ||
4 | +int main() | ||
5 | +{ | ||
6 | + stack s1; | ||
7 | + stack s2; | ||
8 | + s1.push(1); | ||
9 | + s1.push(2); | ||
10 | + s1.push(3); | ||
11 | + s2.push(5); | ||
12 | + s2.push(6); | ||
13 | + { | ||
14 | + stack s3; | ||
15 | + } | ||
16 | + printf("%d %d\n",s1.pop(),s2.pop()); | ||
17 | + printf("%d\n",s1.pop()); | ||
18 | + return 0; | ||
19 | +} |
examples01-en/09-funoverload/makefile
0 → 100644
examples01-en/09-funoverload/overload.cpp
0 → 100644
examples01-pl/01-kompilacja/compile.sh
0 → 100755
examples01-pl/01-kompilacja/compile2.sh
0 → 100755
examples01-pl/01-kompilacja/hello.c
0 → 100644
examples01-pl/02-moduly/hello.c
0 → 100644
examples01-pl/02-moduly/makefile
0 → 100644
1 | +++ a/examples01-pl/02-moduly/makefile | ||
1 | +hello: witaj.o hello.o | ||
2 | + gcc -g witaj.o hello.o -o hello -lm | ||
3 | + | ||
4 | +witaj.o: witaj.c witaj.h | ||
5 | + gcc -g -Wall -pedantic -c witaj.c -o witaj.o | ||
6 | + | ||
7 | +hello.o: hello.c witaj.h | ||
8 | + gcc -g -Wall -pedantic -c hello.c -o hello.o | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm hello.o witaj.o hello | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/02-moduly/witaj.c
0 → 100644
examples01-pl/02-moduly/witaj.h
0 → 100644
examples01-pl/03-cmdline/cmdline.c
0 → 100644
examples01-pl/03-cmdline/makefile
0 → 100644
examples01-pl/04-malloc/ala
0 → 100644
examples01-pl/04-malloc/makefile
0 → 100644
examples01-pl/04-malloc/malloc.c
0 → 100644
1 | +++ a/examples01-pl/04-malloc/malloc.c | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | + | ||
4 | +int | ||
5 | +main () | ||
6 | +{ | ||
7 | + double *a = NULL; | ||
8 | + int n; | ||
9 | + int i; | ||
10 | + FILE *infile = fopen ("ala", "r"); | ||
11 | + if (fscanf (infile, "%d\n", &n) != 1) abort(); | ||
12 | + a = (double *) malloc (n * sizeof (double)); | ||
13 | + if(a==NULL) abort(); | ||
14 | + for (i = 0; i < n; i++) | ||
15 | + if (1 != fscanf (infile, "%lf ", /*a+i */ &(a[i]))) abort(); | ||
16 | + for (i = 0; i < n; i++) | ||
17 | + printf ("%f ", a[i]); | ||
18 | + printf("\n"); | ||
19 | + free (a); | ||
20 | + fclose (infile); | ||
21 | + return 0; | ||
22 | +} |
examples01-pl/05-2darray/2darray.c
0 → 100644
1 | +++ a/examples01-pl/05-2darray/2darray.c | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | + | ||
4 | +int | ||
5 | +main () | ||
6 | +{ | ||
7 | + double **a = NULL; | ||
8 | + double **b = NULL; | ||
9 | + int rows = 3; | ||
10 | + int columns = 5; | ||
11 | + int i; | ||
12 | + a = (double **) malloc (rows * sizeof (double *)); | ||
13 | + for (i = 0; i < rows; i++) | ||
14 | + a[i] = (double *) malloc (columns * sizeof (double)); | ||
15 | + b = (double **) malloc (rows * sizeof (double *)); | ||
16 | + b[0] = (double *) malloc (rows * columns * sizeof (double)); | ||
17 | + for (i = 1; i < rows; i++) | ||
18 | + b[i] = b[i-1] + columns; | ||
19 | + /* use a and b */ | ||
20 | + for (i = 0; i < rows; i++) | ||
21 | + free (a[i]); | ||
22 | + free (a); | ||
23 | + free (b[0]); | ||
24 | + free (b); | ||
25 | + return 0; | ||
26 | +} |
examples01-pl/05-2darray/makefile
0 → 100644
examples01-pl/06-mymalloc/main.c
0 → 100644
1 | +++ a/examples01-pl/06-mymalloc/main.c | ||
1 | +#include <assert.h> | ||
2 | +#include "mymalloc.h" | ||
3 | + | ||
4 | +int main() | ||
5 | +{ | ||
6 | +void* p1; | ||
7 | +void* p2; | ||
8 | +void* p3; | ||
9 | +void* p4; | ||
10 | +void* p5; | ||
11 | + | ||
12 | +if(!mymalloc_init(10000000)) abort(); | ||
13 | +mymalloc_dump(); | ||
14 | +p1=mymalloc(100); | ||
15 | +mymalloc_dump(); | ||
16 | +p2=mymalloc(200); | ||
17 | +mymalloc_dump(); | ||
18 | +p3=mymalloc(300); | ||
19 | +mymalloc_dump(); | ||
20 | +myfree(p2); | ||
21 | +mymalloc_dump(); | ||
22 | +p2=mymalloc(150); | ||
23 | +mymalloc_dump(); | ||
24 | +p2=myrealloc(p2,170); | ||
25 | +mymalloc_dump(); | ||
26 | +p2=myrealloc(p2,300); | ||
27 | +mymalloc_dump(); | ||
28 | +p4=mymalloc(10000); | ||
29 | +mymalloc_dump(); | ||
30 | +p5=mymalloc(50000); | ||
31 | +mymalloc_dump(); | ||
32 | +myfree(p4); | ||
33 | +mymalloc_dump(); | ||
34 | +p4=mymalloc(7000); | ||
35 | +mymalloc_dump(); | ||
36 | +myfree(p2); | ||
37 | +mymalloc_dump(); | ||
38 | +myfree(p1); | ||
39 | +mymalloc_dump(); | ||
40 | +myfree(p3); | ||
41 | +mymalloc_dump(); | ||
42 | +myfree(p4); | ||
43 | +mymalloc_dump(); | ||
44 | +myfree(p5); | ||
45 | +mymalloc_dump(); | ||
46 | +return 0; | ||
47 | +} |
examples01-pl/06-mymalloc/makefile
0 → 100644
1 | +++ a/examples01-pl/06-mymalloc/makefile | ||
1 | +testmymalloc: mymalloc.o main.o | ||
2 | + gcc -g $^ -o $@ -lm | ||
3 | + | ||
4 | +mymalloc.o: mymalloc.c mymalloc.h | ||
5 | + gcc -g -Wall -c $< -o $@ | ||
6 | + | ||
7 | +main.o: main.c mymalloc.h | ||
8 | + gcc -g -Wall -c $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm main.o mymalloc.o testmymalloc | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/06-mymalloc/mymalloc.c
0 → 100644
1 | +++ a/examples01-pl/06-mymalloc/mymalloc.c | ||
1 | +#include <unistd.h> | ||
2 | +#include <assert.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include <string.h> | ||
5 | +#include "mymalloc.h" | ||
6 | + | ||
7 | +typedef struct _blkdata | ||
8 | +{ | ||
9 | + unsigned long size; /* rozmiar bloku wlaczajac naglowek */ | ||
10 | + unsigned int flags; | ||
11 | +} | ||
12 | +blkdata; | ||
13 | +#define MM_OCCUPIED 1 | ||
14 | +#define MM_LAST 2 | ||
15 | + | ||
16 | +static void *begin; | ||
17 | + | ||
18 | +int | ||
19 | +mymalloc_init (unsigned long size) | ||
20 | +{ | ||
21 | + blkdata *b; | ||
22 | + assert (size > 0); | ||
23 | + begin = sbrk (size); | ||
24 | + if (!begin) | ||
25 | + return 0; | ||
26 | + b = (blkdata *) begin; | ||
27 | + b->size = size; | ||
28 | + b->flags = MM_LAST; | ||
29 | + return 1; | ||
30 | +}; | ||
31 | + | ||
32 | +void * | ||
33 | +mymalloc (unsigned long size) | ||
34 | +{ | ||
35 | + blkdata *b = (blkdata *) begin; | ||
36 | + printf ("Request to allocate %lu bytes\n", size); | ||
37 | + while (1) | ||
38 | + { | ||
39 | + if ((b->size > size + sizeof (blkdata)) && !(b->flags & MM_OCCUPIED)) | ||
40 | + { | ||
41 | + unsigned long after = b->size - size - sizeof (blkdata); | ||
42 | + if (after > sizeof (blkdata)) | ||
43 | + { | ||
44 | + blkdata *na = | ||
45 | + (blkdata *) ((void *) b + size + sizeof (blkdata)); | ||
46 | + na->flags = 0; | ||
47 | + if (b->flags & MM_LAST) | ||
48 | + na->flags |= MM_LAST; | ||
49 | + na->size = after; | ||
50 | + b->size = size + sizeof (blkdata); | ||
51 | + b->flags = MM_OCCUPIED; | ||
52 | + return ((void *) b) + sizeof (blkdata); | ||
53 | + } | ||
54 | + else /* wez caly blok */ | ||
55 | + { | ||
56 | + b->flags |= MM_OCCUPIED; | ||
57 | + return ((void *) b) + sizeof (blkdata); | ||
58 | + } | ||
59 | + }; | ||
60 | + if (b->flags & MM_LAST) | ||
61 | + break; | ||
62 | + b = (blkdata *) ((void *) b + b->size); | ||
63 | + }; | ||
64 | + return NULL; | ||
65 | +} | ||
66 | + | ||
67 | +void | ||
68 | +myfree (void *ptr) | ||
69 | +{ | ||
70 | + blkdata *b; | ||
71 | + blkdata *nextb = NULL; | ||
72 | + blkdata *prevb = NULL; | ||
73 | + if (ptr == NULL) | ||
74 | + return; | ||
75 | + b = (blkdata *) (ptr - sizeof (blkdata)); | ||
76 | + printf ("Request to free %lu bytes at the address %p\n", b->size - sizeof (blkdata), b); | ||
77 | + if ((void *) b != begin) | ||
78 | + { | ||
79 | +/* znajdz poprzedni blok */ | ||
80 | + prevb = (blkdata *) begin; | ||
81 | + while (1) | ||
82 | + { | ||
83 | + blkdata *next; | ||
84 | + assert (!(prevb->flags & MM_LAST)); | ||
85 | + next = (blkdata *) ((void *) prevb + prevb->size); | ||
86 | + if (next == b) | ||
87 | + break; | ||
88 | + prevb = next; | ||
89 | + }; | ||
90 | + }; | ||
91 | +/* znajdz nastepny blok */ | ||
92 | + if (!(b->flags & MM_LAST)) | ||
93 | + nextb = (blkdata *) ((void *) b + b->size); | ||
94 | +/* zaznacz blok jako wolny */ | ||
95 | + b->flags &= ~MM_OCCUPIED; | ||
96 | +/* skonsoliduj z nastepnym */ | ||
97 | + if (nextb && !(nextb->flags & MM_OCCUPIED)) | ||
98 | + { | ||
99 | + if (nextb->flags & MM_LAST) | ||
100 | + b->flags = MM_LAST; | ||
101 | + b->size += nextb->size; | ||
102 | + } | ||
103 | +/* skonsoliduj z poprzednim */ | ||
104 | + if (prevb && !(prevb->flags & MM_OCCUPIED)) | ||
105 | + { | ||
106 | + if (b->flags & MM_LAST) | ||
107 | + prevb->flags |= MM_LAST; | ||
108 | + prevb->size += b->size; | ||
109 | + }; | ||
110 | +} | ||
111 | + | ||
112 | +static void * | ||
113 | +copyrealloc (void *ptr, unsigned long size, unsigned long oldsize) | ||
114 | +{ | ||
115 | + void *blk = mymalloc (size); | ||
116 | + if (blk == NULL) | ||
117 | + return NULL; | ||
118 | + memcpy (blk, ptr, oldsize); | ||
119 | + myfree (ptr); | ||
120 | + return blk; | ||
121 | +} | ||
122 | + | ||
123 | +void * | ||
124 | +myrealloc (void *ptr, unsigned long size) | ||
125 | +{ | ||
126 | + blkdata *b; | ||
127 | + if (ptr == NULL) | ||
128 | + return mymalloc (size); | ||
129 | + b = (blkdata *) (ptr - sizeof (blkdata)); | ||
130 | + printf ("Request to reallocate %lu bytes to %lu bytes, old address=%p\n", | ||
131 | + b->size - sizeof (blkdata), size, b); | ||
132 | + if (size == 0) | ||
133 | + { | ||
134 | + myfree (ptr); | ||
135 | + return NULL; | ||
136 | + }; | ||
137 | + if (size <= b->size - sizeof (blkdata)) /* zmniejszenie bloku */ | ||
138 | + { | ||
139 | + unsigned long after = b->size - size - sizeof (blkdata); | ||
140 | + blkdata *nextb = NULL; | ||
141 | + if (!(b->flags & MM_LAST)) | ||
142 | + nextb = (blkdata *) ((void *) b + b->size); | ||
143 | + blkdata *na = ((void *) b) + size + sizeof (blkdata); | ||
144 | + if (nextb && !(nextb->flags & MM_OCCUPIED)) | ||
145 | + /* skonsoliduj z nastepnym */ | ||
146 | + { | ||
147 | + unsigned int flags = nextb->flags; | ||
148 | + unsigned long size = after + nextb->size; | ||
149 | + na->flags = flags; | ||
150 | + na->size = size; | ||
151 | + b->size = size + sizeof (blkdata); | ||
152 | + } | ||
153 | + else if (after > sizeof (blkdata)) | ||
154 | + { | ||
155 | + na->flags = 0; | ||
156 | + if (b->flags & MM_LAST) | ||
157 | + na->flags |= MM_LAST; | ||
158 | + na->size = after; | ||
159 | + b->size = size + sizeof (blkdata); | ||
160 | + b->flags = MM_OCCUPIED; | ||
161 | + } | ||
162 | + return ptr; | ||
163 | + } | ||
164 | + else /* zwiekszenie bloku */ | ||
165 | + { | ||
166 | + blkdata *next = (blkdata *) ((void *) b + b->size); | ||
167 | + if (!(b->flags & MM_LAST) && !(next->flags & MM_OCCUPIED)) | ||
168 | + { | ||
169 | + unsigned long totalsize = b->size + next->size; | ||
170 | + unsigned long after = totalsize - size - sizeof (blkdata); | ||
171 | + if (size + sizeof (blkdata) > totalsize) /* brak miejsca w nastepnym bloku */ | ||
172 | + return copyrealloc (ptr, size, b->size - sizeof (blkdata)); | ||
173 | + if (next->flags & MM_LAST) | ||
174 | + b->flags |= MM_LAST; | ||
175 | + if (after > sizeof (blkdata)) | ||
176 | + { | ||
177 | + blkdata *na = | ||
178 | + (blkdata *) ((void *) b + size + sizeof (blkdata)); | ||
179 | + na->flags = 0; | ||
180 | + if (b->flags & MM_LAST) | ||
181 | + na->flags |= MM_LAST; | ||
182 | + na->size = after; | ||
183 | + b->size = size + sizeof (blkdata); | ||
184 | + b->flags = MM_OCCUPIED; | ||
185 | + } | ||
186 | + else | ||
187 | + b->size += next->size; | ||
188 | + return ptr; | ||
189 | + } | ||
190 | + else /* nastepny zajety albo biezacy ostatni */ | ||
191 | + return copyrealloc (ptr, size, b->size - sizeof (blkdata)); | ||
192 | + } | ||
193 | +} | ||
194 | + | ||
195 | +void | ||
196 | +mymalloc_dump () | ||
197 | +{ | ||
198 | + blkdata *b = (blkdata *) begin; | ||
199 | + unsigned long totalsize = 0; | ||
200 | + printf ("Mymalloc block list dump\n"); | ||
201 | + while (1) | ||
202 | + { | ||
203 | + printf ("Block at the address %p flags %c%c size %lu\n", b, | ||
204 | + (b->flags & MM_OCCUPIED) ? 'O' : 'F', | ||
205 | + (b->flags & MM_LAST) ? 'L' : ' ', b->size); | ||
206 | + totalsize += b->size; | ||
207 | + if (b->flags & MM_LAST) | ||
208 | + break; | ||
209 | + b = (blkdata *) ((void *) b + b->size); | ||
210 | + }; | ||
211 | + printf ("Total size:%lu\n", totalsize); | ||
212 | +} |
examples01-pl/06-mymalloc/mymalloc.h
0 → 100644
examples01-pl/07-badexample1/bad.c
0 → 100644
examples01-pl/07-badexample1/good.c
0 → 100644
examples01-pl/07-badexample1/makefile
0 → 100644
examples01-pl/08-badexample2/bad1.c
0 → 100644
examples01-pl/08-badexample2/bad2.c
0 → 100644
examples01-pl/08-badexample2/good.c
0 → 100644
1 | +++ a/examples01-pl/08-badexample2/good.c | ||
1 | +#include <stdlib.h> | ||
2 | +#include <stdio.h> | ||
3 | + | ||
4 | +char* itoa(int a) | ||
5 | +{ | ||
6 | + char * buf = (char*)malloc(20); | ||
7 | + snprintf(buf,20,"%d", a); | ||
8 | + return buf; | ||
9 | +} | ||
10 | + | ||
11 | +int main() | ||
12 | +{ | ||
13 | + char *w1, *w2; | ||
14 | + printf("%s %s\n",w1=itoa(17), w2=itoa(15)); | ||
15 | + free(w1); | ||
16 | + free(w2); | ||
17 | + return 0; | ||
18 | +} | ||
19 | + |
examples01-pl/08-badexample2/makefile
0 → 100644
1 | +++ a/examples01-pl/08-badexample2/makefile | ||
1 | +all: good bad1 bad2 | ||
2 | + | ||
3 | +good: good.c | ||
4 | + gcc -g -Wall $< -o $@ | ||
5 | + | ||
6 | +bad1: bad1.c | ||
7 | + gcc -g -Wall $< -o $@ | ||
8 | + | ||
9 | +bad2: bad2.c | ||
10 | + gcc -g -Wall $< -o $@ | ||
11 | + | ||
12 | +.PHONY: clean | ||
13 | + | ||
14 | +clean: | ||
15 | + -rm good bad1 bad2 | ||
0 | \ No newline at end of file | 16 | \ No newline at end of file |
examples01-pl/09-valgrind/makefile
0 → 100644
1 | +++ a/examples01-pl/09-valgrind/makefile | ||
1 | +all: testvalgrind1 testvalgrind2 test | ||
2 | + | ||
3 | +testvalgrind1: testvalgrind1.c | ||
4 | + gcc -g -Wall $< -o $@ | ||
5 | + | ||
6 | +testvalgrind2: testvalgrind2.c | ||
7 | + gcc -g -Wall $< -o $@ | ||
8 | + | ||
9 | +.PHONY: clean test | ||
10 | + | ||
11 | +clean: | ||
12 | + -rm testvalgrind1 testvalgrind2 | ||
13 | + | ||
14 | +test: | ||
15 | + valgrind -v ./testvalgrind1 | ||
16 | + valgrind -v ./testvalgrind2 | ||
0 | \ No newline at end of file | 17 | \ No newline at end of file |
examples01-pl/09-valgrind/testvalgrind1.c
0 → 100644
examples01-pl/09-valgrind/testvalgrind2.c
0 → 100644
examples01-pl/10-firstcpp/hellocpp.cpp
0 → 100644
examples01-pl/10-firstcpp/makefile
0 → 100644
examples01-pl/11-string/makefile
0 → 100644
examples01-pl/11-string/string.cpp
0 → 100644
1 | +++ a/examples01-pl/11-string/string.cpp | ||
1 | +#include <iostream> | ||
2 | +#include <string> | ||
3 | +using namespace std; | ||
4 | + | ||
5 | +string getword() | ||
6 | +{ | ||
7 | + string s; | ||
8 | + cin >> s; | ||
9 | + return s; | ||
10 | +} | ||
11 | + | ||
12 | + | ||
13 | +int main() | ||
14 | +{ | ||
15 | + cout << "Podaj slowo" << endl; | ||
16 | + cout << "Podane slowo to:" << getword() << endl; | ||
17 | +} | ||
18 | + |
examples01-pl/12-vector/makefile
0 → 100644
examples01-pl/12-vector/vector.cpp
0 → 100644
1 | +++ a/examples01-pl/12-vector/vector.cpp | ||
1 | +#include <iostream> | ||
2 | +#include <vector> | ||
3 | +#include <string> | ||
4 | +using namespace std; | ||
5 | + | ||
6 | +int main() | ||
7 | +{ | ||
8 | + string word; | ||
9 | + vector<string> v; | ||
10 | + while(cin) | ||
11 | + { | ||
12 | + if(cin>>word) | ||
13 | + v.push_back(word); | ||
14 | + } | ||
15 | + for(unsigned int i=0;i<v.size();i++) | ||
16 | + cout << v[i]<<" "; | ||
17 | + cout << endl; | ||
18 | +} | ||
19 | + |
examples01-pl/13-qt-1/hello.cpp
0 → 100755
examples01-pl/13-qt-1/prepare.sh
0 → 100755
examples01-pl/14-qt-2/hello.cpp
0 → 100755
1 | +++ a/examples01-pl/14-qt-2/hello.cpp | ||
1 | +#include <QApplication> | ||
2 | +#include <QPushButton> | ||
3 | + | ||
4 | +int main(int argc, char *argv[]) | ||
5 | +{ | ||
6 | + QApplication app(argc, argv); | ||
7 | + | ||
8 | + QPushButton hello("Hello world!"); | ||
9 | + hello.resize(100, 30); | ||
10 | + hello.setFont(QFont("Arial", 18, QFont::Bold)); | ||
11 | + hello.setGeometry(400, 200 , 180, 40); | ||
12 | + | ||
13 | + hello.show(); | ||
14 | + return app.exec(); | ||
15 | +} |
examples01-pl/14-qt-2/prepare.sh
0 → 100755
examples01-pl/15-qt-3/hello.cpp
0 → 100755
1 | +++ a/examples01-pl/15-qt-3/hello.cpp | ||
1 | +#include <QApplication> | ||
2 | +#include <QPushButton> | ||
3 | +#include <QDial> | ||
4 | + | ||
5 | +int main(int argc, char *argv[]) | ||
6 | +{ | ||
7 | + QApplication app(argc, argv); | ||
8 | + | ||
9 | + QPushButton hello("Hello world!"); | ||
10 | + hello.resize(100, 30); | ||
11 | + hello.setFont(QFont("Arial", 18, QFont::Bold)); | ||
12 | + hello.setGeometry(400, 200 , 180, 40); | ||
13 | + | ||
14 | + QDial dial; | ||
15 | + dial.show(); | ||
16 | + | ||
17 | + hello.show(); | ||
18 | + return app.exec(); | ||
19 | +} |
examples01-pl/15-qt-3/prepare.sh
0 → 100755
examples01-pl/16-stack1/makefile
0 → 100644
1 | +++ a/examples01-pl/16-stack1/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + gcc -Wall $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.c stack.h | ||
5 | + gcc -c -Wall $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.c stack.h | ||
8 | + gcc -c -Wall $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/16-stack1/stack.c
0 → 100644
1 | +++ a/examples01-pl/16-stack1/stack.c | ||
1 | +#include <assert.h> | ||
2 | +#include "stack.h" | ||
3 | + | ||
4 | +#define STACKSIZE 20 | ||
5 | +static int top; /* pierwsze wolne miejsce na stosie */ | ||
6 | +static int dane[STACKSIZE]; | ||
7 | + | ||
8 | +void init() | ||
9 | +{ | ||
10 | + top=0; | ||
11 | +} | ||
12 | + | ||
13 | +void finalize() | ||
14 | +{ | ||
15 | +} | ||
16 | + | ||
17 | +void clear() | ||
18 | +{ | ||
19 | + top=0; | ||
20 | +} | ||
21 | + | ||
22 | +void push(int a) | ||
23 | +{ | ||
24 | + assert(top<STACKSIZE); | ||
25 | + dane[top++]=a; | ||
26 | +} | ||
27 | + | ||
28 | +int pop() | ||
29 | +{ | ||
30 | + assert(top>0); | ||
31 | + return dane[--top]; | ||
32 | +} | ||
33 | + |
examples01-pl/16-stack1/stack.h
0 → 100644
examples01-pl/16-stack1/teststack.c
0 → 100644
examples01-pl/17-stack2/makefile
0 → 100644
1 | +++ a/examples01-pl/17-stack2/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + gcc -Wall $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.c stack.h | ||
5 | + gcc -c -Wall $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.c stack.h | ||
8 | + gcc -c -Wall $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/17-stack2/stack.c
0 → 100644
1 | +++ a/examples01-pl/17-stack2/stack.c | ||
1 | +#include <assert.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include "stack.h" | ||
5 | + | ||
6 | +static int top; /* pierwsze wolne miejsce na stosie */ | ||
7 | +static int *dane; | ||
8 | +static int size; | ||
9 | + | ||
10 | +void init() | ||
11 | +{ | ||
12 | + top=0; | ||
13 | + size=0; | ||
14 | + dane=0; | ||
15 | +} | ||
16 | + | ||
17 | +void finalize() | ||
18 | +{ | ||
19 | + free(dane); | ||
20 | +} | ||
21 | + | ||
22 | +void clear() | ||
23 | +{ | ||
24 | + top=0; | ||
25 | +} | ||
26 | + | ||
27 | +void push(int a) | ||
28 | +{ | ||
29 | + if(top>=size) | ||
30 | + { | ||
31 | + int newsize=(size+1)*2; | ||
32 | + int* ndane=(int*)realloc(dane,newsize*sizeof(int)); | ||
33 | + if(ndane) | ||
34 | + dane=ndane; | ||
35 | + else | ||
36 | + { | ||
37 | + free(dane); | ||
38 | + abort(); | ||
39 | + } | ||
40 | + fprintf(stderr,"Rozmiar stosu %d -> %d\n",size,newsize); | ||
41 | + size=newsize; | ||
42 | + } | ||
43 | + dane[top++]=a; | ||
44 | +} | ||
45 | + | ||
46 | +int pop() | ||
47 | +{ | ||
48 | + assert(top>0); | ||
49 | + return dane[--top]; | ||
50 | +} | ||
51 | + |
examples01-pl/17-stack2/stack.h
0 → 100644
examples01-pl/17-stack2/teststack.c
0 → 100644
examples01-pl/18-stack3/makefile
0 → 100644
1 | +++ a/examples01-pl/18-stack3/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + gcc -Wall $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.c stack.h | ||
5 | + gcc -c -Wall $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.c stack.h | ||
8 | + gcc -c -Wall $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/18-stack3/stack.c
0 → 100644
1 | +++ a/examples01-pl/18-stack3/stack.c | ||
1 | +#include <assert.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include "stack.h" | ||
5 | + | ||
6 | +#define STACKSIZE 20 | ||
7 | + | ||
8 | +struct stack_handle | ||
9 | +{ | ||
10 | +int top; /* pierwsze wolne miejsce na stosie */ | ||
11 | +int dane[STACKSIZE]; | ||
12 | +} | ||
13 | + | ||
14 | + | ||
15 | +struct stack_handle* init() | ||
16 | +{ | ||
17 | + struct stack_handle* s=(struct stack_handle*)malloc(sizeof(struct stack_handle)); | ||
18 | + s->top=0; | ||
19 | + return s; | ||
20 | +} | ||
21 | + | ||
22 | +void finalize(struct stack_handle* s) | ||
23 | +{ | ||
24 | + free(s); | ||
25 | +} | ||
26 | + | ||
27 | +void clear(struct stack_handle* s) | ||
28 | +{ | ||
29 | + s->top=0; | ||
30 | +} | ||
31 | + | ||
32 | +void push(struct stack_handle* s,int a) | ||
33 | +{ | ||
34 | + assert(s->top<STACKSIZE); | ||
35 | + s->dane[s->top++]=a; | ||
36 | +} | ||
37 | + | ||
38 | +int pop(struct stack_handle* s) | ||
39 | +{ | ||
40 | + assert(s->top>0); | ||
41 | + return s->dane[--s->top]; | ||
42 | +} | ||
43 | + |
examples01-pl/18-stack3/stack.h
0 → 100644
examples01-pl/18-stack3/teststack.c
0 → 100644
1 | +++ a/examples01-pl/18-stack3/teststack.c | ||
1 | +#include <stdio.h> | ||
2 | +#include "stack.h" | ||
3 | + | ||
4 | +int main() | ||
5 | +{ | ||
6 | + struct stack_handle * s1=init(); | ||
7 | + struct stack_handle * s2=init(); | ||
8 | + push(s1,1); | ||
9 | + push(s1,2); | ||
10 | + push(s1,3); | ||
11 | + push(s2,5); | ||
12 | + push(s2,6); | ||
13 | + printf("%d %d\n",pop(s1),pop(s2)); | ||
14 | + printf("%d\n",pop(s1)); | ||
15 | + finalize(s1); | ||
16 | + finalize(s2); | ||
17 | + return 0; | ||
18 | +} |
examples01-pl/19-stack4/makefile
0 → 100644
1 | +++ a/examples01-pl/19-stack4/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + gcc -Wall $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.c stack.h | ||
5 | + gcc -c -Wall $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.c stack.h | ||
8 | + gcc -c -Wall $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/19-stack4/stack.c
0 → 100644
1 | +++ a/examples01-pl/19-stack4/stack.c | ||
1 | +#include <assert.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include "stack.h" | ||
5 | + | ||
6 | +void init(struct stack_handle* s) | ||
7 | +{ | ||
8 | + s->top=0; | ||
9 | +} | ||
10 | + | ||
11 | +void finalize(struct stack_handle* s) | ||
12 | +{ | ||
13 | +} | ||
14 | + | ||
15 | +void clear(struct stack_handle* s) | ||
16 | +{ | ||
17 | + s->top=0; | ||
18 | +} | ||
19 | + | ||
20 | +void push(struct stack_handle* s,int a) | ||
21 | +{ | ||
22 | + assert(s->top<STACKSIZE); | ||
23 | + s->dane[s->top++]=a; | ||
24 | +} | ||
25 | + | ||
26 | +int pop(struct stack_handle* s) | ||
27 | +{ | ||
28 | + assert(s->top>0); | ||
29 | + return s->dane[--s->top]; | ||
30 | +} | ||
31 | + |
examples01-pl/19-stack4/stack.h
0 → 100644
1 | +++ a/examples01-pl/19-stack4/stack.h | ||
1 | +#define STACKSIZE 20 | ||
2 | + | ||
3 | +struct stack_handle | ||
4 | +{ | ||
5 | +int top; /* pierwsze wolne miejsce na stosie */ | ||
6 | +int dane[STACKSIZE]; | ||
7 | +}; | ||
8 | + | ||
9 | +void push(struct stack_handle* s,int a); | ||
10 | +int pop(struct stack_handle* s); | ||
11 | +void clear(struct stack_handle* s); | ||
12 | +void init(struct stack_handle* s); | ||
13 | +void finalize(struct stack_handle* s); | ||
14 | + |
examples01-pl/19-stack4/teststack.c
0 → 100644
1 | +++ a/examples01-pl/19-stack4/teststack.c | ||
1 | +#include <stdio.h> | ||
2 | +#include "stack.h" | ||
3 | + | ||
4 | +int main() | ||
5 | +{ | ||
6 | + struct stack_handle s1; | ||
7 | + struct stack_handle s2; | ||
8 | + init(&s1); | ||
9 | + init(&s2); | ||
10 | + push(&s1,1); | ||
11 | + push(&s1,2); | ||
12 | + push(&s1,3); | ||
13 | + push(&s2,5); | ||
14 | + push(&s2,6); | ||
15 | + printf("%d %d\n",pop(&s1),pop(&s2)); | ||
16 | + printf("%d\n",pop(&s1)); | ||
17 | + finalize(&s1); | ||
18 | + finalize(&s2); | ||
19 | + return 0; | ||
20 | +} |
examples01-pl/20-stack5/makefile
0 → 100644
1 | +++ a/examples01-pl/20-stack5/makefile | ||
1 | +teststack: teststack.o stack.o | ||
2 | + g++ -g -Wall $^ -o $@ | ||
3 | + | ||
4 | +stack.o: stack.cpp stack.h | ||
5 | + g++ -g -c -Wall $< -o $@ | ||
6 | + | ||
7 | +teststack.o: teststack.cpp stack.h | ||
8 | + g++ -g -c -Wall $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm stack.o teststack.o teststack | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples01-pl/20-stack5/stack.cpp
0 → 100644
1 | +++ a/examples01-pl/20-stack5/stack.cpp | ||
1 | +#include <assert.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <stdio.h> | ||
4 | +#include "stack.h" | ||
5 | + | ||
6 | +stack::stack() | ||
7 | +{ | ||
8 | + this->top=0; | ||
9 | +} | ||
10 | + | ||
11 | +stack::~stack() | ||
12 | +{ | ||
13 | +} | ||
14 | + | ||
15 | +void stack::clear() | ||
16 | +{ | ||
17 | + this->top=0; | ||
18 | +} | ||
19 | + | ||
20 | +void stack::push(int a) | ||
21 | +{ | ||
22 | + assert(this->top<STACKSIZE); | ||
23 | + this->dane[this->top++]=a; | ||
24 | +} | ||
25 | + | ||
26 | +int stack::pop() | ||
27 | +{ | ||
28 | + assert(this->top>0); | ||
29 | + return this->dane[--this->top]; | ||
30 | +} | ||
31 | + |
examples01-pl/20-stack5/stack.h
0 → 100644
examples01-pl/20-stack5/teststack.cpp
0 → 100644
1 | +++ a/examples01-pl/20-stack5/teststack.cpp | ||
1 | +#include <stdio.h> | ||
2 | +#include "stack.h" | ||
3 | + | ||
4 | +int main() | ||
5 | +{ | ||
6 | + stack s1; | ||
7 | + stack s2; | ||
8 | + s1.push(1); | ||
9 | + s1.push(2); | ||
10 | + s1.push(3); | ||
11 | + s2.push(5); | ||
12 | + s2.push(6); | ||
13 | + printf("%d %d\n",s1.pop(),s2.pop()); | ||
14 | + printf("%d\n",s1.pop()); | ||
15 | + return 0; | ||
16 | +} |
examples03/00-funptr/funptr.c
0 → 100644
1 | +++ a/examples03/00-funptr/funptr.c | ||
1 | +#include <stdio.h> | ||
2 | + | ||
3 | +int * | ||
4 | +f1 (int *a, const int *b) | ||
5 | +{ | ||
6 | + *a += *b; | ||
7 | + return a; | ||
8 | +} | ||
9 | + | ||
10 | +int * | ||
11 | +f2 (int *a, const int *b) | ||
12 | +{ | ||
13 | + *a -= *b; | ||
14 | + return a; | ||
15 | +} | ||
16 | + | ||
17 | +int *(*fun (int s)) (int *, const int *) | ||
18 | +{ | ||
19 | + if (s == 1) | ||
20 | + return f1; | ||
21 | + else | ||
22 | + return f2; | ||
23 | +} | ||
24 | + | ||
25 | +int | ||
26 | +main () | ||
27 | +{ | ||
28 | + int *(*(*fp2) (int)) (int *, const int *) = fun; | ||
29 | + | ||
30 | + int a = 15; | ||
31 | + int b = 32; | ||
32 | + fp2 (1) (&a, &b); | ||
33 | + fp2 (2) (&b, &a); | ||
34 | + printf ("%d %d\n", a, b); | ||
35 | + return 0; | ||
36 | +} |
examples03/00-funptr/makefile
0 → 100644
examples03/01-list/list.cpp
0 → 100644
1 | +++ a/examples03/01-list/list.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | +list::list() | ||
6 | +{ | ||
7 | + head = NULL; | ||
8 | + current = NULL; | ||
9 | +} | ||
10 | + | ||
11 | +list::~list() | ||
12 | +{ | ||
13 | + while(head) | ||
14 | + { | ||
15 | + node* t=head->next; | ||
16 | + delete head; | ||
17 | + head=t; | ||
18 | + }; | ||
19 | +} | ||
20 | + | ||
21 | +void list::insert(int a) | ||
22 | +{ | ||
23 | + node* t=new node; | ||
24 | + t->next=head; | ||
25 | + head = t; | ||
26 | + head->val = a; | ||
27 | +} | ||
28 | + | ||
29 | +void list::goToHead() | ||
30 | +{ | ||
31 | + current=head; | ||
32 | +} | ||
33 | + | ||
34 | +int list::getCurrentData() | ||
35 | +{ | ||
36 | + return current->val; | ||
37 | +} | ||
38 | + | ||
39 | +void list::advance() | ||
40 | +{ | ||
41 | + current=current->next; | ||
42 | +} | ||
43 | + | ||
44 | +bool list::moreData() | ||
45 | +{ | ||
46 | + if(current) | ||
47 | + return true; | ||
48 | + else | ||
49 | + return false; | ||
50 | +} |
examples03/01-list/list.h
0 → 100644
1 | +++ a/examples03/01-list/list.h | ||
1 | +class list | ||
2 | +{ | ||
3 | +private: | ||
4 | + struct node | ||
5 | + { | ||
6 | + node *next; | ||
7 | + int val; | ||
8 | + }; | ||
9 | + node * head; | ||
10 | + node *current; | ||
11 | +public: | ||
12 | + list (); | ||
13 | + ~list (); | ||
14 | + void insert (int a); | ||
15 | + void goToHead (); | ||
16 | + int getCurrentData (); | ||
17 | + void advance (); | ||
18 | + bool moreData (); | ||
19 | +}; |
examples03/01-list/makefile
0 → 100644
1 | +++ a/examples03/01-list/makefile | ||
1 | +testlist: testlist.o list.o | ||
2 | + g++ -g $^ -o $@ | ||
3 | + | ||
4 | +list.o: list.cpp list.h | ||
5 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
6 | + | ||
7 | +testlist.o: testlist.cpp list.h | ||
8 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm list.o testlist.o testlist | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples03/01-list/testlist.cpp
0 → 100644
1 | +++ a/examples03/01-list/testlist.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | +int main() | ||
6 | +{ | ||
7 | + list l; | ||
8 | + l.insert(1); | ||
9 | + l.insert(2); | ||
10 | + l.insert(3); | ||
11 | + l.goToHead(); | ||
12 | + while(l.moreData()) | ||
13 | + { | ||
14 | + int val; | ||
15 | + val=l.getCurrentData(); | ||
16 | + cout << val << " "; | ||
17 | + l.advance(); | ||
18 | + } | ||
19 | + cout << endl; | ||
20 | +} |
examples03/02-list/list.cpp
0 → 100644
1 | +++ a/examples03/02-list/list.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | +list::list() | ||
6 | +{ | ||
7 | + head = NULL; | ||
8 | + current = NULL; | ||
9 | +} | ||
10 | + | ||
11 | +list::~list() | ||
12 | +{ | ||
13 | + while(head) | ||
14 | + { | ||
15 | + node* t=head->next; | ||
16 | + delete head; | ||
17 | + head=t; | ||
18 | + }; | ||
19 | +} | ||
20 | + | ||
21 | +void list::insert(int a) | ||
22 | +{ | ||
23 | + node* t=new node; | ||
24 | + t->next=head; | ||
25 | + head = t; | ||
26 | + head->val = a; | ||
27 | +} | ||
28 | + | ||
29 | +void list::goToHead() | ||
30 | +{ | ||
31 | + current=head; | ||
32 | +} | ||
33 | + | ||
34 | +int list::getCurrentData() | ||
35 | +{ | ||
36 | + return current->val; | ||
37 | +} | ||
38 | + | ||
39 | +void list::advance() | ||
40 | +{ | ||
41 | + current=current->next; | ||
42 | +} | ||
43 | + | ||
44 | +bool list::moreData() | ||
45 | +{ | ||
46 | + if(current) | ||
47 | + return true; | ||
48 | + else | ||
49 | + return false; | ||
50 | +} |
examples03/02-list/list.h
0 → 100644
1 | +++ a/examples03/02-list/list.h | ||
1 | +class list | ||
2 | +{ | ||
3 | +private: | ||
4 | + struct node | ||
5 | + { | ||
6 | + node *next; | ||
7 | + int val; | ||
8 | + }; | ||
9 | + node * head; | ||
10 | + node *current; | ||
11 | +public: | ||
12 | + list (); | ||
13 | + ~list (); | ||
14 | + void insert (int a); | ||
15 | + void goToHead (); | ||
16 | + int getCurrentData (); | ||
17 | + void advance (); | ||
18 | + bool moreData (); | ||
19 | +}; |
examples03/02-list/makefile
0 → 100644
1 | +++ a/examples03/02-list/makefile | ||
1 | +testlist: testlist.o list.o | ||
2 | + g++ -g $^ -o $@ | ||
3 | + | ||
4 | +list.o: list.cpp list.h | ||
5 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
6 | + | ||
7 | +testlist.o: testlist.cpp list.h | ||
8 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm list.o testlist.o testlist | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples03/02-list/testlist.cpp
0 → 100644
1 | +++ a/examples03/02-list/testlist.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | + | ||
6 | +void | ||
7 | +PrintList (list & toPrint, ostream & Out) | ||
8 | +{ | ||
9 | + int nextValue; | ||
10 | + Out << "Printing list contents: " << endl; | ||
11 | + toPrint.goToHead (); | ||
12 | + if (!toPrint.moreData ()) | ||
13 | + { | ||
14 | + Out << "List is empty" << endl; | ||
15 | + return; | ||
16 | + } | ||
17 | + while (toPrint.moreData ()) | ||
18 | + { | ||
19 | + nextValue = toPrint.getCurrentData (); | ||
20 | + Out << nextValue << " "; | ||
21 | + toPrint.advance (); | ||
22 | + } | ||
23 | + Out << endl; | ||
24 | +} | ||
25 | + | ||
26 | +int | ||
27 | +main () | ||
28 | +{ | ||
29 | + list l; | ||
30 | + l.insert (1); | ||
31 | + l.insert (2); | ||
32 | + l.insert (3); | ||
33 | + PrintList (l, cout); | ||
34 | +} |
examples03/03-list/list.cpp
0 → 100644
1 | +++ a/examples03/03-list/list.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | +list::list() | ||
6 | +{ | ||
7 | + head = NULL; | ||
8 | + current = NULL; | ||
9 | +} | ||
10 | + | ||
11 | +list::~list() | ||
12 | +{ | ||
13 | + while(head) | ||
14 | + { | ||
15 | + node* t=head->next; | ||
16 | + delete head; | ||
17 | + head=t; | ||
18 | + }; | ||
19 | +} | ||
20 | + | ||
21 | +void list::insert(int a) | ||
22 | +{ | ||
23 | + node* t=new node; | ||
24 | + t->next=head; | ||
25 | + head = t; | ||
26 | + head->val = a; | ||
27 | +} | ||
28 | + | ||
29 | +void list::goToHead() | ||
30 | +{ | ||
31 | + current=head; | ||
32 | +} | ||
33 | + | ||
34 | +int list::getCurrentData() | ||
35 | +{ | ||
36 | + return current->val; | ||
37 | +} | ||
38 | + | ||
39 | +void list::advance() | ||
40 | +{ | ||
41 | + current=current->next; | ||
42 | +} | ||
43 | + | ||
44 | +bool list::moreData() | ||
45 | +{ | ||
46 | + if(current) | ||
47 | + return true; | ||
48 | + else | ||
49 | + return false; | ||
50 | +} |
examples03/03-list/list.h
0 → 100644
1 | +++ a/examples03/03-list/list.h | ||
1 | +class list | ||
2 | +{ | ||
3 | +private: | ||
4 | + struct node | ||
5 | + { | ||
6 | + node *next; | ||
7 | + int val; | ||
8 | + }; | ||
9 | + node * head; | ||
10 | + node *current; | ||
11 | +public: | ||
12 | + list (); | ||
13 | + ~list (); | ||
14 | + void insert (int a); | ||
15 | + void goToHead (); | ||
16 | + int getCurrentData (); | ||
17 | + void advance (); | ||
18 | + bool moreData (); | ||
19 | +}; |
examples03/03-list/makefile
0 → 100644
1 | +++ a/examples03/03-list/makefile | ||
1 | +testlist: testlist.o list.o | ||
2 | + g++ -g $^ -o $@ | ||
3 | + | ||
4 | +list.o: list.cpp list.h | ||
5 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
6 | + | ||
7 | +testlist.o: testlist.cpp list.h | ||
8 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm list.o testlist.o testlist | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples03/03-list/testlist.cpp
0 → 100644
1 | +++ a/examples03/03-list/testlist.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | + | ||
6 | +void | ||
7 | +PrintList (list toPrint, ostream & Out) | ||
8 | +{ | ||
9 | + int nextValue; | ||
10 | + Out << "Printing list contents: " << endl; | ||
11 | + toPrint.goToHead (); | ||
12 | + if (!toPrint.moreData ()) | ||
13 | + { | ||
14 | + Out << "List is empty" << endl; | ||
15 | + return; | ||
16 | + } | ||
17 | + while (toPrint.moreData ()) | ||
18 | + { | ||
19 | + nextValue = toPrint.getCurrentData (); | ||
20 | + Out << nextValue << " "; | ||
21 | + toPrint.advance (); | ||
22 | + } | ||
23 | + Out << endl; | ||
24 | +} | ||
25 | + | ||
26 | +int | ||
27 | +main () | ||
28 | +{ | ||
29 | + list l; | ||
30 | + l.insert (1); | ||
31 | + l.insert (2); | ||
32 | + l.insert (3); | ||
33 | + PrintList (l, cout); | ||
34 | + cout << endl; | ||
35 | +} |
examples03/04-list/list.cpp
0 → 100644
1 | +++ a/examples03/04-list/list.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | +list::list () | ||
6 | +{ | ||
7 | + head = NULL; | ||
8 | + current = NULL; | ||
9 | +} | ||
10 | + | ||
11 | +list::~list () | ||
12 | +{ | ||
13 | + while (head) | ||
14 | + { | ||
15 | + node *t = head->next; | ||
16 | + delete head; | ||
17 | + head = t; | ||
18 | + }; | ||
19 | +} | ||
20 | + | ||
21 | +void | ||
22 | +list::insert (int a) | ||
23 | +{ | ||
24 | + node *t = new node; | ||
25 | + t->next = head; | ||
26 | + head = t; | ||
27 | + head->val = a; | ||
28 | +} | ||
29 | + | ||
30 | +void | ||
31 | +list::goToHead () | ||
32 | +{ | ||
33 | + current = head; | ||
34 | +} | ||
35 | + | ||
36 | +int | ||
37 | +list::getCurrentData () | ||
38 | +{ | ||
39 | + return current->val; | ||
40 | +} | ||
41 | + | ||
42 | +void | ||
43 | +list::advance () | ||
44 | +{ | ||
45 | + current = current->next; | ||
46 | +} | ||
47 | + | ||
48 | +bool | ||
49 | +list::moreData () | ||
50 | +{ | ||
51 | + if (current) | ||
52 | + return true; | ||
53 | + else | ||
54 | + return false; | ||
55 | +} | ||
56 | + | ||
57 | +list::list (const list & l) | ||
58 | +{ | ||
59 | + current=NULL; | ||
60 | + node *src, **dst; | ||
61 | + head = NULL; | ||
62 | + src = l.head; | ||
63 | + dst = &head; | ||
64 | + while (src) | ||
65 | + { | ||
66 | + *dst = new node; | ||
67 | + (*dst)->val = src->val; | ||
68 | + (*dst)->next = NULL; | ||
69 | + if(src==l.current) | ||
70 | + current=*dst; | ||
71 | + src = src->next; | ||
72 | + dst = &((*dst)->next); | ||
73 | + } | ||
74 | +} | ||
75 | + | ||
76 | +list & list::operator= (const list & l) | ||
77 | +{ | ||
78 | + if (&l == this) | ||
79 | + return *this; | ||
80 | + current=NULL; | ||
81 | + while (head) | ||
82 | + { | ||
83 | + node *t = head->next; | ||
84 | + delete head; | ||
85 | + head = t; | ||
86 | + }; | ||
87 | + node *src, **dst; | ||
88 | + head = NULL; | ||
89 | + src = l.head; | ||
90 | + dst = &head; | ||
91 | + while (src) | ||
92 | + { | ||
93 | + *dst = new node; | ||
94 | + (*dst)->val = src->val; | ||
95 | + (*dst)->next = NULL; | ||
96 | + if(src==l.current) | ||
97 | + current=*dst; | ||
98 | + src = src->next; | ||
99 | + dst = &((*dst)->next); | ||
100 | + } | ||
101 | + return *this; | ||
102 | +} |
examples03/04-list/list.h
0 → 100644
1 | +++ a/examples03/04-list/list.h | ||
1 | +class list | ||
2 | +{ | ||
3 | +private: | ||
4 | + struct node | ||
5 | + { | ||
6 | + node *next; | ||
7 | + int val; | ||
8 | + }; | ||
9 | + node * head; | ||
10 | + node *current; | ||
11 | +public: | ||
12 | + list (); | ||
13 | + list (const list& l); | ||
14 | + list& operator=(const list& l); | ||
15 | + ~list (); | ||
16 | + void insert (int a); | ||
17 | + void goToHead (); | ||
18 | + int getCurrentData (); | ||
19 | + void advance (); | ||
20 | + bool moreData (); | ||
21 | +}; |
examples03/04-list/makefile
0 → 100644
1 | +++ a/examples03/04-list/makefile | ||
1 | +testlist: testlist.o list.o | ||
2 | + g++ -g $^ -o $@ | ||
3 | + | ||
4 | +list.o: list.cpp list.h | ||
5 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
6 | + | ||
7 | +testlist.o: testlist.cpp list.h | ||
8 | + g++ -g -c -Wall -pedantic $< -o $@ | ||
9 | + | ||
10 | +.PHONY: clean | ||
11 | + | ||
12 | +clean: | ||
13 | + -rm list.o testlist.o testlist | ||
0 | \ No newline at end of file | 14 | \ No newline at end of file |
examples03/04-list/testlist.cpp
0 → 100644
1 | +++ a/examples03/04-list/testlist.cpp | ||
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | +#include "list.h" | ||
4 | + | ||
5 | + | ||
6 | +void | ||
7 | +PrintList (list toPrint, ostream & Out) | ||
8 | +{ | ||
9 | + int nextValue; | ||
10 | + Out << "Printing list contents: " << endl; | ||
11 | + toPrint.goToHead (); | ||
12 | + if (!toPrint.moreData ()) | ||
13 | + { | ||
14 | + Out << "List is empty" << endl; | ||
15 | + return; | ||
16 | + } | ||
17 | + while (toPrint.moreData ()) | ||
18 | + { | ||
19 | + nextValue = toPrint.getCurrentData (); | ||
20 | + Out << nextValue << " "; | ||
21 | + toPrint.advance (); | ||
22 | + } | ||
23 | + Out << endl; | ||
24 | +} | ||
25 | + | ||
26 | +int | ||
27 | +main () | ||
28 | +{ | ||
29 | + list l; | ||
30 | + l.insert (1); | ||
31 | + l.insert (2); | ||
32 | + l.insert (3); | ||
33 | + PrintList (l, cout); | ||
34 | + list l2; | ||
35 | + l2=l; | ||
36 | + PrintList (l2,cout); | ||
37 | +} |