Declaring a struct:
typedef struct {
int x;
int y;
} point;
Declaring a variable and accessing members:
point first;
first.x = 1;
first.y = 4;
printf("(%d, %d) \n", first.x, first.y);
|
lower (Ω)
|
upper (O)
|
|
|
insertion into a hash table with separate chaining
|
1
|
1
|
|
insertion into a trie
|
1
|
1
|
|
insertion into a sorted linked list
|
1
|
n
|
|
deletion from a sorted linked list
|
1
|
n
|
|
deletion from an unsorted linked list
|
1
|
n
|
**Hashtable:**
typedef struct _node
{
char word[50]; // 50-char word
struct _node *next;
}
node;
**Tree:**
typedef struct _tree3 {
bool valid; // exists or not
struct _tree3 *child1;
struct _tree3 *child2;
struct _tree3 *child3;
}
tree3;
**Trie:**
typedef struct _btrie {
bool valid;
struct _btrie *children[2];
}
btrie;
**Pop:**
int pop(void)
{
if (stack.size == 0)
return -1;
return stack.numbers[--stack.size];
}
**Push:**
bool push(int n)
{
if (stack.size == CAPACITY || n < 0)
return false;
stack.numbers[stack.size++] = n;
return true;
}
Declaration and initialization:
int a = 14;
int b = 15;
int * iPtr;
iPtr = &a;
int * anotherPtr = &b;
Accessing pointers and values:
// assign an address to another pointer
anotherPtr = iPtr;
// change the value stored in the memory
// location being pointed to
*iPtr = 3;
// print the address held be a pointer
printf("%x \n", iPtr);
// print the value being pointed to
printf("%d \n", *iPtr);
Declaring a FILE pointer:
FILE * inputFile;
FILE * outputFile;
Opening a file:
inputFile = fopen("file1.txt", "r");
outputFile = fopen("file2.txt", "w");
Input / Output:
fscanf(inputFile, "%d", &x);
fprintf(outputFile, "%f \n", 3.14);
Closing a file:
fclose(inputFile);
fclose(outputFile);
|
increment, decrement
|
++, --
|
|
multiply, divide, modulus
|
*, /, %
|
|
add, subtract
|
+, -
|
|
relational comparisons
|
>, >=, <, <=
|
|
equality comparisons
|
==, !=
|
|
and
|
&&
|
|
or
|
||
|
|
assignment
|
=, +=, -=, *=, /=, %=
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.