Beruflich Dokumente
Kultur Dokumente
Lecture No. 01
___________________________________________________________________
Data Structure
Lecture No. 01
Summary
Page 1 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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.
Page 2 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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 0 th 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 cant say what will be the
next position to get the data or insert the data.
Page 3 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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.
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.
Lets 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:
Page 4 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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
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 arrays 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 cant 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
Page 5 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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. 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:
Page 6 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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.
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.
Page 7 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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
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:
Function
s
start()
tail()
next()
back()
Description
Moves the current pointer to the very first element
Moves the current pointer to the very last element
Move the current position forward one element
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.
Data Structure
Lecture No. 03
Summary
Array
Page 8 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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.
Lets 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
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.
Page 9 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
In the above program, we have declared an array named x. x is an arrays 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 cant 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. 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
Page 10 of 11
Data Structures
Lecture No. 01
___________________________________________________________________
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.
Page 11 of 11