1 / 15

Recitation 9

COP 3502 Christopher Elwell Spring 2005. Recitation 9. Administrivia. Programming Assignment 3 posted Programming Assignment 2 grades posted. Quiz. Assume we have defined this structure: struct node{ int val; struct node *next; };

tam
Download Presentation

Recitation 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3502 Christopher Elwell Spring 2005 Recitation 9

  2. Administrivia • Programming Assignment 3 posted • Programming Assignment 2 grades posted

  3. Quiz • Assume we have defined this structure: • struct node{ • int val; • struct node *next; • }; • Write a function, void insert(struct node *head, int val) which inserts a new node at the tail of the list pointed to by *head. Assume that the list already contains at least one node (so you won't have to change the head pointer). • Also, draw a diagram to show the list before and after the insertion.

  4. Problem 1 • struct node{ • int data; • struct node *next; • }; • Given a list pointed to by A, delete the first node using this function: • struct node *trim(struct node *A)

  5. Problem 1 • struct node *trim(struct node *A){ • struct node *temp; • if(A==NULL) return NULL; • else{ • temp=A->next; • free(A); • Return temp; • } • }

  6. Problem 1-Extension • void trim(struct node * *A){ • struct node *temp; • If (A==NULL) return; • temp=A; • A=A.next; • free(temp); • }

  7. Problem 2 • Write a function which returns the data stored in the ith node of a list containing n nodes: • int contents(struct node *list, int index)

  8. Problem 2 • int contents(struct node *list, int index){ • int i; • If (index>1){ • for (i=1; i<index; i++) list=list->next; • } • Return list->data; • }

  9. Problem 3 • A list contains pointer to the first node as well as to the last node. The following call is supposed to remove the first element of the list. • First_go(struct node &first, struct node &last); • Write the appropriate function. Take care of all possible situations.

  10. Problem 3 • void First_go(struct node**qfront, struct node**qback) • { • struct node* temp; • if (*qfront!= NULL) • { • temp = *qfront; • *qfront = temp->next; • if ((*qfront) == NULL) *qback = NULL; • free (temp); • } • }

  11. Problem 4 • Given a linked list pList, whose elements are in non-decreasing order, count the number of duplicate items present in the list. • If the list is 12,23,23,45,67,67,67,67,82, it should print out count = 4.

  12. Problem 4 • ... • pCur= pList; • while(pCur->next !=NULL) { • if(pCur->data == pCur->next->data) { • count++; • afternext = pCur->next->next; • pCur->next = afternext; • } • else • pCur = pCur->next; • } • printf(“\n count = %d “, count);

  13. Problem 5 • Complete the following incomplete function to extend the circular linked list being pointed by p ( the first node) by appending a singly linked list (pointed by q ) to the end of the circular list. The resulting list will be one big circular list.

  14. Problem 5 • void extend(struct node *p, struct node * q) • { • struct node *t; /* t is used to traverse the lists */ • if ( _____________ ) /* if standardlinked list is empty */ • return; • t = p; /* Assume p cannot be NULL */ • while ( ________ ) /*traverse the circular list */ • t =; • ____________; /* append the standard list */ • while ( __________ ) /* traverse the std. list */ • t = ; • t->next=; • }

  15. Problem 5 • void extend(struct node *p, struct node * q) • { • struct node *t; /* t is used to traverse the lists */ • if ( q == NULL ) return; /* if standardlinked list is empty */ • t = p; /* Assume p cannot be NULL */ • while (t->next!=p ) t=t->next; /*traverse the circular list*/ • t->next=q; /*append the standard list*/ • while (t->next!=NULL) t=t->next; /*traverse the standard list*/ • t->next=p; • }

More Related