CS301-Lec05 handout - CS301 Data Structures Lecture No 05...

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

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 05 ___________________________________________________________________ Data Structures Lecture No. 05 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 3 3.1, 3.2.5, 3.3.1, 3.3.2 (array implementation) Summary 1) Benefits of using circular list 2) Abstract Data Type 3) Stacks 4) Stack Implementation using arrays In the previous lecture, we demonstrated the use of the circular list for the resolution of the Josephus problem. After writing a program with the help of this data structure, a leader among ten persons was selected. You must have noted many things while trying to solve the problem. These things will help us to understand the usage of data structures in C++, thus making the programming easy. The code of the program is given below. #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl; } In the program, we include the file of the class CList and create its object i.e. list. Then we solve the problem by using the add, start, length, next, remove and get methods of the class CList . In the program, we have included already-defined data structure CList . After defining its different methods, we have an interface of Clist . There is no need to be worry about the nature of the list i.e. whether it is linked list, doubly linked list or an array. Page 1 of 10
Background image of page 1

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

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 05 ___________________________________________________________________ For us, it is only a list to be manipulated according to our requirement. You will see that a programmer may use different methods of the list object to solve the problem. We add elements to the list by a simple call of add method and go to the first element of the list by start method. Here, the length method is used in the condition of the while loop. Then we remove elements from the list and use the next , get and remove methods during this process. We get the current element by using the get method, then remove it by calling the remove method and then go to the next element by the method next . This way, all the elements are removed from the list except one element, called the leader. This one element remains there as we execute the while loop one less than the length of the list. In singly linked list, the ‘ next’ returns false when it reaches to the last node due to the fact that the next field of the last node is set to NULL. But in a circularly linked list there is no NULL. It will be there only when there is no node in the list. The whole process, which we carried out to solve the Josephus problem, can also be
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

CS301-Lec05 handout - CS301 Data Structures Lecture No 05...

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

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