(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
[[Category:ECE264]] [[Category:Programming]] [[Category:C]] [[Category:lecture notes]]
 +
 +
=Lecture 22, [[ECE264]], Spring 2012, Prof. Lu=
 +
----
 
John Ribeiro - Professor Lu 8:30 - 9:20 - Lesson 22
 
John Ribeiro - Professor Lu 8:30 - 9:20 - Lesson 22
  
Line 253: Line 257:
  
 
*/
 
*/
 +
 
===============================
 
===============================
Kevin Tan(0023987592), section#1 notes 04/05
+
Kevin Tan(0023987592), section#2 notes 04/05
  
 
random access
 
random access
Line 271: Line 276:
 
.
 
.
 
fscanf(fp,"%d%d",&a,&b);
 
fscanf(fp,"%d%d",&a,&b);
 +
 +
 +
 +
____________________________________________________________________
 +
 +
Shiyu Wang ECE264 LEC22  April 8th
 +
 +
Node*List_Search(Node*head,int v)
 +
{
 +
  while((head!=NULL)&&((head->value)!=v))
 +
    {
 +
      head=head->next;
 +
    }
 +
  return head;
 +
}
 +
 +
Node*List_insertFront(Node*head,int v)
 +
{
 +
  Node*n=Node_construct(v);
 +
  n->next = head;
 +
  return n;
 +
}
 +
 +
Node *List_insertEnd(Node*head,int v)
 +
{
 +
  Node* n=Node_construct(v);
 +
  if(head==NULL)
 +
  {
 +
    return n;
 +
  }
 +
  Node* p=head;
 +
  while((p->next)!=NULL)
 +
  {
 +
    P=P->next;
 +
  }
 +
  p->next=n;
 +
  return head;
 +
}
 +
 +
Node *List_delete(Node *head,int v)
 +
{
 +
  if(head==NULL)
 +
  {
 +
    return NULL;
 +
  }
 +
  Node*p=head;
 +
  if(head->value ==v)
 +
  {
 +
    p=head->next;
 +
    free(head);
 +
    return p;
 +
  }
 +
  Node*q=p->next;
 +
  while((q!=NULL)&&(q->value)!=v))
 +
  {
 +
    p=q;
 +
    q=q->next;
 +
  }
 +
  if(q!=NULL)
 +
  {
 +
    p->next=q->next;
 +
    free(q);
 +
  }
 +
  return head;
 +
}
 +
 +
main
 +
{
 +
  Node*head=NULL;
 +
  head=list_insert(head,2);
 +
  Node*p;
 +
  p=List_search(head,16);
 +
  if(p!=NULL)
 +
  {
 +
    //found it
 +
  }
 +
  return 0;
 +
}
 +
 +
-------------------------------------------------
 +
Wudi Zhou, Professor Lu's section, Lecture 22, April 9th
 +
 +
 +
static Node * Node_construct(int v)
 +
{
 +
Node * n = malloc(sizeof(Node));
 +
n -> value = v;
 +
n -> next = 0;
 +
return n;
 +
}
 +
 +
Node * List_insert(Node * head, int v)
 +
{
 +
printf("insert %d\n", v);
 +
Node * p = Node_construct(v);
 +
p -> next = head;
 +
return p;
 +
}
 +
 +
Node * List_search(Node * head, int v)
 +
{
 +
Node * p = head;
 +
while (p != NULL)
 +
{
 +
  if ((p -> value) == v)
 +
  {
 +
  return p;
 +
  }
 +
  p = p -> next;
 +
}
 +
return p;
 +
}
 +
 +
Node * List_delete(Node * head, int v)
 +
{
 +
printf("delete %d\n", v);
 +
Node * p = head;
 +
if (p == NULL) /* empty list, do nothing */
 +
  {
 +
  return p;
 +
  }
 +
  if ((p -> value) == v)
 +
  {
 +
p = p -> next;
 +
free (head);
 +
return p;
 +
}
 +
Node * q = p -> next;
 +
while ((q != NULL) && ((q -> value) != v))
 +
{
 +
  p = p -> next;
 +
  q = q -> next;
 +
}
 +
if (q != NULL)
 +
{
 +
p -> next = q -> next;
 +
free (q);
 +
}
 +
return head;
 +
}
 +
  void List_destruct(Node * head)
 +
  {
 +
  while (head != NULL)
 +
  {
 +
  Node * p = head -> next;
 +
  free (head);
 +
  head = p;
 +
  }
 +
}
 +
 +
