Free Essay

Assigment

In: Computers and Technology

Submitted By alih
Words 13571
Pages 55
CS301 – Data Structures ___________________________________________________________________

Data Structures

1

CS301 – Data Structures ___________________________________________________________________ Data Structures..........................................................................................................1 Lecture No. 01 ............................................................................................................3 Lecture No. 02 ..........................................................................................................12 Lecture No. 03 ..........................................................................................................21 Lecture No. 04 ..........................................................................................................34 Lecture No. 05 ..........................................................................................................49 Lecture No. 06 ..........................................................................................................59 Lecture No. 07 ..........................................................................................................66 Lecture No. 08 ..........................................................................................................73 Lecture No. 09 ..........................................................................................................84 Lecture No. 10 ..........................................................................................................97 Lecture No. 11 ........................................................................................................108 Lecture No. 12 ........................................................................................................126 Lecture No. 13 ........................................................................................................138 Lecture No. 14 ........................................................................................................149 Lecture No. 15 ........................................................................................................160 Lecture No. 16 ........................................................................................................173 Lecture No. 18 ........................................................................................................192 Lecture No. 19 ........................................................................................................201 Lecture No. 20 ........................................................................................................212 Lecture No. 21 ........................................................................................................223 Lecture No. 22 ........................................................................................................233 Lecture No. 23 ........................................................................................................250 Lecture No. 24 ........................................................................................................267 Lecture No. 25 ........................................................................................................279 Lecture No. 26 ........................................................................................................292 Lecture No. 27 ........................................................................................................304 Lecture No. 28 ........................................................................................................318 Lecture No. 29 ........................................................................................................331 Lecture No. 30 ........................................................................................................346 Lecture No. 31 ........................................................................................................358 Lecture No. 32 ........................................................................................................369 Lecture No. 33 ........................................................................................................378 Lecture No. 34 ........................................................................................................387 Lecture No. 35 ........................................................................................................394 Lecture No. 36 ........................................................................................................407 Lecture No. 37 ........................................................................................................420 Lecture No. 38 ........................................................................................................427 Lecture No. 39 ........................................................................................................434 Lecture No. 40 ........................................................................................................446 Lecture No. 41 ........................................................................................................453 Lecture No. 42 ........................................................................................................463 Lecture No. 43 ........................................................................................................472 Lecture No. 44 ........................................................................................................478 Lecture No. 45 ........................................................................................................489

2

CS301 – Data Structures ___________________________________________________________________

Data Structure

Lecture No. 01
Reading Material
Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1

Summary
Introduction to Data Structures Selecting a Data Structure Data Structure Philosophy Goals of this Course Array List data structure Welcome to the course of data structure. This is very important subject as the topics covered in it will be encountered by you again and again in the future courses. Due to its great applicability, this is usually called as the foundation course. You have already studied Introduction to programming using C and C++ and used some data structures. The focus of that course was on how to carry out programming with the use of C and C++ languages besides the resolution of different problems. In this course, we will continue problem solving and see that the organization of data in some cases is of immense importance. Therefore, the data will be stored in a special way so that the required result should be calculated as fast as possible. Following are the goals of this course: Prepare the students for (and is a pre-requisite for) the more advanced material students will encounter in later courses. Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, trees and graphs. Implement data structures in C++ You have already studied the dynamic arrays in the previous course. We will now discuss linked lists, stacks, queues, trees and graphs and try to resolve the problems with the help of these data structures. These structures will be implemented in C++ language. We will also do programming assignments to see the usage and importance of these structures.

3

CS301 – Data Structures ___________________________________________________________________

Introduction to Data Structures
Let’s discuss why we need data structures and what sort of problems can be solved with their use. Data structures help us to organize the data in the computer, resulting in more efficient programs. An efficient program executes faster and helps minimize the usage of resources like memory, disk. Computers are getting more powerful with the passage of time with the increase in CPU speed in GHz, availability of faster network and the maximization of disk space. Therefore people have started solving more and more complex problems. As computer applications are becoming complex, so there is need for more resources. This does not mean that we should buy a new computer to make the application execute faster. Our effort should be to ensue that the solution is achieved with the help of programming, data structures and algorithm. What does organizing the data mean? It means that the data should be arranged in a way that it is easily accessible. The data is inside the computer and we want to see it. We may also perform some calculations on it. Suppose the data contains some numbers and the programmer wants to calculate the average, standard deviation etc. May be we have a list of names and want to search a particular name in it. To solve such problems, data structures and algorithm are used. Sometimes you may realize that the application is too slow and taking more time. There are chances that it may be due to the data structure used, not due to the CPU speed and memory. We will see such examples. In the assignments, you will also check whether the data structure in the program is beneficial or not. You may have two data structures and try to decide which one is more suitable for the resolution of the problem. As discussed earlier, a solution is said to be efficient if it solves the problem within its resource constraints. What does it mean? In the computer, we have hard disk, memory and other hardware. Secondly we have time. Suppose you have some program that solves the problem but takes two months. It will be of no use. Usually, you don’t have this much time and cannot wait for two months. Suppose the data is too huge to be stored in disk. Here we have also the problem of resources. This means that we have to write programs considering the resources to achieve some solution as soon as possible. There is always cost associated with these resources. We may need a faster and better CPU which can be purchased. Sometimes, we may need to buy memory. As long as data structures and programs are concerned, you have to invest your own time for this. While working in a company, you will be paid for this. All these requirements including computer, your time and computer time will decide that the solution you have provided is suitable or not. If its advantages are not obtained, then either program or computer is not good. So the purchase of a faster computer, while studying this course, does not necessarily help us in the resolution of the problem. In the course of “Computer Architecture” you will see how the more efficient solutions can be prepared with the hardware. In this course, we will use the software i.e. data structures, algorithms and the recipes through which the computer problems may be resolved with a faster solution.

Selecting a Data Structure
How can we select the data structure needed to solve a problem? You have already studied where to use array and the size of array and when and where to use the 4

CS301 – Data Structures ___________________________________________________________________ pointers etc. First of all, we have to analyze the problem to determine the resource constraints that a solution must meet. Suppose, the data is so huge i.e. in Gega bytes (GBs) while the disc space available with us is just 200 Mega bytes. This problem can not be solved with programming. Rather, we will have to buy a new disk. Secondly, it is necessary to determine the basic operations that must be supported. Quantify the resource constraints for each operation. What does it mean? Suppose you have to insert the data in the computer or database and have to search some data item. Let’s take the example of telephone directory. Suppose there are eight million names in the directory. Now someone asks you about the name of some particular person. You want that this query should be answered as soon as possible. You may add or delete some data. It will be advisable to consider all these operations when you select some data structure. Finally select the data structure that meets these requirements the maximum. Without, sufficient experience, it will be difficult to determine which one is the best data structure. We can get the help from internet, books or from someone whom you know for already getting the problems solved. We may find a similar example and try to use it. After this course, you will be familiar with the data structures and algorithms that are used to solve the computer problems. Now you have selected the data structure. Suppose a programmer has inserted some data and wants to insert more data. This data will be inserted in the beginning of the existing data, or in the middle or in the end of the data. Let’s talk about the arrays and suppose you have an array of size hundred. Data may be lying in the first fifty locations of this array. Now you have to insert data in the start of this array. What will you do? You have to move the existing data (fifty locations) to the right so that we get space to insert new data. Other way round, there is no space in the start. Suppose you have to insert the data at 25th location. For this purpose, it is better to move the data from 26th to 50th locations; otherwise we will not have space to insert this new data at 25th location. Now we have to see whether the data can be deleted or not. Suppose you are asked to delete the data at 27th position. How can we do that? What will we do with the space created at 27th position? Thirdly, is all the data processed in some well-defined order or random access allowed? Again take the example of arrays. We can get the data from 0th position and traverse the array till its 50th position. Suppose we want to get the data, at first from 50th location and then from 13th. It means that there is no order or sequence. We want to access the data randomly. Random access means that we can’t say what will be the next position to get the data or insert the data.

Data Structure Philosophy
Let’s talk about the philosophy of data structure. Each data structure has costs and benefits. Any data structure used in your program will have some benefits. For this, you have to pay price. That can be computer resources or the time. Also keep in mind

5

CS301 – Data Structures ___________________________________________________________________ that you are solving this problem for some client. If the program is not efficient, the client will not buy it. In rare cases, a data structure may be better than another one in all situations. It means that you may think that the array is good enough for all the problems. Yet this is not necessary. In different situations, different data structures will be suitable. Sometimes you will realize that two different data structures are suitable for the problem. In such a case, you have to choose the one that is more appropriate. An important skill this course is going to lend to the students is use the data structure according to the situation. You will learn the programming in a way that it will be possible to replace the one data structure with the other one if it does not prove suitable. We will replace the data structure so that the rest of the program is not affected. You will also have to attain this skill as a good programmer. There are three basic things associated with data structures. A data structure requires: – space for each data item it stores – time to perform each basic operation – programming effort

