150 likes | 329 Views
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; };
E N D
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; • }; • 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.
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)
Problem 1 • struct node *trim(struct node *A){ • struct node *temp; • if(A==NULL) return NULL; • else{ • temp=A->next; • free(A); • Return temp; • } • }
Problem 1-Extension • void trim(struct node * *A){ • struct node *temp; • If (A==NULL) return; • temp=A; • A=A.next; • free(temp); • }
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)
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; • }
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.
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); • } • }
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.
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);
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.
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=; • }
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; • }