CS112_20_Slides--Efficiency&ADT-2

CS112_20_Slides--Efficiency&ADT-2 - Efficiency and...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions Efficiency and ADTs-cont. Yinglin Wang CS Dept., SJTU CS112 Fundamentals of Programming Abstractions In the linked list representation, given a buffer variable, at any time you only directly know two positions: the start position of the buffer and the cursor position of the buffer. Other elements can be found indirectly through search starting from them. Learn how to move the cursor forward. If buffer->cursor point at the cell containing 'A‘ , buffer->cursor->link point at the cell containing ‘ C‘as the following picture shows. you can move the cursor one step forward by running: buffer->cursor= buffer->cursor->link; Before the operation after the operation Operation: buffer->cursor= buffer->cursor->link;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions In the linked list representation, given a buffer variable, at any time you only directly know two positions: the start position of the buffer and the cursor position of the buffer. Other elements can be found indirectly through search starting from them. Learn how to move the cursor forward. If buffer->cursor point at the cell containing 'A‘ , buffer->cursor->link point at the cell containing ‘ C‘as the following picture shows. you can move the cursor one step forward by running: buffer->cursor= buffer->cursor->link; Before the operation after the operation Operation: buffer->cursor= buffer->cursor->link; CS112 Fundamentals of Programming Abstractions after the operation So if you want to find the m -th element from the beginning, what will you do? you should start from buffer->start, and search forward m steps. Before the operation Operation: cp= cp->link; cellT *cp; cp=buffer->start; for(i=1; i<m; i++) cp=cp->link; cp cp Find the m-th element from the beginning But You should modify the coding!
Background image of page 2
CS112 Fundamentals of Programming Abstractions cellT *cp; cp=buffer->start; for(i=1; i<m; i++) cp=cp->link; Find the m-th element from the beginning cellT *cp; cp=buffer->start; for(i=1; i<m; i++) { if(cp!=NULL) cp=cp->link else break; } Should be modified to : Your code: CS112 Fundamentals of Programming Abstractions So if you want to find the final element, it means you should search from the beginning to the end, then you will traverse the whole list. you should start from
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

CS112_20_Slides--Efficiency&amp;amp;ADT-2 - Efficiency and...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online