Goals of this Course
Reinforce the concept that costs and benefits exist for every data structure. We will learn this with practice. Learn the commonly used data structures. These form a programmer's basic data structure “toolkit”. In the previous course, you have learned how to form a loop, functions, use of arrays, classes and how to write programs for different problems. In this course, you will make use of data structures and have a feeling that there is bag full of different data structures. In case of some problem, you will get a data structure from the toolkit and use some suitable data structure. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits of new data structures that you or others might develop. At times, you may have two suitable data structures for some problem. These can be tried one by one to adjudge which one is better one. How can you decide which data structure is better than other. Firstly, a programmer can do it by writing two programs using different data structure while solving the same problem. Now execute both data structures. One gives the result before the other. The data structure that gives results first is better than the other one. But sometimes, the data grows too large in the problem. Suppose we want to solve some problem having names and the data of names grows to10 lakhs (one million). Now when you run both programs, the second program runs faster. What does it mean? Is the data structure used in program one not correct? This is not true. The size of the data, being manipulated in the program can grow or shrink. You will also see that some data structures are good for small data while the others may suit to huge data. But the problem is how can we determine that the data in future will increase or decrease. We should have some way to take decision in this regard. In this course we will do some mathematical analysis and see which data structure is better one.

6

CS301 – Data Structures ___________________________________________________________________

Arrays
You have already studied about arrays and are well-versed with the techniques to utilize these data structures. Here we will discuss how arrays can be used to solve computer problems. Consider the following program: main( int argc, char** argv ) { int x[6]; int j; for(j = 0; j < 6; j++) x[j] = 2 * j; } We have declared an int array of six elements and initialized it in the loop. Let’s revise some of the array concepts. The declaration of array is as int x[6]; or float x[6]; or double x[6]; You have already done these in your programming assignments. An array is collection of cells of the same type. In the above program, we have array x of type int of six elements. We can only store integers in this array. We cannot put int in first location, float in second location and double in third location. What is x? x is a name of collection of items. Its individual items are numbered from zero to one less than array size. To access a cell, use the array name and an index as under: x[0], x[1], x[2], x[3], x[4], x[5] To manipulate the first element, we will use the index zero as x[0] and so on. The arrays look like in the memory as follows:

X[0] Array cells are contiguous in computer memory X[1] X[2] X[3] X[4] X[5] Array occupies contiguous memory area in the computer. In case of the above example, if some location is assigned to x[0], the next location can not contain data other than x[1]. The computer memory can be thought of as an array. It is a very big array. Suppose a computer has memory of 2MB, you can think it as an array of size 2

7

CS301 – Data Structures ___________________________________________________________________ million and the size of each item is 32 bits. You will study in detail about it in the computer organization, and Assembly language courses. In this array, we will put our programs, data and other things. In the above program, we have declared an array named x. ‘x’ is an array’s name but there is no variable x. ‘x’ is not an lvalue. If some variable can be written on the lefthand side of an assignment statement, this is lvalue variable. It means it has some memory associated with it and some value can be assigned to it. For example, if we have the code int a, b; it can be written as b = 2; it means that put 2 in the memory location named b. We can also write as a = b; it means whatever b has assign it to a, that is a copy operation. If we write as a = 5; it means put the number 5 in the memory location which is named as a. But we cannot write 2 = a; that is to put at number 2 what ever the value of a is. Why can’t we do that? Number 2 is a constant. If we allow assignment to constants what will happen? Suppose ‘a’ has the value number 3. Now we assigned number 2 the number 3 i.e. all the number 2 will become number 3 and the result of 2 + 2 will become 6. Therefore it is not allowed. ‘x’ is a name of array and not an lvalue. So it cannot be used on the left hand side in an assignment statement. Consider the following statements int x[6]; int n; x[0] = 5; x[1] = 2; x = 3; //not allowed x = a + b; // not allowed x = &n; // not allowed In the above code snippet, we have declared an array x of int. Now we can assign values to the elements of x as x[0] = 5 or x[1] = 2 and so on. The last three statements are not allowed. What does the statement x = 3; mean? As x is a name of array and this statement is not clear, what we are trying to do here? Are we trying to assign 3 to each element of the array? This statement is not clear. Resultantly, it can not be allowed. The statement x = a + b is also not allowed. There is nothing wrong with a + b. But we cannot assign the sum of values of a and b to x. In the statement x = &n, we are trying to assign the memory address of n to x which is not allowed. The reason is the name x is not lvalue and we cannot assign any value to it. For understanding purposes, consider x as a constant. Its name or memory location can not be changed. This is a collective name for six locations. We can access these locations as x[0], x[1] up to x[5]. This is the way arrays are manipulated. Sometimes, you would like to use an array data structure but may lack the information about the size of the array at compile time. Take the example of telephone directory. You have to store one lakh (100,000) names in an array. But you never know that the number of entries may get double or decline in future. Similarly, you can not say that the total population of the country is one crore (10 million) and declare an array of one crore names. You can use one lakh locations now and remaining will be used as the need arrives. But this is not a good way of using the computer resources. You have declared a very big array while using a very small chunk of it. Thus the remaining space goes waste which can, otherwise, be used by some other programs.

8

CS301 – Data Structures ___________________________________________________________________ We will see what can be the possible solution of this problem? Suppose you need an integer array of size n after the execution of the program. We have studied that if it is known at the execution of the program that an array of size 20 or 30 is needed, it is allocated dynamically. The programming statement is as follows: int* y = new int[20]; It means we are requesting computer to find twenty memory locations. On finding it, the computer will give the address of first location to the programmer which will be stored in y. Arrays locations are contiguous i.e. these are adjacent. These twenty locations will be contiguous, meaning that they will be neighbors to each other. Now y has become an array and we can say y[0] =1 or y[5] = 15. Here y is an lvalue. Being a pointer, it is a variable where we can store the address of some variable. When we said int* y = new int[20]; the new returns the memory address of first of the twenty locations and we store that address into y. As y is a pointer variable so it can be used on the left-hand side. We can write it as: y = &x[0]; In the above statement, we get the address of the fist location of the array x and store it in y. As y is lvalue, so it can be used on left hand side. This means that the above statement is correct. y = x; Similarly, the statement y = x is also correct. x is an array of six elements that holds the address of the first element. But we cannot change this address. However we can get that address and store it in some other variable. As y is a pointer variable and lvalue so the above operation is legal. We have dynamically allocated the memory for the array. This memory, after the use, can be released so that other programs can use it. We can use the delete keyword to release the memory. The syntax is: delete[ ] y; We are releasing the memory, making it available for use by other programs. We will not do it in case of x array, as ‘new’ was not used for its creation. So it is not our responsibility to delete x.

List data structure
This is a new data structure for you. The List data structure is among the most generic of data structures. In daily life, we use shopping list, groceries list, list of people to invite to a dinner, list of presents to give etc. In this course, we will see how we use lists in programming. A list is the collection of items of the same type (grocery items, integers, names). The data in arrays are also of same type. When we say int x[6]; it means that only the integers can be stored in it. The same is true for list. The data which we store in list should be of same nature. The items, or elements of the list, are stored in some

9

CS301 – Data Structures ___________________________________________________________________ particular order. What does this mean? Suppose in the list, you have the fruit first which are also in some order. You may have names in some alphabetical order i.e. the names which starts with A should come first followed by the name starting with B and so on. The order will be reserved when you enter data in the list. It is possible to insert new elements at various positions in the list and remove any element of the list. You have done the same thing while dealing with arrays. You enter the data in the array, delete data from the array. Sometimes the array size grows and at times, it is reduced. We will do this with the lists too. List is a set of elements in a linear order. Suppose we have four names a1, a2, a3, a4 and their order is as (a3, a1, a2, a4) i.e. a3, is the first element, a1 is the second element, and so on. We want to maintain that order in the list when data is stored in the list. We don’t want to disturb this order. The order is important here; this is not just a random collection of elements but an ordered one. Sometimes, this order is due to sorting i.e. the things that start with A come first. At occasions, the order may be due to the importance of the data items. We will discuss this in detail while dealing with the examples. Now we will see what kind of operations a programmer performs with a list data structure. Following long list of operations may help you understand the things in a comprehensive manner. Operation Name createList() copy() clear(); insert(X, ?) remove(?) get(?) update(X, ?) find(X) length() Description Create a new list (presumably empty) Set one list to be a copy of another Clear a list (remove all elements) Insert element X at a particular position in the list Remove element at some position in the list Get element at a given position Replace the element at a given position with X Determine if the element X is in the list Returns the length of the list.

createList() is a function which creates a new list. For example to create an array, we use int x[6] or int* y = new int[20]; we need similar functionality in lists too. The copy() function will create a copy of a list. The function clear() will remove all the elements from a list. We want to insert a new element in the list, we also have to tell where to put it in the list. For this purpose insert(X, position) function is used. Similarly the function remove(position) will remove the element at position. To get an element from the list get(position) function is used which will return the element at position. To replace an element in the list at some position the function update(X, position) is used. The function find(X) will search X in the list. The function length() tells us about the number of elements in the list. We need to know what is meant by “particular position” we have used “?” for this in the above table. There are two possibilities: Use the actual index of element: i.e. insert it after element 3, get element

10

