{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS301-Lec02 handout

# CS301-Lec02 handout - CS301 Data Structures Lecture No 02...

This preview shows pages 1–3. Sign up to view the full content.

CS301 – Data Structures Lecture No. 02 ___________________________________________________________________ Data Structures Lecture No. 02 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1, 3.2.2 Summary 1) List Implementation add Method next Method remove Method find Method Other Methods 2) Analysis Of Array List 3) List Using Linked Memory 4) Linked List Today, we will discuss the concept of list operations. You may have a fair idea of ‘ start operation’ that sets the current pointer to the first element of the list while the tail operation moves the current pointer to the last element of the list. In the previous lecture, we discussed the operation next that moves the current pointer one element forward. Similarly, there is the ‘ back operation’ which moves the current pointer one element backward. List Implementation Now we will see what the implementation of the list is and how one can create a list in C++. After designing the interface for the list, it is advisable to know how to implement that interface. Suppose we want to create a list of integers. For this purpose, the methods of the list can be implemented with the use of an array inside. For example, the list of integers (2, 6, 8, 7, 1) can be represented in the following manner where the current position is 3. A 2 6 8 7 1 current size 1 2 3 4 5 3 5 In this case, we start the index of the array from 1 just for simplification against the usual practice in which the index of an array starts from zero in C++. It is not necessary to always start the indexing from zero. Sometimes, it is required to start the indexing from 1. For this, we leave the zeroth position and start using the array from index 1 that is actually the second position. Suppose we have to store the numbers from 1 to 6 in the array. We take an array of 7 elements and put the numbers from the index 1. Thus there is a correspondence between index and the numbers stored in it. This is not very useful. So, it does not justify the non-use of zeroth position of the Page 1 of 8

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

View Full Document
CS301 – Data Structures Lecture No. 02 ___________________________________________________________________ array out-rightly. However for simplification purposes, it is good to use the index from 1. add Method Now we will talk about adding an element to the list. Suppose there is a call to add an element in the list i.e. add(9) . As we said earlier that the current position is 3, so by adding the element 9 to the list, the new list will be (2, 6, 8, 9, 7, 1). To add the new element (9) to the list at the current position, at first, we have to make space for this element. For this purpose, we shift every element on the right of 8 (the current position) to one place on the right. Thus after creating the space for new element at position 4, the array can be represented as A 2 6 8 7 1 current size 1 2 3 4 5 3 5 Now in the second step, we put the element 9 at the empty space i.e. position 4. Thus the array will attain the following shape. The figure shows the elements in the array in the same order as stored in the list.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

CS301-Lec02 handout - CS301 Data Structures Lecture No 02...

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

View Full Document
Ask a homework question - tutors are online