=============================
 +
Hanye Xu Lec22
 +
 +
 +
Node *List_search(Node *t, int v) { while((t != NULL) && (t->value) != v) { t = t->next; } return t; }
 +
 +
Node *List_insertFront(Node *head, int v) { Node *n = Node_construct(v); n->next = head; return n; }
 +
 +
Node *List_insertEnd(Node *t, int v) { Node *n = Node_construct(v); if (t == NULL) { return n; } Node *p = t; while ((p->next) != NULL) { p = p-> next; } return t; }
 +
 +
Node *List_delete(Node *head, int v) { if (head == NULL) { return NULL; } Node *p = head; if ((head ->value) == v) { p = head -> next; free(head); return p; } }
 +
 +
Node *q = p-> next;/*p is not NULL */ while ((q != NULL)&&(q ->value) != v) { p = q; q = q->next; } if (q != NULL) { p->next = q -> next; free(q); } return head; }
 +
 +
 +
main: Node *head = NULL;(initialize) head = list_insertFront/End (head, 2); head = list_delete(head,9);
 +
 +
 +
===========================
 +
Rohan Khante
 +
 +
 +
Node *List_search(Node *t, int v)
 +
{
 +
while((t != NULL) && (t->value) != v)
 +
{
 +
t = t->next;
 +
}
 +
return t;
 +
}
 +
The while loop here makes sure that the Node is not empty and continues till the value in the node  is NOT v.
 +
When it comes to t->value == v the t has got incremented and t is returned.
 +
 +
Node *List_insertFront(Node *head, int v)
 +
{
 +
 +
Node *n = Node_construct(v);
 +
 +
n->next = head;
 +
 +
return n;
 +
 +
}
 +
Over here you create a node using the node_construct function. The next location of n is pointed to head and then the head is returned.
 +
In main the function call looks like head = List_InsertFront(*head,v); Thus head value gets changed to n. Thus a new element is added at the start of the list.
 +
 +
Node *List_insertEnd(Node *t, int v)
 +
 +
{
 +
 +
Node *n = Node_construct(v);
 +
 +
if (t == NULL)
 +
 +
{
 +
 +
return n;
 +
 +
}
 +
 +
Node *p = t;
 +
 +
while ((p->next) != NULL)
 +
 +
{ p = p-> next; }
 +
 +
return t;
 +
 +
}
 +
Here a Node is again created using the Node_construct function. If loop ensured that the node being passed into the function is not NULL pointer. Here p pointer is incremented till it reaches the end of list. Note like previous function(add at start) you do not need to change the head function
 +
 +
Node *List_delete(Node *head, int v)
 +
 +
{ if (head == NULL)
 +
 +
{
 +
 +
return NULL;
 +
 +
}
 +
 +
Node *p = head;
 +
 +
if ((head ->value) == v)
 +
 +
{
 +
 +
p = head -> next;
 +
 +
free(head);
 +
 +
return p;
 +
 +
}
 +
 +
}
 +
 +
Node *q = p-> next;/*p is not NULL */
 +
 +
while ((q != NULL)&&(q ->value) != v)
 +
 +
{ p = q; q = q->next; }
 +
 +
if (q != NULL)
 +
 +
{ p->next = q -> next;
 +
 +
 +
free(q);
 +
 +
}
 +
 +
return head;
 +
 +
}
 +
 +
This is function that deletes a node whose value matches the value passed as parameter. It makes sure that the list in non-empty to begin with. If the heads value is same as the passed value then head is pointed to the next node and the original heads memory is freed. Here the list is browsed through till the node is found. Then the node is deleted by freeing the memory. Then the nodes on either sides of the deleted node are joined so the list becomes continuous again. This is done by pointing the 'next' of the "previous node" to the "next node"
 +
----
 +
[[2012_Spring_ECE_264_Lu|Back to ECE264, Spring 2012, Prof. Lu]]

Latest revision as of 04:27, 11 July 2012