CS301 – Data Structures ___________________________________________________________________ number 6. This approach is used with arrays Use a “current” marker or pointer to refer to a particular position in the list. The first option is used in the data structures like arrays. When we have to manipulate the arrays, we use index like x[3], x[6]. In the second option we do not use first, second etc for position but say wherever is the current pointer. Just think of a pointer in the list that we can move forward or backward. When we say get, insert or update while using the current pointer, it means that wherever is the current pointer, get data from that position, insert data after that position or update the data at that position. In this case, we need not to use numbers. But it is our responsibility that current pointer is used in a proper way. If we use the “current” marker, the following four methods would be useful: Functions Description start() Moves the “current” pointer to the very first element tail() Moves the “current” pointer to the very last element next() Move the current position forward one element back() Move the current position backward one element In the next lecture, we will discuss the implementation of the list data structure and write the functions discussed today, in C++ language.

11

CS301 – Data Structures ___________________________________________________________________

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 Analysis Of Array List List Using Linked Memory Linked List

2) 3) 4)

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 12

CS301 – Data Structures ___________________________________________________________________ manner where the current position is 3. A 2 1 6 2 8 3 7 4 1 5 current 3 size 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 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 1 6 2 8 3 4 7 5 1 current 3 size 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. A 2 1 6 2 8 3 9 4 7 5 1 6 current 4 size 6

We have moved the current position to 4 while increasing the size to 6. The size shows that the elements in the list. Where as the size of the array is different that we have defined already to a fixed length, which may be 100, 200 or even greater. next Method Now let’s see another method, called ‘next’. We have talked that the next method moves the current position one position forward. In this method, we do not add a new element to the list but simply move the pointer one element ahead. This method is required while employing the list in our program and manipulating it according to the requirement. There is also an array to store the list in it. We also have two variablescurrent and size to store the position of current pointer and the number of elements in the list. By looking on the values of these variables, we can find the state of the list

13

CS301 – Data Structures ___________________________________________________________________ i.e. how many elements are in the list and at what position the current pointer is. The method next is used to know about the boundary conditions of the list i.e. the array being used by us to implement the list. To understand the boundary conditions, we can take the example of an array of size 100 to implement the list. Here, 100 elements are added to the array. Let’s see what happens when we want to add 101st element to the array? We used to move the current position by next method and reached the 100th position. Now, in case of moving the pointer to the next position (i.e. 101st), there will be an error as the size of the array is 100, having no position after this point. Similarly if we move the pointer backward and reach at the first position regardless that the index is 0 or 1. But what will happen if we want to move backward from the first position? These situations are known as boundary conditions and need attention during the process of writing programs when we write the code to use the list. We will take care of these things while implementing the list in C++ programs. remove Method We have seen that the add method adds an element in the list. Now we are going to discuss the remove method. The remove method removes the element residing at the current position. The removal of the element will be carried out as follows. Suppose there are 6 elements (2, 6, 8, 9, 7, 1) in the list. The current pointer is pointing to the position 5 that has the value 7. We remove the element, making the current position empty. The size of the list will become 5. This is represented in the following figure.

A

2 1

6 2

8 3

9 4 5

1 6

current 5

size 6 5

We fill in the blank position left by the removal of 7 by shifting the values on the right of position 5 to the left by one space. This means that we shift the remaining elements on the right hand side of the current position one place to the left so that the element next to the removed element (i.e. 1) takes its place (the fifth position) and becomes the current position element. We do not change the current pointer that is still pointing to the position 5. Thus the current pointer remains pointing to the position 5 despite the fact that there is now element 1 at this place instead of 7. Thus in the remove method, when we remove an element, the element next to it on the right hand side comes at its place and the remaining are also shifted one place to the right. This step is represented by the following figure.

A

2 1

6 2

8 3

9 4

1 5

current 5

size 5

find Method Now lets talk about a function, used to find a specific element in the array. The find (x) function is used to find a specific element in the array. We pass the element, which is to be found, as an argument to the find function. This function then traverses the array until the specific element is found. If the element is found, this function sets the

14

