University of Delaware



Singly Linked Lists Hands On:(27 pts,3 pts each) Hands-on Due Sunday(This accompanies the second video – fill in the blanks and submit via canvas)-167640235585FirstLastNULL00FirstLastNULLGiven the following linked list: S a n r o r d a e yAnd the following code:void SLL::showstuff() {SNode *tmp = first->next; while (tmp != NULL ) { cout << tmp->data; if (tmp->next !=NULL) { tmp = tmp->next->next; } else { tmp = NULL; } cout << endl;}What is printed out? _______________________________________/*******************************************************************************/Identify the following code (give the code its appropriate name):void LL::__________________(int x) {currsize++;if (first == NULL) {first = new SNode(x);last = first;}else {last->next = new SNode(x);last = last->next;}}/*******************************************************************************/Identify the following code (give it an appropriate name):int SLL:: ________________(SNode* first, int x)? {SNode* tmp = first; int ct = 0;????while (tmp != NULL && tmp->data!=x) {? ????????tmp=tmp->next; ct++;? ????}? if (tmp == NULL) { Ct = -1; }????return ct;? }? /*******************************************************************************/Identify the following code (give it an appropriate name):string LL::________________(){if (size == 0) {return "";}else if (size == 1) {string x = last->word;delete last;size =0;last = NULL;first = NULL;return x;}else {string x = last->word;Node *tmp = first;for (int i = 1; i < size-1;i++) {tmp = tmp->next;}last = tmp;delete last->next;last->next = NULL;size--;return x;}}/*******************************************************************************/Given the following linked list: 31->44->17->29->21->42->18->60->12->22->33, a. what is returned?_________________________b. What did this function do? _______________________________int LL:: func() {SNode *tmp = first;SNode *tmp2 = first;while ((tmp2 != NULL)&&(tmp2->next != NULL)) {tmp = tmp->next;tmp2 = tmp2->next->next;}return tmp->data;}/*******************************************************************************/Given the following singly linked list: ______________________a->c->a->p->l->aIf you run the following function, then print out the linked list, what do you get?void SLL::f3() {SNode *tmp;SNode *tmp2 = first;while (tmp2->next != NULL) {tmp = tmp2->next;tmp2->next = tmp2->next->next;tmp->next = first;first = tmp;}last = tmp2;}/***********************************************************************************/ Given the following linked list, what is created? _______________________________"o"->"x"->"a"->"e"->"q"->"p"->"i"->"u"->"y"->"n"void LL::showstuff4() { Node *tmp = first; Node *tmp2 = first->next->next; first = tmp2->next; Node *tmp3 = first; tmp3 = tmp3->next; tmp2 = tmp2->next->next->next->next; tmp3->next = tmp2->next; tmp3 = tmp3->next; tmp3->next = tmp2; tmp3 = tmp3->next; tmp3->next = last; last->next = tmp; last = tmp->next; last->next = NULL;}/********************************************************************************/Assume you have two singly linked lists as follows:list1 is b->k->e->z->l->d->u->g->v->e->list2 is s->k->r->l->t->u->p->g->x->e->And SLL *n = new SLL();SNode *tmp = list1->first;SNode *tmp2 = list2->first;If you run the following function with:is(tmp,tmp2,n);then print out the new list (n), what do you get?void SLL::is(SNode *tmp, SNode *tmp2, SLL *n) { if (tmp2 == NULL) {first = n->first;size = n->size;last = n->last;return;}else if (tmp == NULL) {return(is(first,tmp2->next,n));}else if (tmp->c == tmp2->c) {if (n->size == 0) {n->first = new SNode(0);n->first->c = tmp->c;n->last = n->first;}else {n->last->next = new SNode(0);n->last->next->c = tmp->c;n->last = n->last->next;}n->size++;n->last->next = NULL;return(is(first, tmp2->next, n));}else {return(is(tmp->next, tmp2, n));}}/*************************************************************************************/Given the following list:rm->u->o->g->s->o->inIf you run the following function, then print out the list characters, what do you get?void SLL::MakeIt() {last->next = first;SNode *tmp = first;int ct = 0;int s2 = 0;SNode *tmp2;while (size > 0) {if (ct ==2) {if (s2 == 0) {first = tmp->next;tmp->next = tmp->next->next;first->next = NULL;tmp2 = first;}else {tmp2->next = tmp->next;tmp->next = tmp->next->next;tmp2->next->next = NULL;tmp2 = tmp2->next;}s2++;ct = 0;size--;}ct++;tmp = tmp->next;}last = tmp2;}************************************************************************** ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download