Lecture 22, ECE264, Spring 2012, Prof. Lu


John Ribeiro - Professor Lu 8:30 - 9:20 - Lesson 22

Node * List_serach ( NOde * led, int v ) {

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

}

/* Remember to check if the pointer is equal to NULL. This makes sure that the list exists in the first place. */

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

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

}

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

 Node * n = Node_construct(v);
 if ( head == NULL )
 {
   return n;
 }

/* If the list is empty create only one node */

 Node * p = head;
 while ( ( p->next) != NULL )
 {
   p = p -> next;
 }
 p->next = n;
 return head;

}

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

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

}

    • Exam 2 -> 8 - 10 PM this Thursday **

EE 206/207 open book and open note Will cover: 1) File 2) Structure 3) Linked List 4) Recursion

Possibly more questions and 1.5 times longer than exam 1

In main:

Node * head = NULL; head = List_insert ( head, 2 );

Node * p; p = List_search ( head, 6 );

if ( p != NULL ) {

 /* The value is found */

}



4/3/2012 Lecture 22_Kailu Song

Node *List_search(Node *t, int v) { while((t != NULL) && (t->value) != v) { t = t->next; } return t; }

Node *List_insertFront(Node *head, int v) { Node *n = Node_construct(v); n->next = head; return n; }

Node *List_insertEnd(Node *t, int v) { Node *n = Node_construct(v); if (t == NULL) { return n; } Node *p = t; while ((p->next) != NULL) { p = p-> next; } return t; }

Node *List_delete(Node *head, int v) { if (head == NULL) { return NULL; } Node *p = head; if ((head ->value) == v) { p = head -> next; free(head); return p; } }

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


main: Node *head = NULL;(initialize) head = list_insertFront/End (head, 2); head = list_delete(head,9);



4/4/2012 Huayi Guo Lecture 22 Professor Niklas Elmqvist


/*

 exam 
 
 Q1: file I/O;
 Q2: structure;
 Q3: linked list;
 Q4: recursion;
 BE SURE TO LOOK AT YOUR ASSIGNMENTS AND EXERCISES
 
 shallow vs deep copy
 
 DEEP COPY is when you are copying not only of the pointer but also the contents that the pointer
 is pointing to.
 
 Example:
 */
 typedef enum{ MALE, FEMALE }Gender;
 struct Person{
 char * first;
 char * last;
 Gender gender;  

 }Person;
 
 Person Person_create(char * first, char * last, int age, Gender gender)
 {
     Person person;

person.first = strdup(first); person.last = strdup(last); person.age = age; person.gender = gender; return person;

 }
 
 void Person_destroy(Person person)
 {
    //you need to free the thing that created by strdup!!
    free(person.first);

free(person.last);

 }
 Person_copy(Person person)
 {
    return Person_create(preson.first, person.last, 

person.age, person.gender);

 }
 Person_addSuffix(Person person, char *middler)
 {
    char * tmp = malloc(strlen(person->last)_ strlen())
 
 }
 
 Person_print(Person person)
 {
   printf("first: %s, last %s, age: %d, gender: %s\n", person.first, person. last, person.age,

(person.gender == MALE ? "male":"female"));

 }
  
 int main(int argc, char: argv[])
{
   if(argc != 5){

return EXIT_FAILURE; }

int age = strtol(argv[3], NULL, 10); Gender gender = (strcmp(argv[4],"male")==0) ? MALE: FEMALE Person person1 = Person_create(argv[1], argv[2], age, gender);

     Pereon person2 = Person_copy(person1); //deep copy
    //Person person2 = person1 ;//shallow copy

   Person_print(person1);

Person_print(person2);

Person_destroy(person1); // Since person2 is deep copy you need to free it // If you do shallow copy then you DONT need to free it, //otherwise it's a segmentation fault Person_destroy(person2);

return EXIT_SUCCESS;

}  

