Skip to content

Commit 047ca4e

Browse files
committed
add array stuff and stack stuff
organize more
1 parent 54d3375 commit 047ca4e

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

c/array_pointers.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#define NUM_ELEMENTS 5 // http://stackoverflow.com/a/13645995/511710
3+
4+
// http://en.wikipedia.org/wiki/C_%28programming_language%29#Array.E2.80.93pointer_interchangeability
5+
6+
int i;
7+
8+
int arr[NUM_ELEMENTS];
9+
10+
char* arr_addr = arr;
11+
12+
int main() {
13+
14+
for ( i; i < NUM_ELEMENTS; i++) {
15+
16+
arr[i] = i + 20;
17+
18+
printf("arr[%d] val: %d \n", i , arr[i]);
19+
// http://boredzo.org/pointers/
20+
21+
printf("*(arr + i): %d \n\n", *(arr_addr + i * sizeof(int)));
22+
}
23+
24+
return 0;
25+
}
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var assert = require('assert');
2+
3+
function Stack() {
4+
this.items = [];
5+
this.stack_top_index = 0;
6+
this.push = function (thing) {
7+
this.stack_top_index++;
8+
this.items[this.stack_top_index] = thing;
9+
}
10+
11+
this.pop = function (thing) {
12+
if(this.stack_top_index > 0){
13+
var top_item = this.items[this.stack_top_index];
14+
this.stack_top_index--;
15+
return top_item;
16+
} else{
17+
throw Error("stack empty");
18+
}
19+
}
20+
}
21+
22+
var stack = new Stack();
23+
24+
stack.push(42);
25+
26+
27+
assert(stack.stack_top_index == 1 , "stack has one item on it");
28+
29+
var num = stack.pop();
30+
31+
assert(num == 42 , "correct number is returned from stack");
32+
assert(stack.stack_top_index === 0 , "zero items on stack");

0 commit comments

Comments
 (0)