Line 89: Line 89:
  
 
--------------------------------------------
 
--------------------------------------------
 +
 +
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);

Revision as of 03:54, 3 April 2012

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

Alumni Liaison

Abstract algebra continues the conceptual developments of linear algebra, on an even grander scale.

Dr. Paul Garrett