Lecture 21 - Professor Lu (taught by Professor Elmqvist) - John Ribeiro
In order to think about recursion, think about the base case. Then move on to thinking about the recursive case
- Spending and Budgets helpful in understanding this concept
Linked Lists are resizable.
Node * List)insertFront ( Node * head, int value ) {
return Node_create(value, head);
}
void List_destroy ( Node * head ) {
while ( head != NULL ) { Node * temp = head; head = head->next; destroy ( temp ); }
}
Node * List_insertBack ( Node * head, int value ) {
Node * node = Node_create ( value, NULL ); if ( head == NULL ) { return node; } Node * curr = head; while ( curr->next != NULL ) { curr = curr -> next; } curr->next = node; return head;
}
Node * List_search ( Node * head, int v ) {
while ( head != NULL && head->value != v ) { head = head -> next; } return head;
}
Linked List does not support random access.
Lecture 21_3/29_Kailu Song void Node_destroy(Node *node) { free(node); }
Node *List_elementAt(Node *head, int index);
Node *List_search(Node *head, int v) { while(head != NULL && head -> value != v) { head = head -> next; } return head; }
Node *List_insertBack(Node *head, int value) { Node *node = Node_create(value,NULL); if (head == NULL){return node;} Node *curr = head; while(curr ->next != NULL) { curr= curr->next; } curr->next = node; return head; }
Node *List_insertFront(Node *head, int value) { return Node_create(value, head); }
void List_print(Node *head); { while (head != NULL) { printf("%d", head->value); head = head->next; } }
void List_destroy(Node *head); { while (head != NULL) { Node *tmp = head; head = head->next; Node_destroy(tmp); } }
int main(int argc, char *argv[]) { int i; Node *head = NULL; for (i=0;i<10;i++) { head = List_insertFront(head, i); } List_print(head); printf("value = %d", List_search(head,8)->value); List_destroy(head);
return EXIT_SUCCESS; }
ECE264 LEC20 Shiyu Wang April 1
4=[4]
=[3 1] =[1 3] =[2 1 1] =[1 2 1] =[1 1 2] =[1 1 1 1]
Void x(int *array, int budget, int pos) {
if(budget==0) { print return; } int spending; for(spending = 1; spending <= budget; spending++) { array[pos]=spending; x(array,budget,spending,pos+1); }
}
int main(int argc, char*argv[]) {
if( argc!=2) { return Exit_failure; } num = strtol......; . . .
}
int main {
int i; Node * head=BULL; for( i=0;i<6;i++) { head = List_insect(head,i); } List_print(head); List_destroy(head); return exit_success;
}
Node *Node_construct(int value ,Node* next) {
Node *node =malloc(sizeof(Node)); node->next=next; node->value = value; return node;
}
Node *List_insertfront(Node*head,int value) {
return Node_crest(value,head);
}
Void List_Print(Node*head) {
while(head!=NULL) { printf("%d\n",head->value); head=head->next; }
}
Void List_destroy(Node *head) {
while(head!=NULL) { Node *temp = head; head=head->next; Node_destroy(temp); } free;
}
Node* List_insertback(Node *head, int value) {
Node*node = Node_creat(value, NULL) if(head==NULL) { return node; } Node *arr =head; while(arr->next !=NULL) { arr = arr->next; } arr->next =node;
}
Node *List_search(Node *head, int v) {
while((head!=NULL) && (head->value != v)) { head=head->next; } return head;
}