Lecture 21, ECE264, Spring 2012, Prof. Lu


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; }

============================

Wudi Zhou Prof. Lu's section

  1. include "ipa2.h"

Node *Node_construct(int v, Node *Next) {

 Node *n;
 n = malloc(sizeof(Node));
 n->next = Next;
 n->value = v;
 return n;

}

Node *List_insertFront(Node *head,int v) {

 Node *n = Node_construct(v);
 n->next = head;
 return n;

}

Node *List_search(Node*head, int v) {

 while ((head != NULL)&&(head->value)!=v) {
   head = head->next;
 }
 return head;

} Node *List_insertEnd(Node *h, int v) {

 Node *n = Node_construct(v,NULL);

 if (h == NULL)
   {
     return n;
   }
 if(v<h->value){
   n->next = h;
   return n;
 }
 Node *curr = h;
 while (curr->next != NULL && curr->next->value < v)
   {
     curr = curr->next;
   }
 n->next=curr->next;
 curr->next = n;
 return h;

}

Node *List_delete(Node *head, int del) {

 if(head == NULL)
   {
     return NULL;
   }
 Node *p=head; 
 
 if((head->value)== del)  
   {
     p=head->next;
     free(head);
     return p;
   } 
 Node *q=p->next;
 while((q!=NULL)&&(q->value)!=del)
   {
     p=q;
     q=q->next;
   }
 if(q!=NULL)
   {
     p->next=q->next;
     free (q);
   }
 return head;

}



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;

}


/**************************************************

Huayi Guo Lecture 0330 by professor Niklas Elmqvist


/* lecture0330 ipa2-2

for the Unix and Window system the text files are different when there is a new line And that is important!!

  • /
  1. include<math.h>

int main(int argc, char*argv[]) {

  //check arguments
 
  //check input file to see if it is valid
  const int Data_size = 101;
  
  char buffer[Data_size], block[Data_size];
  block[0] = '\0';
  
  int muti_line =0;
  
  while (fgets(buffer, Date_size, f) != NULL){
      if(buffer[strlen(buffer) - 1 ] == '\n')

buffer[strlen(buffer) - 1 ] ='\0'; if(strlen(buffer) ==0){ //found the end of the block if(){ printf("%s\n",blcok); } else{ int dim = sqrt(strlen(block)); int i,j; for(i =0;i < dim;i++) { for(j =0; j <dim; j++){ printf("%c".block[i*dim+j]); } printf("\n"); } } printf("\n"); block ='\0'; muti_line =0; }

      else{

//found the data if(strlen(block) != 0) nuti_line = 1; strcpy(block+strlen(block),buffer); }

  }
  
  fclose(f);
  return EXIT_SUCCESS;

}

/*

 check if there's a \n at the end of the file
  • /
===================

Kevin Tan(0023987592), section#2 notes 04/03


  1. include "ipa2.h"

Node *Node_construct(int v, Node *Next) {

 Node *n;
 n = malloc(sizeof(Node));
 n->next = Next;
 n->value = v;
 return n;

}

Node *List_insertFront(Node *head,int v) {

 Node *n = Node_construct(v);
 n->next = head;
 return n;

}

Node *List_search(Node*head, int v) {

 while ((head != NULL)&&(head->value)!=v) {
   head = head->next;
 }
 return head;

} Node *List_insertEnd(Node *h, int v) {

 Node *n = Node_construct(v,NULL);

 if (h == NULL)
   {
     return n;
   }
 if(v<h->value){
   n->next = h;
   return n;
 }
 Node *curr = h;
 while (curr->next != NULL && curr->next->value < v)
   {
     curr = curr->next;
   }
 n->next=curr->next;
 curr->next = n;
 return h;

}

Node *List_delete(Node *head, int del) {

 if(head == NULL)
   {
     return NULL;
   }
 Node *p=head; 
 
 if((head->value)== del)  
   {
     p=head->next;
     free(head);
     return p;
   } 
 Node *q=p->next;
 while((q!=NULL)&&(q->value)!=del)
   {
     p=q;
     q=q->next;
   }
 if(q!=NULL)
   {
     p->next=q->next;
     free (q);
   }
 return head;

}

====================

Hanye Xu April 03

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; }


Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

has a message for current ECE438 students.

Sean Hu, ECE PhD 2009