Line 86: | Line 86: | ||
head = List.insert(head, 2012); | head = List.insert(head, 2012); | ||
+ | Class Notes: James Chen | ||
+ | three ways to allocate memory | ||
+ | 1. -> know the size when writing the program; | ||
+ | int arr[size]; | ||
+ | vector v[size]; | ||
+ | 2. -> know the size somewhere during execution | ||
+ | int length; | ||
+ | scanf("%d", &length); | ||
+ | int *arr; | ||
+ | arr = malloc(sizeof(int)*length); | ||
+ | |||
+ | 3. -> grow and shrink -- dynamic structure | ||
+ | link listed | ||
+ | binary tree | ||
+ | |||
+ | typedef struct dstructure //treenode, listnode | ||
+ | { | ||
+ | /*data*/ | ||
+ | int value; | ||
+ | Vector vect; | ||
+ | Person *p | ||
+ | /*link*/ | ||
+ | /*link list*/ | ||
+ | struct dstructure *next; //same name as in header (dstructure) | ||
+ | /*binary tree*/ | ||
+ | struct dstructure *left; //see dstructure | ||
+ | struct dstructure *right;//see dstructure | ||
+ | }Node; | ||
+ | // name of structure is Node, dstructure relates to its organization, name is assigned | ||
+ | links are pointers, needed to store address because the structure has not ended yet | ||
+ | // can link the list so only one value is needed to store the entire array | ||
+ | |||
+ | //how to create linked nodes: | ||
+ | //create original node | ||
+ | n = malloc(sizeof(Node)); | ||
+ | n->value = 264; | ||
+ | n->next = NULL; | ||
+ | //create new node, link to existing node | ||
+ | Node *n2; | ||
+ | n2 = malloc(sizeof(Node)); | ||
+ | n2->value = 2012; | ||
+ | n2->next = NULL; | ||
+ | n->next = n2; | ||
+ | |||
+ | //constructor example | ||
+ | Node *Node_construct(int v) | ||
+ | { | ||
+ | Node *n; | ||
+ | n = malloc(sizeof(Node)); | ||
+ | n->value = v; | ||
+ | n->next = NULL; | ||
+ | return n; | ||
+ | } | ||
+ | Node *List_insert(Node *insert, int v) //starting (original) node | ||
+ | { | ||
+ | Node *n = Node_construct(v); | ||
+ | n->next = head; | ||
+ | return n; | ||
+ | }// program creates from the end of the list | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | head=List_insert(head,264); | ||
+ | head=List_insert(head,2012); | ||
+ | return(0); | ||
+ | } | ||
[[ 2012 Spring ECE 264 Lu|Back to 2012 Spring ECE 264 Lu]] | [[ 2012 Spring ECE 264 Lu|Back to 2012 Spring ECE 264 Lu]] |
Revision as of 08:57, 22 March 2012
Lecture 19
Put your content here . . .
three ways to allocate memory:
1. static---->know the size when writing the program
eg. int arr[100];
vector v[200];
2. know the size somewhere during execution
eg. int length;
scanf("%d", &length);
int *arr;
arr = malloc(sizeof(int)*length);
3.grow and shrink based on ran-time needs(dynamic structures)
eg. linked list, binary tree(list mode, tree mode)
type of struct dstructure { int value; vector vec; Person *p;
struct dstructure *next;(linked list) struct dstructure * left;(binary tree) struct dstructure * right;(binary tree) } Node
streaming data
Node *n; n = malloc(sizeof(Node));
typeof struct listnode { int value; struct listnode *next; }Node;<---(don't forget)
(next few lectures, focus on linklist only, and data type would just be integer)
Node *n= NULL; n = malloc(sizeof(Node)); n->value = 264; Node *n2; n2 = malloc(sizeof(Node)); n2->value = 2012; n2->next = NULL; n->next = n2;
(abstract view below) assign one's value to another's link to link them together
Node *n3; n3 = n; printf("%d",n3->value);(=264) n3 = n3->next; printf("%d", n3->value);(=2012)
Node *Node.construct(int v) { Node *n; n = mallloc(sizeof(Node)); n->value = v; n->next = NULL;(<----don't forget) return n; }
Node *List_insert(Node *t, int v) { Node *n = Node.construct(v); n->next = t; return n; }
Node *head = NULL; head = List.insert(head, 264); head = List.insert(head, 2012);
Node *head = NULL; head = List.insert(head, 264); head = List.insert(head, 2012);
Class Notes: James Chen three ways to allocate memory 1. -> know the size when writing the program;
int arr[size]; vector v[size];
2. -> know the size somewhere during execution
int length; scanf("%d", &length); int *arr; arr = malloc(sizeof(int)*length);
3. -> grow and shrink -- dynamic structure
link listed binary tree
typedef struct dstructure //treenode, listnode { /*data*/
int value; Vector vect; Person *p
/*link*/
/*link list*/ struct dstructure *next; //same name as in header (dstructure) /*binary tree*/ struct dstructure *left; //see dstructure struct dstructure *right;//see dstructure
}Node; // name of structure is Node, dstructure relates to its organization, name is assigned links are pointers, needed to store address because the structure has not ended yet // can link the list so only one value is needed to store the entire array
//how to create linked nodes: //create original node
n = malloc(sizeof(Node)); n->value = 264; n->next = NULL;
//create new node, link to existing node
Node *n2; n2 = malloc(sizeof(Node)); n2->value = 2012; n2->next = NULL; n->next = n2;
//constructor example Node *Node_construct(int v) {
Node *n; n = malloc(sizeof(Node)); n->value = v; n->next = NULL; return n;
} Node *List_insert(Node *insert, int v) //starting (original) node {
Node *n = Node_construct(v); n->next = head; return n;
}// program creates from the end of the list
int main() {
head=List_insert(head,264); head=List_insert(head,2012); return(0);
}