CS301 – Data Structures ___________________________________________________________________ current position to it and returns 1 i.e. true. On the other hand, if the element is not found, the function returns 0 i.e. false. This indicates that the element was not found. Following is the code of this find(x) function in C++. int find (int x) { int j ; for (j = 1; j < size + 1; j++ ) if (A[j] == x ) break ; if ( j < size + 1) // x is found { current = j ; //current points to the position where x found return 1 ; // return true } return 0 ; //return false, x is not found } In the above code, we execute a for loop to traverse the array. The number of execution of this loop is equal to the size of the list. This for loop gets terminated when the value of loop variable (j) increases from the size of the list. However we terminate the loop with the break statement if the element is found at a position. When the control comes out from the loop, we check the value of j. If the value of j is less than the size of the array, it means that the loop was terminated by the break statement. We use the break statement when we find the required element (x) in the list. The execution of break statement shows that the required element was found at the position equal to the value of j. So the program sets the current position to j and comes out the function by returning 1 (i.e. true). If the value of j is greater than the size of the array, it means that the whole array has traversed and the required element is not found. So we simply return 0 (i.e. false) and come out of the function. Other Methods There are some other methods to implement the list using an array. These methods are very simple, which perform their task just in one step (i.e. in one statement). There is a get() method , used to get the element from the current position in the array. The syntax of this function is of one line and is as under return A[current] ; This statement returns the element to which the current is pointing to (i.e. the current position) in the list A. Another function is update(x). This method is used to change (set) the value at the current position. A value is passed to this method as an argument. It puts that value at the current position. The following statement in this method carries out this process. A [current] = x ; Then there is a method length( ).This method returns the size of the list. The syntax of this method is

15

CS301 – Data Structures ___________________________________________________________________ return size ; You may notice here that we are returning the size of the list and not the size of the array being used internally to implement the list. This size is the number of the elements of the list, stored in the array. The back() method decreases the value of variable current by 1. In other words, it moves the current position one element backward. This is done by writing the statement. current -- ; The -- is a decrement operator in C++ that decreases the value of the operand by one. The above statement can also be written as current = current -1 ; The start() method sets the current position to the first element of the list. We know that the index of the array starts from 0 but we use the index 1 for the starting position. We do not use the index zero. So we set the current position to the first element by writing current = 1 ; Similarly, the end() method sets the current position to the last element of the list i.e. size. So we write current = size ;

Analysis of Array List
Now we analyze the implementation of the list while using an array internally. We analyze different methods used for the implementation of the list. We try to see the level upto which these are efficient in terms of CPU’s time consumption. Time is the major factor to see the efficiency of a program. Add First of all, we have talked about the add method. When we add an element to the list, every element is moved to the right of the current position to make space for the new element. So, if the current position is the start of the list and we want to add an element in the beginning, we have to shift all the elements of the list to the right one place. This is the worst case of adding an element to the list. Suppose if the size of the list is 10000 or 20000, we have to do the shift operation for all of these 10000 or 20000 elements. Normally, it is done by shifting of elements with the use of a for loop. This operation takes much time of the CPU and thus it is not a good practice to add an element at the beginning of a list. On the other hand, if we add an element at the end of the list, it can be done by carrying out ‘no shift operation’. It is the best case of adding an element to the list. However, normally we may have to move half of the elements. The usage of add method is the matter warranting special care at the time of implementation of the list in our program. To provide the interface of the list,

16

CS301 – Data Structures ___________________________________________________________________ we just define these methods. Remove When we remove an element at the current position in the list, its space gets empty. The current pointer remains at the same position. To fill this space, we shift the elements on the right of this empty space one place to the left. If we remove an element from the beginning of the list, then we have to shift the entire remaining elements one place to the left. Suppose there is a large number of elements, say 10000 or 20000, in the list. We remove the first element from the list. Now to fill this space, the remaining elements are shifted (that is a large number). Shifting such a large number of elements is time consuming process. The CPU takes time to execute the for loop that performs this shift operation. Thus to remove an element at the beginning of the list is the worst case of remove method. However it is very easy to remove an element at the end of the list. In average cases of the remove method we expect to shift half of the elements. This average does not mean that in most of the cases, you will have to shift half the elements. It is just the average. We may have to shift all the elements in one operation (if we remove at the beginning) and in the second operation, we have to shift no element (if we remove at the end). Similarly, in certain operations, we have to shift just 10, 15 elements. Find We have discussed that the find method takes an element and traverses the list to find that element. The worst case of the find method is that it has to search the entire list from beginning to end. So, it finds the element at the end of the array or the element is not found. On average the find method searches at most half the list. The other methods get (), length () etc are one-step methods. They carry out their operation in one instruction. There is no need of any loop or other programming structures to perform the task. The get() method gets the value from the specified position just in one step. Similarly the update() method sets a value at the specific position just in one-step. The length () method returns the value of the size of the list. The other methods back(), start() and end() also perform their tasks only in one step.

List using Linked Memory
We have seen the implementation of the list with the use of an array. Now we will discuss the implementation of the list while using linked memory. In an array, the memory cells of the array are linked with each other. It means that the memory of the array is contiguous. In an array, it is impossible that one element of the array is located at a memory location while the other element is located somewhere far from it in the memory. It is located in very next location in the memory. It is a property of the array that its elements are placed together with one another in the memory. Moreover, when we have declared the size of the array, it is not possible to increase or decrease it during the execution of the program. If we need more elements to store in the array, there is need of changing its size in the declaration. We have to compile the program again before executing it. Now array will be of the new size. But what happens if we again need to store more elements? We will change the code of our program to change the declaration of the array while recompiling it. Suppose we have used the dynamic memory allocation and created an array of 100

17

CS301 – Data Structures ___________________________________________________________________ elements with the use of new operator. In case of need of 200 elements, we will release this array and allocate a new array of 200 elements. Before releasing the previous array, it will wise to copy its elements to the new array so that it does not lose any information. Now this new array is in ‘ready for use’ position. Thus the procedure of creating a new array is not an easy task. To avoid such problems, usually faced by the programmers while using an array, there is need of using linked memory in which the various cells of memory, are not located continuously. In this process, each cell of the memory not only contains the value of the element but also the information where the next element of the list is residing in the memory. It is not necessary that the next element is at the next location in the memory. It may be anywhere in the memory. We have to keep a track of it. Thus, in this way, the first element must explicitly have the information about the location of the second element. Similarly, the second element must know where the third element is located and the third should know the position of the fourth element and so on. Thus, each cell (space) of the list will provide the value of the element along with the information about where the next element is in the memory. This information of the next element is accomplished by holding the memory address of the next element. The memory address can be understood as the index of the array. As in case of an array, we can access an element in the array by its index. Similarly, we can access a memory location by using its address, normally called memory address.

Linked List
For the utilization of the concept of linked memory, we usually define a structure, called linked list. To form a linked list, at first, we define a node. A node comprises two fields. i.e. the object field that holds the actual list element and the next that holds the starting location of the next node.

object

next

A chain of these nodes forms a linked list. Now let’s consider our previous list, used with an array i.e. 2, 6, 8, 7, 1. Following is the figure which represents the list stored as a linked list. Head

2

6

8

7

1

size = 5

current This diagram just represents the linked list. In the memory, different nodes may occur at different locations but the next part of each node contains the address of the next node. Thus it forms a chain of nodes which we call a linked list. While using an array we knew that the array started from index 1that means the first

18

CS301 – Data Structures ___________________________________________________________________ element of the list is at index 1. Similarly in the linked list we need to know the starting point of the list. For this purpose, we have a pointer head that points to the first node of the list. If we don’t use head, it will not be possible to know the starting position of the list. We also have a pointer current to point to the current node of the list. We need this pointer to add or remove current node from the list. Here in the linked list, the current is a pointer and not an index as we used while using an array. The next field of the last node points to nothing .It is the end of the list. We place the memory address NULL in the last node. NULL is an invalid address and is inaccessible. Now again consider the list 2, 6, 8, 7, 1. The previous figure represents this list as a linked list. In this linked list, the head points to 2, 2 points to 6, 6 points to 8, 8 points to 7 and 7 points to 1. Moreover we have the current position at element 8. This linked list is stored in the memory. The following diagram depicts the process through which this linked list is stored in the memory.

19

CS301 – Data Structures ___________________________________________________________________ 1051 1052 current 1053 1054 1055 1056 1057 1058 1059 1060 1061 head 1062 1063 1064 1065 We can see in the figure that each memory location has an address. Normally in programming, we access the memory locations by some variable names. These variable names are alias for these locations and are like labels that are put to these memory locations. We use head and current variable names instead of using the memory address in numbers for starting and the current nodes. In the figure, we see that head is the name of the memory location 1062 and the name current is used for the memory address 1053. The head holds the address 1054 and the element 2, the first one in the list, is stored in the location 1054. Similarly current holds the address 1063 where the element 8 is stored which is our current position in the list. In the diagram, two memory locations comprise a node. So we see that the location 1054 holds the element 2 while the next location 1055 holds the address of the memory location (1051) where the next element of the list (i.e. 6) is stored. Similarly the next part of the node that has value 6 holds the memory address of the location occupied by the next element (i.e. 8) of the list. The other nodes are structured in a similar fashion. Thus, by knowing the address of the next element we can traverse the whole list. 1 0 1054 8 1057 2 1051 7 1060 6 1063

20

CS301 – Data Structures ___________________________________________________________________

Data Structures Lecture No. 03
Reading Material
Data Structures and algorithm analysis in C++ Chapter. 3 3.2.2, 3.2.3, 3.2.5

Summary
Linked List inside Computer Memory Linked List Operations Linked List Using C++ Example Program

In the previous lectures, we used an array to construct a list data structure and observed the limitation that array being of fixed size can only store a fixed number of elements. Therefore, no more elements can be stored after the size of the array is reached. In order to resolve this, we adopted a new data structure called linked list. We started discussing, how linked lists are stored in computer memory and how memory chains are formed.

21

CS301 – Data Structures ___________________________________________________________________

Linked List inside Computer Memory
1051 1052 currentent 1053 1054 1055 head 1056 1057 2 6 8 7 1 1058 1059 1060 1061 headent 1062 1063 1064 1065 Fig 1. Linked list in memory There are two parts of this figure. On the left is the linked list chain that is actually the conceptual view of the linked list and on the right is the linked list inside the computer memory. Right part is a snapshot of the computer memory with memory addresses from 1051 to 1065. The head pointer is pointing to the first element in the linked list. Note that head itself is present in the memory at address 1062. It is actually a pointer containing the memory address 1054. Each node in the above linked list has two parts i.e. the data part of the node and the pointer to the next node. The first node of the linked list pointed by the head pointer is stored at memory address 1054. We can see the data element 2 present at that address. The second part of the first node contains the memory address 1051. So the second linked list’s node starts at memory address 1051. We can use its pointer part to reach the third node of the list and in this way, we can traverse the whole list. The last node contains 1 in its data part and 0 in its pointer part. 0 indicates that it is not pointing to any node and it is the last node of the linked list. 1 0 1054 8 1057 7 1060 6 1063 1063 2 1051

Linked List Operations
The linked list data structure provides operations to work on the nodes inside the list. The first operation we are going to discuss here is to create a new node in the memory. The Add(9) is used to create a new node in the memory at the current position to hold ‘9’. You must remember while working with arrays, to add an 22

CS301 – Data Structures ___________________________________________________________________ element at the current position that all the elements after the current position were shifted to the right and then the element was added to the empty slot. Here, we are talking about the internal representation of the list using linked list. Its interface will remain the same as in case of arrays. We can create a new node in the following manner in the add() operation of the linked list with code in C++: Node * newNode = new Node(9); The first part of the statement that is on the left of the assignment is declaring a variable pointer of type Node. It may also be written as Node * newNode. On the right of this statement, the new operator is used to create a new Node object as new Node(9). This is one way in C++ to create objects of classes. The name of the class is provided with the new operator that causes the constructor of the class to be called. The constructor of a class has the same name as the class and as this a function, parameters can also be passed to it. In this case, the constructor of the Node class is called and ‘9’ is passed to it as an int parameter. Hence, the whole statement means: “Call the constructor of the Node class and pass it ‘9’ as a parameter. After constructing the object in memory, give me the starting memory address of the object. That address will be stored in the pointer variable newNode.” To create an object of int type in the same manner, we can write as: int * i = new int ; Previously, we used the same technique to allocate memory for an array of ints as: int * i = new int [10] ; Now after the node has been created, how the node is fit into the chain of the linked list. Fig 2. Insertion of new Node into the linked list 2 6 8 7 1

current 3 9 newNode

2

1

In the above figure, there is a linked list that contains five nodes with data elements as 2, 6, 8, 7 and 1. The current pointer is pointing to the node with element as 8. We want to insert a new node with data element 9. This new node will be inserted at the current position (the position where the current pointer is pointing to). This insertion operation is performed in a step by step fashion. - The first step is to point next pointer of the new node (with data element as 9) to 23

CS301 – Data Structures ___________________________________________________________________ the node with data element as 7. The second step is to point the next pointer of the node with data element 8 to the node the new node with data element 9. - The third step is to change the current pointer to point to the new node.
-

Now, the updated linked list has nodes with data elements as 2, 6, 8, 9, 7 and 1. The list size has become 6.

Linked List Using C++
/* The Node class */ class Node { public: int get() { return object; }; void set(int object) { this->object = object; }; Node * getNext() { return nextNode; }; void setNext(Node * nextNode) { this->nextNode = nextNode; }; private: int object; Node * nextNode; };

Whenever, we write a class, it begins with the word class followed by the class-name and the body of the class enclosed within curly braces. In the body, we write its public variables, methods and then private variables and methods, this is normally the sequence. If there is no code to write inside the constructor function of a class, we need not to declare it ourselves as the compiler automatically creates a default constructor for us. Similarly, if there is nothing to be done by the destructor of the class, it will be better not to write it explicitly. Rather, the compiler writes it automatically. Remember, the default constructor and destructor do nothing as these are the function without any code statements inside. Let’s start with the data members first. These are given at the bottom of the class body with the scope mentioned as private. These data members are actually two parts of a linked list’s node. First variable is object of type int, present there to store the data part of the node. The second variable is nextNode, which is a pointer to an object of type Node. It has the address of the next element of the linked list. The very first public function given at the top is get(). We have written its code within the class Node. It returns back the value of the variable object i.e. of the type of int. When we write class in C++, normally, we make two files (.h and .cpp) for a class. The .h file contains the declarations of public and private members of that class. The public methods are essentially the interface of the class to be employed by the users of this class. The .cpp file contains the implementation for the class methods that has the actual code. This is usually the way that we write two files for one class. But this is

24

CS301 – Data Structures ___________________________________________________________________ not mandatory. In the code given above, we have only one file .cpp, instead of separating into two files. As the class methods are very small, so their code is written within the body of the class. This facilitates us in carrying on discussion. Thus instead of talking about two files, we will only refer to one file. On the other hand, compiler takes these functions differently that are called inline functions. The compiler replaces the code of these inline functions wherever the call to them is made. The second method in the above-mentioned class is set() that accepts a parameter of type int while returning back nothing. The accepted parameter is assigned to the internal data member object. Notice the use of this pointer while assigning the value to the internal data member. It is used whenever an object wants to talk to its own members. The next method is getNext() which returns a pointer to an object of type Node lying somewhere in the memory. It returns nextNode i.e. a pointer to an object of type Node. As discussed above, nextNode contains the address of next node in the linked list. The last method of the class is setNext() that accepts a pointer of type Node, further assigned to nextNode data member of the object. This method is used to connect the next node of the linked list with the current object. It is passed an address of the next node in the linked list. Let’s discuss a little bit about classes. A very good analogy of a class is a factory. Think about a car factory. On the placement of order, it provides us with the number of vehicles we ordered for. Similarly, you can see number of other factories in your daily-life that manufacture the specific products. Let’s take this analogy in C++ language. Suppose, we want to make a factory in C++. By the way, what is our Node class? It is actually a factory that creates nodes. When we want to make a new node, a new operator is used. By using new operator with the Node class, actually, we send an order to Node factory, to make as many as nodes for us. So we have a good analogy, to think about a class as a factory. The products that are made by the factory have their own characteristics. For example, a car made by an automobile factory has an engine, wheels, steering and seats etc. These variables inside a class are called state variables. Now the kinds of operations this car can do are the methods of its class. A car can be driven, engine can be started, gears can be shifted and an accelerator can be pressed to run it faster. Similarly, the Node class creates nodes, where every node has two-state variables i.e. object and nextNode. We have already seen its operations in the above code. We use new to create new object or an array of new objects, stored in memory. Let’s see the code below. /* List class */ #include #include "Node.cpp" class List { public:

25

CS301 – Data Structures ___________________________________________________________________ // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }

We are creating a list factory here employed to create list objects. Remember the list operations; add, remove, next, back and start etc. Let’s see the above class declaration code in detail. There are two include statements at the start. The first line is to include a standard library stdlib.h while the second line includes the Node class file Node.cpp. This Node class is used to create nodes that form a List object. So this List factory will order Node class to create new nodes. The List class itself carries out the chain management of these Node objects. We have written our own constructor of List class as the default constructor is not sufficient enough to serve the purpose. The List constructor is parameterless. The very first step it is doing internally is that it is asking Node class to create a new node and assigning the starting address of the new Node’s object to the headNode data member. In the second statement, we are calling setNext() method of the Node class for the object pointed to by the headNode pointer. This call is to set the nextNode data member to NULL, i.e. Node’s object pointed to by the headNode pointer is not pointing to any further Node. The next statement is to set the currentNode pointer to NULL. So at the moment, we have initialized the currentNode pointer to NULL that is not pointing to any Node object. The next statement is to initialize the size data member to 0 indicating that there is no node present in the list. All this processing is done inside the constructor of List class, as we want all this done when a list object is created. Considering the analogy of car factory, the constructor function can perform certain tasks: The oil is poured into the engine, the tyres are filled-in with air etc. Let’s see the add method of the List class: /* add() class method */ void add (int addObject) { 1. Node * newNode = new Node(); 2. newNode->set(addObject); 3. if( currentNode != NULL ) 4. { 5. newNode->setNext(currentNode->getNext()); 6. currentNode->setNext( newNode ); 7. lastCurrentNode = currentNode; 8. currentNode = newNode; 9. } 10. else 11. { 12. newNode->setNext(NULL);

26

CS301 – Data Structures ___________________________________________________________________ 13. headNode->setNext(newNode); 14. lastCurrentNode = headNode; 15. currentNode = newNode; 16. } 17. size ++; }

The interface or signatures of add() method is similar to the one discussed in case of an array. This method takes the object to be added as a parameter. The implementation of this add() method is a bit longer as the method is being implemented for linked list. In the first statement, a new Node object is created with its address stored in the newNode pointer variable. The second statement is to call set() method of the Node object pointed to by the newNode pointer. You can note the way the method is called. A pointer variable is at the left most side then an arrow sign (->), then the name of the method with appropriate arguments within parenthesis. It is followed by the if-statement that checks the currentNode is not NULL to perform certain operations inside the if-code block. Inside the if-statement, at line 5, the nextNode pointer of the new node is being set to the nextNode of the object pointed to by the currentNode pointer. In order to understand the statements given in this code properly, consider the fig 2 above, where we added a node in the linked list. We have done step 1 at line5. At line 6, we are performing the second step by setting the newNode in the nextNode pointer of the object pointed to by the currentNode. At line 7, we are saving the current position (address) of the currentNode pointer in the pointer variable lastCurrentNode, which might be useful for backward traversing. Although, the fig 1 (left part) indicates movement in one direction from left to right but the lastCurrentNode pointer node can be used by the back() member function to traverse one position back from right to left. At line 8, the currentNode pointer is assigned the address of the object pointed to by newNode. This way, a new node is added in already existent linked list. Line 10 is start of the else part of if-statement. This is executed if the currentNode is NULL. It means that there is no node present in the list previously and first node is going to be added. At line 12, we are setting the nextNode pointer of the object pointed to by newNode pointer. The nextNode is being set to NULL by calling the setNext() method. Then at line 13, we point the head pointer (headNode) to this new node pointed to by newNode pointer. Note that headNode is pointing to a node that is there despite the fact that the size of the linked list is 0. Actually, we have allocated a Node object for headNode pointer. Although, we don’t need a Node object here, yet it will be helpful when we perform other operations like remove() and find(). At line 14, the headNode address is being assigned to lastCurrentNode. At line 15, currentNode pointer is assigned the address of newNode. At the end i.e. at line 17, the size of the list is incremented by 1.

27

CS301 – Data Structures ___________________________________________________________________

List list;

headNode

size =

currentNode list.add(2); headNode 2 lastCurrentNode currentNode size = size =

list.add(6);

headNodee

2

6

lastCurrentNode Fig 3. Add operation of linked list Following is the crux of this add() operation : Firstly, it will make a new node by calling Node class constructor. Insert the value e.g. 2. of the node into the node by calling the set method. Now if the list already exists (has some elements inside or its size is non-zero), it will insert the node after the current position. If the list does not already exist, this node is added as the first element inside the list. Let’s try to add few more elements into the above linked list in the figure. The following are the lines of code to be executed to add nodes with values 8, 7 and 1 into the linked list. list.add(8); list.add(7); list.add(1); currentNode

2

6

8

7

1

size = 5

headNode Fig 4. More Nodes added into linked list

lastCurrentNode

Now we will see the remaining methods of the linked list. The get() method of the

28

CS301 – Data Structures ___________________________________________________________________ List class is given below /* get() class method */ int get() { if (currentNode != NULL) return currentNode->get(); } This method firstly confirms that the currentNode pointer is not NULL. If it is not NULL, then it must be pointing to some Node object as inside the constructor of the List class, we have initialized this pointer variable to NULL. That indicates that the currentNode is NULL when there is no element inside the list. However, when a Node object is added into it, it starts pointing to it. So, this get() returns the value of the node pointed to by the currentNode pointer. Further, we have another method given below: /* next() class method */ bool next() { 1. if (currentNode == NULL) return false; 2. 3. lastCurrentNode = currentNode; 4. currentNode = currentNode->getNext(); 5. return true; };

This is next() method, used to advance the currentNode pointer to the next node inside the linked list. At line 1, the currentNode is being checked to confirm that there are some elements present in the list to advance further. At line 1, the method is returning false if there is no element present in the list. At line 3, it is storing the value of the currentNode pointer into the lastCurrentNode. At line 4, currentNode is calling the getNext() method to get the address of next node to be stored in the currentNode pointer to advance the currentNode pointer to the next element. At line 5, it returns true indicating the method is successful in moving to the next node.

Example Program
Given below is the full source code of the example program. You can copy, paste and compile it right away. In order to understand the linked list concept fully, it is highly desirable that you understand and practice with the below code. #include #include /* The Node class */ class Node { public: int get() { return object; };

29

CS301 – Data Structures ___________________________________________________________________ void set(int object) { this->object = object; }; Node * getNext() { return nextNode; }; void setNext(Node * nextNode) { this->nextNode = nextNode; }; private: int object; Node * nextNode; }; /* The List class */ class List { public: List(); void add (int addObject); int get(); bool next(); friend void traverse(List list); friend List addNodes(); private: int size; Node * headNode; Node * currentNode; Node * lastCurrentNode; }; /* Constructor */ List::List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; lastCurrentNode = NULL; size = 0; } /* add() class method */ void List::add (int addObject) { Node * newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ) { newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode;

30

CS301 – Data Structures ___________________________________________________________________ } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; } size ++; } /* get() class method */ int List::get() { if (currentNode != NULL) return currentNode->get(); } /* next() class method */ bool List::next() { if (currentNode == NULL) return false; lastCurrentNode = currentNode; currentNode = currentNode->getNext(); if (currentNode == NULL || size == 0) return false; else return true; } /* Friend function to traverse linked list */ void traverse(List list) { Node* savedCurrentNode = list.currentNode; list.currentNode = list.headNode; for(int i = 1; list.next(); i++) { cout getNext()) will set the next pointer of the node pointed by the lastCurrentNode to the node with value 8. So the next pointer of the node with value 2 is pointing to the node with value 8. currentNode headNode Step1 2 lastCurrentNode 6 8 7 1 Size = 5 6 8 7 1 Size = 5

You see that the next pointer of the node having data element 2 contains the address of the node having data element 8. The node with value 6 has been disconnected from the chain while the node with value 2 is connected to the node with the value 8. The code of the next step is: delete currentNode; You already know, in case of allocation of the memory with the help of the new keyword, the delete statement releases this memory which returns the memory to the system. Pictorially it can be represented as:

35

CS301 – Data Structures ___________________________________________________________________ currentNode headNode Step1 2 Step2 lastCurrentNode 8 7 1 Size = 5

In the next step, we have moved the currentNode to point the next node. The code is: currentNode = lastCurrentNode->getNext(); In the fourth step, the size of the list has been reduced by 1 after the deletion of one node i.e. size--; Step3 currentNode headNode Step1 2 Step2 lastCurrentNode 8 7 1 Step4 Size = 4

The next method is length() that simply returns the size of the list. The code is as follows: // returns the size of the list int length() { return size; }; The private data members of the list are: private: int size; // contains the size of the list Node *headNode; // points to the first node of the list Node *currentNode, // current node Node *lastCurrentNode; // last current node

36

CS301 – Data Structures ___________________________________________________________________ The list class completed just now, can be termed as list factory. We have included all the required methods in it. We may employ more methods if required. A programmer can get the size of the list, add or remove nodes in it besides moving the pointers.

Example of list usage
Now let’s see how we use the link list. Here is an example showing the use of list: /* A simple example showing the use of link list */ #include #include #include "List.cpp" // This contains the definition of List class // main method int main(int argc, char *argv[]) { List list; // creating a list object // adding values to the list list.add(5); list.add(13); list.add(4); list.add(8); list.add(24); list.add(48); list.add(12); // calling the start method of the list list.start(); // printing all the elements of the list while (list.next()) cout setNext( current->getNext() ); current

head

2

6

8

7

1

size=5

newNode

9

1

In the next step, a programmer points the prevNode of the newNode to the node with value 6. newNode->setprev( current );

41

CS301 – Data Structures ___________________________________________________________________ current

head

2 2 newNode

6

8

7

1

size=5

9

1

In the third step, we will set the previous node with value 8 to point to the newNode. (current->getNext())->setPrev(newNode); current

head

2 2 newNode

6 3 9

8

7

1

size=5

1

Now the prevNode of the node with value 8 is pointing to the node with value 9. In the fourth step, the nextNode of the node with value 6 is pointing to the newNode i.e. the node with value 9. Point the current to the newNode and add one to the size of the list. current->setNext( newNode ); current = newNode; size++;

head

2 2 newNode

6 4 9 current 3

8

7

1

size=6

1

Now the newNode has been inserted between node with value 6 and node with value 8.

42

CS301 – Data Structures ___________________________________________________________________

Circularly-linked lists
Let’s talk about circularly linked list. The next field in the last node in a singly-linked list is set to NULL. The same is the case in the doubly-linked list. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers i.e. prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. The next method in the singly-linked list or doubly-linked list moves the current pointer to the next node and every time it checks whether the next pointer is NULL or not. Similarly the back method in the double-linked list has to be employed carefully if the current is pointing the first node. In this case, the prev pointer is pointing to NULL. If we do not take care of this, the current will be pointing to NULL. So if we try to access the NULL pointer, it will result in an error. To avoid this, we can make a circularly linked list. We have a list with five elements. We have connected the last node with the first node. It means that the next of the last node is pointing towards the first node. current head

2

6

8

7

1

size=5

The same list has been shown in a circular shape. current 6 8 head 2 7 1 size=5

You have noticed that there is no such node whose next field is NULL. What is the benefit of this? If you use the next or back methods that move the current pointer, it will never point to NULL. It may be the case that you keep on circulating in the list. To avoid this, we get help from the head node. If we move the head node in the circularly linked list, it will not be certain to say where it was pointing in the start. Its advantages depend on its use. If we do not have to move too much in the list and have

43

CS301 – Data Structures ___________________________________________________________________ no problem checking the NULL, there is little need a circularly-linked list. But this facility is available to us. In this example, we made a circular linked list from a singly link list. In a singly link list we move in one direction. We point the next pointer of the last node to the first node. We can do the same with the doubly-linked list. The prev pointer of the first node will point to the last node and the next pointer of the last node will point to the first node. If you arrange all the nodes in a circle, one of the pointers (i.e. next pointer) will move in clockwise direction while the prev pointers in anti-clockwise direction. With the help of these pointers, you can move in the clockwise direction or anti-clockwise direction. Head node pointer will remain at its position. You don’t need to change it. If there is a need to remove the node pointed by head node than you have to move the head pointer to other node. Now we don’t have any NULL pointer in the doubly-linked list. We will not get any exception due to NULL pointers.

Josephus Problem
Now we will see an example where circular link list is very useful. This is Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. You might ask why someone has to choose a leader in this way. There are some historical stories attached to it. This problem is also studied in mathematics. Let’s see its pictorial view. N=10, M=3
3

4 5 6 7 9 8

2 1 10

We have ten numbers representing the ten persons who are in a circle. The value of M shows the count. As the value of M is three, the count will be three. N represents the number of persons. Now we start counting clockwise. After counting up to three, we have the number four. The number four is eliminated and put in the eliminated column.

44

CS301 – Data Structures ___________________________________________________________________ N=10, M=3
3

Eliminated 4 5 6 7 9 8

2 1 10

After eliminating the number four, we will start our counting from number five. Counting up to three, we have number eight which is eliminated and so on. Eliminated N=10, M=3 4 3 2 1 10 9 In the end, only number five will remain intact. N=10, M=3 Eliminated 4 5 8 2 7
3

5 8 6 7

10 9 1 6

45

CS301 – Data Structures ___________________________________________________________________ If we have ten persons (N = 10) in a circle and eliminate after counting up to three (M = 3). If we start our count from one, who will be the leader? We have studied this earlier and know that the person who is sitting at the fifth position will become the leader. Suppose if the value of N is 300 or 400 and the value of M is 5 or 10. Now who will be the leader? This is a mathematical problem where we can change the values of N and M. There is a formula where the values of N, M are allotted. You can calculate who should become the leader. Here we will not solve it mathematically. Rather, it will be tackled as a computer problem. If you analyze the pictures shown above, it gets clear that this can be solved with the circular link list. We arrange these numbers in a circularly-linked list, point the head pointer at the starting number and after calling the next method for three times, we will reach the node which is to be removed. We will use the remove method to remove the node. Then the next method is called thrice from there and the node is removed. We will continue this till we have only one node. We are not concerned with the NULL pointers, internal to link list. However, if you want to solve this problem and choose the best data structure, then circular link list is the best option. We can also use the list to solve this. Let’s see the code of the program by which we can solve this problem. The code is as under: /*This program solves the Josephus Problem */ #include #include "CList.cpp" //contains the circularly-linked list definition // The main method void main(int argc, char *argv[]) { CList list; // creating an object of list int i, N=10, M=3; for(i=1; i 1 ) { for(i=1; i…...

Similar Documents

Premium Essay

Ilz Assigments

...MANAGEMENT ACCOUNTING COSTING AND BUDGETING. HND IN BUSSINESS MANGMENT Indivdual Assigment [Ilzam Ilyas/BM43/09] Executive Summery This report describes Managing accounting costing and budgeting to an organization of both current and future business. Effective information and knowledge can be gained by an organization if they have a clear understanding about their costing and budgeting flow. There are four learning outcomes that have been talked in this report. The learning outcome 1 speaks about the types of costing that organization has to bear and it shows with the relevant examples. It also calculates cost and the price and found the net profit of the given statement using different costing methods. The learning outcome 3 explains the purpose of budgeting advantages and disadvantages and some other types of budgets and calculated the cash budget statement of the company. Further, in learning outcome 2 and 4 of the assignment, the way company “Cosmo” that makes and sell products is clearly described using variance as its method of controlling and coordinating their labors. Moreover, existing approach of Variance by the “Cosmo” organization has highlighted with the recommendation to improve their performance next year with all the calculations process is identified and discussed. Acknowledgment "I would like to thank our Management Accounting......

Words: 3508 - Pages: 15

Premium Essay

Assigments

...CHAPTER 3: QUESTIONS AND ANSWERS 1 Why do some people pronounce SQL as “sequel”? Ans: Because of its naming history, SQL is developed from SEQUEL language, so some people pronounce SQL as “sequel”. 2 Why are the manipulation statements of SQL more widely used than the definition and control statements? Ans: Because only the database administrator uses the definition and control statements, while power users and analysts, who are the majority, use the manipulation statements. 3 Is the SQL standard supported in whole or in part? Briefly explain. Ans: The SQL standard is supported in part. Because of the size of the previous standard SQL-92 (contains more than 600 pages) and the current SQL: 1999 standard (contains more than 2,100 pages), there are multiple levels that can be supported. No vendor supports the entire SQL-92 standard now. Most vendors support a super/subset of the standard. With the new SQL: 1999 standard, vendor implementation will likely be more fragmented because of the size and scope of the standard. 4 How many levels of conformance does the SQL-92 standard have? Ans: The SQL-92 specification contained three levels: Entry SQL, Intermediate SQL, and Full SQL. Unfortunately, no vendor claimed conformance beyond Entry SQL although most vendors implemented some features in the higher SQL levels as well as providing proprietary SQL extensions. 5 How many levels of conformance do the SQL: 1999 standard have? Ans: The SQL: 1999 standard contains a single level of......

Words: 6440 - Pages: 26

Free Essay

Assigment

...Assignment #2  Production economics  Exercise 1: Production function  In the Deep Creek Mining Company example described in class suppose that capital is fixed at 500 brake  horse power, while labor is a variable input.  a) Complete the following table  Labor input L   (n. of workers)  1  2  3  4  5  6  7  8  9  10    b) Plot the (i) total product, (ii) marginal product and (iii) average product functions  c) Determine the boundaries of the three stages of production  Exercise 2. Optimal input level in short‐run  The amount of fish caught per week on a trawler is a function of the crew size assigned to operate the  boat. Based on past data, the following production schedule was developed:  Crew size (number of workers)  2  3  4  5  6  7  8  9  10  11  12    a) Over what ranges of workers are there (i) increasing, (ii) constant, (iii) decreasing and (iv)  negative returns?  b) How large a crew should be used if the trawler owner is interested in maximizing the total  amount of fish caught?  1    Total product Q                      Marginal product MPL                                          Average product APL  Amount of fish caught per week (hundreds of lbs.)  3  6  11  19  24  28  31  33  34  34  33  c) How large a crew should be used if the trawler owner is maximizing the average amount of fish  caught per worker?  d) Suppose that the trawler owner can sell all the fish caught for $75 per 100 pounds and can hire ......

Words: 571 - Pages: 3

Premium Essay

Assigment

...INSTRUCTIONS TO STUDENTS 1. Students are required to form a group of FOUR (4) to FIVE (5) members. 2. Each group need to appoint a System Analyst (SA), Accountant, Administration Manager, Human Resource (HR) Manager and Programmer. These roles should be rotated among members. SA (Project Manager) - Keeps group on track; maintain full participation. Accountant - Records assignments, strategies, unresolved issue matters. HR Manager - Reports out during whole class discussion; monitors write up of final drafts of assignments. Programmer - Checks group understanding; finds resources. Administration Manager - Coordinate and service group members. 3. Deadlines for presentation and written report : Planning : Week 4 Analysis : Week 9 Design : Week 12 Implementation : Week 14 4. Assessment : * Written report = 80% (20M) * Peer evaluation = 10% (2.5M) * Student’s log = 10% (2.5M) 100% ( 25M) 5. Assessment Rubrics Assessment for written projects and peer evaluation will be done using assessment rubrics. 6. Student Learning Log Each group needs to complete the following student learning logs: * Student Product Brief : beginning of project * Student Learning : during the process Each member of the group needs to complete the following student learning logs: * Group Contribution & End-of-Project Self-Assessment : end of project * Peer Evaluation : end of project 7...

Words: 274 - Pages: 2

Premium Essay

Assigments

...Argument Racism at College University. I have to stay I am against what university are treating other ethics. We as human beings should be treated the same even though “people” put us in different classes. An article that I read up. Said “Researchers from Harvard and Tufts University have conducted a surprising study which shows that white people think that much needed racial progress has been made since the 1950s but at their expense. The study in question polled several hundred people, both black and white, about the pace of progress since the 1950s. Both racial groups indicated that there has been some definite change in the racial climate since then. But the surprising piece of the puzzle is that white people now believe that the decrease in anti-black racism has led to the rise of anti-white racism. In other words, white people believe that equality has been reached but that this has subjected them to experience more racism than black folk.” Its slowly staying that white people are starting to sue the schools because they aren’t getting accepted into their school of chooses. Seeing Asians, Blacks and Latinos percent of getting accepted higher than them makes them mad. It’s like saying there will never be any other race the white house. Yet times has change and soon there will be a women in power of the white house one day. These research findings are just another indicator of the pervasiveness of white privilege, where white privilege means whiteness at all costs...

Words: 445 - Pages: 2

Free Essay

Unit 6. Assigment 1. Cable Testers

...UNIT 6. ASSIGMENT 1. CABLE TESTERS. THE THREE CABLES TESTERS I WOULD PICK IN MY LINE UP WOULD BE THE OPTICAL LOSS TEST SET AND TEST KIT, THE TDR, AND THE TONE GENERATORS. THE OPTICAL LOSS TEST SET AND KITS WORKS MORE SO IN A LAB BASED SET UP THEN OUT ON THE FIELD BUT THE TEST KITS IS MORE FAVORABLE BY FIBER TECHS BECAUSE IT HAS A POWER METER AND LIGHT SOURCE THAT ARE MADE TO WORK TOGETHER. THE TDR (TIME DOMAIN REFLECTOR) IS A TOOL THAT IS USED BE SENDING A TONE OR / BEAM OF LIGHT (WITH A DATA ON IT SOME TIMES) AND WILL MEASURE HOW LONG IT TOOK FOR THAT TONE TO REACH THE DESTINE PLACE AND OR BACK TO THE SENDER FROM WHICH IT CAME. AND DEPENDING ON HOW LONG IT TAKES TO COME BACK (IF IT EVER DOES) WILL USUALLY LET YOU KNOW WHERE YOUR FAULT IS IN TYOU NETWORK CABLING THE TONE GENERATOR IS USED BY GENERATING A CERATIN TONE ALONG THE CABLING TO LET YOU KNOW WHERE EXACTLY YOU INCOSISTANCIES ARE WITH IN THAT SPECIFIC CABLE. THE OPTICAL LOSS TEST SET AND TEST KITS WILL SEEM TO WORK BEST WITH FIBER BASED LAYOUT, THE TIME DEFLECTIVE DOMAIN WILL WORK BEST ON THICKNET CABLING SET UPS AND THE TONE GENERATOR WILL WORK BEST ON COPPER/COAXIL LIKE SET UPS. THE TWO TESTER I THINK SHOULD BE IN EVER TECHS INSTALL KITS WILL BE THE TDR AND THE OPTICAL LOSS TEST SET AND TEST KITS THE REASON WHY IS THAT BOTH OF THESE TESTERS CAN SEND TESTING SIGNALS DOWN A CABLE THAT GOES IN OR THRU WALL OR TIGHT SPACES THAT THE HUMAN HAND CANT REACH NOT THE EYE CAN SEE, ALSO MOST OF THESE TESTERS WILL GIVE......

Words: 312 - Pages: 2

Premium Essay

Assigment

...Introduction Economic system is defined as the basic arrangements made by societies to solve the economic problem. There are three economic systems that classification by degree of government control. The three economic systems are Command Economy System (Socialism). A system where the government, rather than the free market, determines what goods should be produced, how much should be produced and the price at which the goods will be offered for sale. The command economy is a key feature of any communist society. Example of country that under this economy system are North Korea, Cuba and Russia. Market Economy System (Capitalism) is an economic system in which economic decisions and the pricing of goods and services are guided solely by the aggregate interactions of a country's citizens and businesses and there is little government intervention or central planning. This is the opposite of a centrally planned economy, in which government decisions drive most aspects of a country's economic activity. Example of the country are Hong Kong and USA.  Mixed Economy System is an economic system that features characteristics of both capitalism and socialism. A mixed economic system allows a level of private economic freedom in the use of capital, but also allows for governments to interfere in economic activities in order to achieve social aims. This type of economic system is less efficient than capitalism, but more efficient than socialism. Example of the country are Malaysia and......

Words: 2150 - Pages: 9

Free Essay

Assigment

...HANDOUT 13 Internet Resources Muslim Contributions to Civilization: Past and Present I. Islam and Science A. (Article) Science and Civilization in Islam (Seyyed Hossein Nasr) http://www.fordham.edu/halsall/med/nasr.html B. Overview of Islamic Culture and the Medical Arts (National Library of Medicine Exhibit) http://www.nlm.nih.gov/exhibition/islamic_medical/islamic_00.html C. Resource page of Islam SET (Science, Environment and Technology) ( www.islamset.com) http://www.islamset.com/introd.html i. History of Islamic Science http://www.islamset.com/heritage/history.html ii. History of Muslim Pharmacology http://www.islamset.com/heritage/pharmacy/index.html D. History of Islamic Biomedicine (links to many articles on this topic, including chronology of Muslim civilization) http://www.mic.ki.se/Arab.html E. Numbers http://www.islamic-paths.org/Home/English/History/Literature/Arabic_Numerals.htm II. Environment A. Islam and the Environment, theory and practice (Dr. Mawil Izzi Dien) http://www.lampeter.ac.uk/trs/staffgallery/mawil_paper.html B. (Article) Islam and Ecology http://www.crosscurrents.org/islamecology.htm III. History and Civilization A. History of Islamic Civilization http://www.islamset.com/islam/civil/index.html - and http://www.fordham.edu/halsall/islam/islamsbook.html Pg. 1 HANDOUT 13 B. Influence of Islamic Culture on Western......

Words: 1486 - Pages: 6

Premium Essay

Assigment

...Information Technology in Business Information technology. Collection of technologies Deal with processing, storing, and communicating information, including all types of computer and communications systems as well as reprographics methodologies. It refers to anything related to computing technology, such as networking, hardware, software, the Internet, or the people that work with these technologies. Many companies now have IT departments for managing the computers, networks, and other technical areas of their businesses. IT jobs include computer programming, network administration, computer engineering, Web development, technical support, and many other related occupations. Since we live in the "information age," information technology has become a part of our everyday lives. That means the term "IT," already highly overused, is here to stay. The role of information technology systems in a business environment can be classified into four broad categories. These categories include function performance, communication through networking, management and enterprise roles. Information technology provides commercial and industrial systems for businesses. These systems enable businesses to function effectively and efficiently. Function IT Systems Function IT systems are applications that allow individuals to function effectively in the workplace. Examples of common IT systems that enhance workplace functions are word processor applications, spreadsheet applications,...

Words: 2302 - Pages: 10

Premium Essay

Assigment

...Table of conte Content................................................................................................................................Page Introduction.........................................................................................................................3 1.1. Different organisational structure and culture..............................................................3 1.2. Relationship between organisational structure and culture..........................................4 1.3. Factors influencing individual behaviour at work........................................................5 2.1. Organisation theory and management practice.............................................................6 2.2. Different approaches to management used by Peacocks and Primark..........................6 3.1. Leadership styles and their effectiveness......................................................................7 3.2. Application of different motivational theories in workplace........................................8 3.3. Relationship between motivation theory and the practice of management..................10 4.1. Nature of groups and group behaviour.........................................................................11 4.2. Factors lead to effective teamwork and threaten the success......................................12 4.3. The impact of technology on team......

Words: 3354 - Pages: 14

Free Essay

Assigment

...1. An interview is a meeting between an applicant for employment and a company representative to determine if the candidate is qualified for a job, an internship or a volunteer opportunity. a. What are different types of interview? b. What are the skills and techniques required to have a successful job interview? There are several different kinds of interviews such as : Traditional Face-to-Face Interview * Most interviews are face-to-face. * The most traditional is a one-on-one conversation. * Your focus should be on the person asking questions. * Maintain eye contact, listen and respond once a question has been asked. * Don't interrupt the interviewer. Panel/Committee Interview * In this situation, there is more than one interviewer. * Typically, three to ten members of a panel may conduct this part of the selection process. T * his is your chance to put your group management and group presentation skills on display. * As quickly as possible, try to 'read' the various personality types of each interviewer and adjust to them. Find a way to connect with each interviewer. * Remember to take your time in responding to questions. * Maintain primary eye contact with the panel member who asked the question, but also seek eye contact with other members of the panel as you give your response. Behavioral Interview * The basic premise behind this type of interview is that your past behavior is the best......

Words: 761 - Pages: 4

Premium Essay

Assigment

...Colorado State University: Farmers’ Market Options? What You Really Get Silvija Nad Southern States University Abstract Organic vs non-organic food, what is a smarter choice?  Living green is front and center in our life today, from driving a fuel-efficient car to recycling more at home. Buying food at farmers' markets is a green option that is more popular than ever. Before you pick up a basket to head out to shop, get familiar with the options at farmers' markets. Organic farmers focus their efforts on renewable resources and the conservation of water and soil. Most organic foods cost 10 to 40 percent more than conventionally grown food. Some reasons people may feel better about paying the higher cost are thinking the organic options taste better, may be more nutritious, have lower levels of pesticides and herbicides and a desire to help the environment. On the other hand, some choose not to purchase organic food because it has a higher cost, fewer choices, a blemished appearance and uncertainty if the product is truly organic. Colorado State University: Farmers’ Market Options? What You Really Get Here are the facts about organic vs. non-organic food. Organic is not more nutritious when compared with conventional choices according to research studies. The nutrients in food can vary depending on soil and climate conditions when it was grown and how it was handled and shipped. Organic produce is not necessarily free of pesticides and herbicides, but it likely has......

Words: 427 - Pages: 2

Premium Essay

Assigment

...Customer Service Charter Australia Post plays an important role in the Australian community and is required by law to meet specific performance standards. These are called Community Service Obligations (CSOs). This Customer Service Charter aims to communicate these standards to our customers. This document also explains how you can obtain more information about these standards, how to offer feedback or let us know about any concerns. It also offers advice on how you can help Australia Post to serve you better. Contents Community Service Obligations (CSOs) 2 Mail services 4 Letter and parcel lodgement points 4 Posting times 5 Delivery timetable 6 Stamp purchases 7 Delivery services 8 Delivery frequency 9 Basic letter price 10 Other services 11 Feedback and complaints 12 Compensation 13 Making the most of Australia Post 14 Performance reports 15 Australia Post contact points 16 Customer Service Charter |page 1 Community Service Obligations (CSOs) Australia Post is required by law to provide a universal letter service which is reasonably accessible to all Australians and, in addition, to provide a standard letter service at a uniform price (currently 55 cents) from anywhere to anywhere in the country. This means the cost of delivering some letters can be many times higher than the postage charged. The CSO cost occurs when the real......

Words: 2186 - Pages: 9

Premium Essay

Week 3 Assigment

...Week 3 Assignment RES 345 February 01, 2016 Week 3 Assigment The article "American Dream in Flux: The Endangered Right to Lease A Home highlights the constitutional violation and social harm perpetuated from the declining shift in homeownership coupled with the increasing inclusion of governmentally promoted community rental restrictive zoning ordinances and private covenants. For the most part zoning ordinances and private laws (rules, regulations, and deed restrictions) appear to provide considerable aesthetic benefit as well as serve to preserve property value. However, these very laws and covenants work to advance government policy and foster continued segregation not promote community interest or protect ownership rights. Consequently, governmental and private leasing prohibition often significantly restricts a title holders exercise of property rights and use of capital. Additionally, rental restrictive covenants result in undue hardship for owners and potential tenants alike. Further, these frequently encountered universally accepted private laws disguised as property use tools work to control who is allowed to reside in a particular neighborhood not prohibit value impacting undesirable usage. More, the preclusion of leasehold estate alienation ultimately infringes on the freedom to contract, decreases availability of rental housing, and artificially raises market rents, as well as functions to segregate specific demographic groups (Boyack, 2014). ......

Words: 1030 - Pages: 5

Free Essay

Ethics Assigment- New Systems Inc

...interoffice memorandum subject: ethics assigment- New systems inc date: [ 3/18/2012 ] A number of business ethical conflicts develop due to divergences between different stakeholders’ interests such as; company owners, employees, customers or/and surrounding community. One of the major stakeholders New Systems Inc is experiencing an ethics situation. Alexandra Lopez a certified internal auditor for New Systems Inc has been assigned to the Royal Division for the past two years. Alexandra has found some discrepancies in the company’s asset turnover ratios, which will reduce the company’s ROI (Return on Investments), the base for the division’s performance assessment and bonus system. Other stakeholders are Will Dixon and the Royal Division V.P. Will seems to be bothered with Alessandra findings; however he made a commitment to look into the problem and to rectify any problems before next year’s review. Jean Williams, the Royal Division CFO, is also an important stakeholder, who is considered Alexandra’s mentor and friend. Jean and other key people of the department would be directly impacted by Alexandra’s findings, which will reduce their performance assessment and consequently their bonus; 30% of their base pay is directly related to the company’s ROI. Finally Alexandra’s husband can also exert some influence on her decisions, especially when it involves their personal finances. The main dilemma with Alexandra’s findings started when Jean, her friend,......

Words: 1098 - Pages: 5

Nhạc nước khác | Adaptador Micro SD a Memory Stick PRO DUO [ MicroSD - MS PRODUO - Sony - PSP ] | premiere annee