/*

  • /
===================

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

random access

arr[5]; arr[9];

FILE *fp= NULL; fp = fopen(filename,"rb");

int a; double b; fread(&a,sizeof(int),1,fp); fread(&b,sizeof(double),1,fp); . . fscanf(fp,"%d%d",&a,&b);


____________________________________________________________________

Shiyu Wang ECE264 LEC22 April 8th

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

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

}

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

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

}

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

 Node* n=Node_construct(v);
 if(head==NULL)
 {
   return n;
 }
 Node* p=head;
 while((p->next)!=NULL)
 {
   P=P->next;
 }
 p->next=n;
 return head;

}

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

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

}

main {

 Node*head=NULL;
 head=list_insert(head,2);
 Node*p;
 p=List_search(head,16);
 if(p!=NULL)
 {
    //found it
 }
 return 0;

}


Wudi Zhou, Professor Lu's section, Lecture 22, April 9th


static Node * Node_construct(int v) { Node * n = malloc(sizeof(Node)); n -> value = v; n -> next = 0; return n; }

Node * List_insert(Node * head, int v) { printf("insert %d\n", v); Node * p = Node_construct(v); p -> next = head; return p; }

Node * List_search(Node * head, int v) { Node * p = head; while (p != NULL)

{
 if ((p -> value) == v)
 {
  return p;
 }
 p = p -> next;
}
return p;

}

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

printf("delete %d\n", v);
Node * p = head;
if (p == NULL) /* empty list, do nothing */
 {
  return p;
 }
 if ((p -> value) == v)
 {
p = p -> next;
free (head);
return p;
}

Node * q = p -> next; while ((q != NULL) && ((q -> value) != v)) {

 p = p -> next;
 q = q -> next;

} if (q != NULL) {

p -> next = q -> next;
free (q);

}

return head;
}
 void List_destruct(Node * head)
 {
 while (head != NULL)
 {
 Node * p = head -> next;
 free (head);
 head = p;
 }
}
=================

Hanye Xu Lec22


Node *List_search(Node *t, int v) { while((t != NULL) && (t->value) != v) { t = t->next; } return t; }

Node *List_insertFront(Node *head, int v) { Node *n = Node_construct(v); n->next = head; return n; }

Node *List_insertEnd(Node *t, int v) { Node *n = Node_construct(v); if (t == NULL) { return n; } Node *p = t; while ((p->next) != NULL) { p = p-> next; } return t; }

Node *List_delete(Node *head, int v) { if (head == NULL) { return NULL; } Node *p = head; if ((head ->value) == v) { p = head -> next; free(head); return p; } }

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


main: Node *head = NULL;(initialize) head = list_insertFront/End (head, 2); head = list_delete(head,9);


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

Rohan Khante


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

while((t != NULL) && (t->value) != v) 

{

t = t->next;

} return t;

}

The while loop here makes sure that the Node is not empty and continues till the value in the node is NOT v. When it comes to t->value == v the t has got incremented and t is returned.

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

Node *n = Node_construct(v);

n->next = head;

return n;

} Over here you create a node using the node_construct function. The next location of n is pointed to head and then the head is returned. In main the function call looks like head = List_InsertFront(*head,v); Thus head value gets changed to n. Thus a new element is added at the start of the list.

Node *List_insertEnd(Node *t, int v)

{

Node *n = Node_construct(v);

if (t == NULL)

{

return n;

}

Node *p = t;

while ((p->next) != NULL)

{ p = p-> next; }

return t;

} Here a Node is again created using the Node_construct function. If loop ensured that the node being passed into the function is not NULL pointer. Here p pointer is incremented till it reaches the end of list. Note like previous function(add at start) you do not need to change the head function

Node *List_delete(Node *head, int v)

{ if (head == NULL)

{

return NULL;

}

Node *p = head;

if ((head ->value) == v)

{

p = head -> next;

free(head);

return p;

}

}

Node *q = p-> next;/*p is not NULL */

while ((q != NULL)&&(q ->value) != v)

{ p = q; q = q->next; }

if (q != NULL)

{ p->next = q -> next;


free(q);

}

return head;

}

This is function that deletes a node whose value matches the value passed as parameter. It makes sure that the list in non-empty to begin with. If the heads value is same as the passed value then head is pointed to the next node and the original heads memory is freed. Here the list is browsed through till the node is found. Then the node is deleted by freeing the memory. Then the nodes on either sides of the deleted node are joined so the list becomes continuous again. This is done by pointing the 'next' of the "previous node" to the "next node"


Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett