1 / 15

DSA Homework 4 – Lists

This notice provides suggestions and hints for operating lists structure, including basic structure, list length, terminate conditions, making changes, removing nodes, common pitfalls, and skip lists.

lantz
Download Presentation

DSA Homework 4 – Lists

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. DSA Homework 4 – Lists

  2. List Operation Notice Some suggestions and hints for operating lists structure

  3. Basic Structure • A (singly linked) list is a structure consisting of nodes and interconnecting links. • It serves as a dynamically stretchable array. head data = a data = b data = c data = d

  4. 1.List Length • Not like arrays, usually we cannot infer the length of lists directly unless explicitly recorded. • Lists are designed to be dynamically changeable in size. • The size of arrays are fixed when declared. • So usually all operations on lists (insert, delete, search) depends on iterative or recursive approaches. void op (ptr) { …… if (ptr != nil) op(ptr->next);} while (ptr != nil) { …… ptr= ptr->next;}

  5. 2.Terminate Condition • Terminate conditions can greatly affect the design of algorithms. • e.g. (ptr != nil) / (ptr->next != nil) • e.g. (ptr1!=nil && ptr2!=nil) / (ptr1!=nil || ptr2!=nil) • Consider the following code snippets for printing all elements: • Which one is correct? Void printList(Node ptr) { while (ptr!= nil) { print(ptr->data); ptr = ptr->next; }} Void printList(Node ptr) { while (ptr->next != nil) { print(ptr->data); ptr = ptr->next; }}

  6. 3.What Really Makes Changes? • Always identify the pointer you are actually operating on. • Here we means call by value or call by reference. • Consider the following code snippets for removing a node. Void removeNode(Node ptr, int value) { if (ptr->data == value) ptr =nil; else removeNode(ptr->next, value);} ptr

  7. 4.Remove Nodes • Always try to remove all resources unused. • C/C++ language doesn’t do garbage collection. • Constantly requiring spaces without freeing them will cause memory leakage. • Of course, be sure to release the “right” resources. • Freeing wrong resources may lead to disastrous results.

  8. Homework Explanation Common pitfalls.

  9. 4.1.1/2 Merging lists • Given list x1:nand y1:m, interleave the nodes to a new list. • Remember that n and m are not given as parameters. • Even if the length is recorded, it should not appear on argument list. • Complex pointer operation is needed. • Final connection. • Many works fail to connect correctly in the end of the shorter list. • Pay attention to your loop condition. • Is your algorithm really O(m+n)? • Some works append the new node to the resulting list. • Append operation sometimes is not O(1)!!

  10. 4.1.3/4 Concatenate Circular Lists • Given two circular lists (the last node), concatenated to a new list. • Remember that you are given the last node. Not first! • Always use temp pointers to do the pointer exchanging. • Don’t write codes like listA->ptr = listB->ptr; listB->ptr = listA->ptr • Return listA or listB? • The first node of the new list is the first node of original listA. … last first last first listA listB

  11. 4.1.5/6 Xor Traversal • Given a special doubly linked list, traverse it from left to right/right to left. • Note that the member link is not the one you familiar with. • n->link = l  r, l/r isthe address of node left/right to n. • Use the propertyl  (n->link) = l (l  r) = r to do traversal. • Be sure to specify the parameter. • Since it is a doubly linked list, head/tail pointer will matter. • Again temp pointers are important. • Try not to append auxiliary parameters. Void traverse(head, prev) { …} Void traverse(head) { realTraverse(head, nil);}

  12. 4.1.7 Node Deletion • Delete the node whose address is given. • No direct deletion is feasible. • Never tries ptr= nil; It does nothing. • We still can simulate the effect by copying the next node. • Usually releasing spaces use is a must-do. • When release, be sure not to release the wrong pointer. • Since this is not strictly required in the problem, no penalty will be applied. (But is highly recommended!) ptr

  13. 4.1.8 Cycle Detection • Floyd's cycle-finding algorithm. • By simultaneously operating two pointers of different speed, the faster one will catch up the slower one eventually. • It also detect the beginning of the cycle. • Always remember citing. It is rather important in all academic reports and theses.

  14. 4.1.9 Finding 3Kth Element • Naïve solution : traverse the whole list to get K, then step to the 3Kth element. • Pay attention to your accumulator and loop condition. • Remember that you are at the first node initially. • Simpler solution : maintaining two pointers moves by 4 nodes/3 nodes at a time. Return the slower one when the faster one reaches the end. • You are at the first node initially, so beware not to step further in the first loop. • K is NOT known in advance!

  15. 4.2 Skip Lists • O(n) time building. • But a larger constant overhead both on time and space complexity. • O(lgn) time insertion/deletion. • Lgn refers to the time finding the insertion/deletion point. • O(lgn) time searching.

More Related