1. Home
  2. Explore
  3. Computer Science
  4. Intro to Data Structures

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Intro to Data Structures

Transcript

In this lesson and in this series of lessons, we will introduce you to the concept of data structures. Data structure is the most fundamental and building block concept in computer science and good knowledge of data structures is a must to design and develop efficient software systems. Okay, so let's get started. We deal with data all the time and how we store, organize and group our data together matters. Let's pick up some examples from our day-to-day life

where organizing data in a particular structure helps us. We are able to search a word quickly and efficiently in a language dictionary because the words in the dictionary are sorted. What if the words in the dictionary were not sorted? It would be impractical and impossible to search for a word among millions of words. So dictionary is organized as a sorted list of words. Let's pick up another example. If we have something like a city map, the data like position of a

landmark and road network connection. So all this data is organized in the form of geometries. We show the map data in the form of these geometries on a two dimensional plane. So map data needs to be structured like this so that we have scales and directions and we are effectively able to search for a landmark and get route from one place to another. And I'll pick one more example for something like daily cash in and cash out statement of a business, what we

also call a cash book in accounts. It makes most sense to organize and store the data in the form of a tabular schema. It is very easy to aggregate data and extract information if the data is organized in these columns in these tables. So different kind of structures are needed to organize different kind of data. Now computers work with all kind of data. Computers work with text, images, videos, relational data, geospatial data and pretty much any kind of data that we

have on this planet. How we store, organize and group data in computers matters because computers deal with really really large data and even with the computational power of machines, if we do not use the right kind of structures, the right kind of logical structures, then our software systems will not be efficient. Formal definition of a data structure would be that a data structure is a way to store and organize data in a computer so that the

data can be used efficiently. When we study data structures as ways to store and organize data, we study them in two ways. So I'll say that we talk about data structures as one, we talk about them as mathematical and logical models. When we talk about them as mathematical and logical models, we just look at an abstract view of them. We just look at from a high level what all features and what all operations define that particular data structure. Example of abstract view

from real world can be something like the abstract view of a device named television can be that it is an electrical device that can be turned on and off. It can receive signals for satellite programs and play the audio video of the program and as long as I have a device like this, I do not bother how circuits are embedded to create this device or which company makes this device. So this is an abstract view. So when we study data structures as mathematical or

logical models, we just define their abstract view or in other words, we have a term for this, we define them as abstract data types. An example of abstract data type can be, I want to define something called a list that should be able to store a group of elements of a particular data type and we should be able to read the elements by their position in the list and we should be also able to modify element at a particular position in the list. I would

say store a given number of elements of any data type. So we are just defining a model. Now we can implement this in a programming language in a number of ways. So this is a definition of an abstract data type. We also call abstract data type as ADT and if you see, all the high-level language is already have a concrete implementation of such an ADT in the form of arrays. So arrays give us all these functionalities. So arrays are data types which are concrete

implementation. So the second way of talking about data structures is talking about their implementation. So implementations would be some concrete types and not an abstract data type. We can implement the same ADT in multiple ways in the same language. For example in C or C++ we can implement this list ADT as a data structure named linked list and if you have not heard about it, we will be talking about them a lot. We will be talking about linked list a lot in the coming lessons. Okay so let's define an abstract data type formally because this is one term that we will encounter quite often. Abstract data types are entities that are definitions of data and operation but do not have implementations. So they do not have any implementation details. We will be talking about a lot of data structures in this course. We will be talking about them as abstract data types and we will also be looking at how to implement them. Some of the

data structures that we will talk about are arrays linked list, stack, queue, tree, graph and the list goes on. There are many more to study. So when we will study these data structures we will study their logical view. We will study what operations are available to us with these data structures. We will study the cost of these operations mostly in terms of time and then definitely we will study the implementation in a programming language. So we will be studying all these

data structures in the coming lessons and this is all for this introductory lesson. Thanks for watching. In our previous lesson we introduced you to the concept of data structures and we saw how we can talk about data structures in two ways. One as a mathematical and logical model that we also call that we also term as an abstract data type or ADT and then we also study data structures as concrete implementations. In this lesson we will study one simple data

structure. We will first define an abstract view of it. We will first define it as an abstract data type and then we will see the possible implementations and this data structure is list. List is a common real world entity. List is nothing but a collection of objects of the same type. We can have a list of words, we can have a list of names or we can have a list of numbers. So let us first define list as an abstract data type. So when we define abstract data type we just

define the data that will store and we define the operations available with the type and we do not go into the implementation details. Let us first define a very basic list. I want a list that can store a given number of elements of a given data type. This would be a static list. The number of elements in the list will not change and we will know the number of elements before creating the list. We should be able to write or modify element at any position in the

list and of course we should be able to read element at a particular position in the list. So if I ask you for an implementation of such a list and you have taken a basic course in programming, a basic introductory course then you'll be like hey I know this and array gives us all these features. All these operations are available with an array. We can create an array of any data type so let us say if we want to create a list of integers then we declare the array

type as integer and then we can give the size as a parameter in declaration. I can write or modify element at a particular position. The elements are A0, A1 and are accessed something like this. We all know about arrays and then we can also read elements at a particular position. The element at ith position is accessed as AI. So array is a data structure that gives us implementation for this list. Now I want a list that should have many more features. I want it to handle more

scenarios for me. So I'll redefine this list here. I do not want a static list a static collection with a fixed size. I want a dynamic list that should grow as per my need. So the features of my list are that I'll call my list empty if there are no elements in the list. I'll say the size of the list is 0 when it is empty and then I can insert an element into the list and I can insert an element at any position in the list and in an existing list. I can remove element from the list.

I can count the number of elements in the list and I should be able to read or write or rather read or modify element at a particular position in the list and I should also be able to specify the data type for the list. So I should be able to while creating the list I should be able to say whether this is a list of integers or whether this is a list of string or float or whatever. Now I want a data structure which is implementation of this dynamic list. So how do I get it?

Well actually we can implement such a dynamic list using arrays. It's just that we will have to write some more operations on top of arrays to provide for all these functionalities. So let us see how we can implement this particular list using arrays. Let's for the sake of simplicity of design assume that the data type for the list is integer. So we are creating a list of dynamic list of integers. What we can do is to implement such a list we can declare a really large array. We will define some max size and declare an array of this max size. Now as we know the elements in the array are indexed as a0, a1, a2 and we go on like this. So what I'll do is I'll define a variable that will mark the end of the list in this array. So if the list is empty we can initialize this variable or we can set this variable as minus 1 because the lowest index possible is 0. So if end is minus 1 the list is empty. At any time

a part of the array will store the list. Okay so let's say initially when the list is empty this pointer end is pointing to index minus 1 which is not valid which does not exist. And now I insert an integer into this array and let's say if we do not give the position at which the number is to be inserted the number is always inserted towards the tail of the list towards the end of the list. So the list will be like we will have an element at position 0 and now

end is index 0. So at any time end marks the this variable end marks the end of the list in this array. Now if I want to insert something in the list at a particular position let's say I want to insert number 5 at index 2 then to accommodate 5 here at this particular position we will have to shift all the elements one unit towards the right. All the elements starting index 2 we need to shift all the elements starting index 2 towards the right. Okay I just inserted

some elements into the list let me also write the function call for these. Let's say we went in this order we inserted 2 then we inserted 4 and then we inserted in the end we are inserting 5 and we will also give the position at which we want to insert. So this insert with two arguments would be the call to insert element at a particular position. So after all these operations after all these insertions this is what the list will look like. This arrow here marks the end of the list in the array. Now if I want to remove an element from a particular position let's say I make a call to something to the remove function I want to remove the element 2. So I'll pass the index 0 here I want to remove the element at index 0. So to do so all these elements after index 0 will be shifted one unit towards the left or towards the lower indices and 2 will go away. Now this end variable here is being adjusted after each insertion that we are making. So after

this insertion end will be 0 after this 1, 2, 3 and so on after this remove end will be 4 again. Okay looks like we pretty much have an implementation of this list in the left that is described as an abstract data type. We have a logic of calling the list empty when we have this variable n is equal to minus 1. We can insert element at a particular position in the list we can remove element. It's just that we have to perform some shifts in the array. We can

count the number of elements in the list. It will be equal to n plus 1 the value in the variable n plus 1. We can read or modify element at a position well this is an array so we can definitely read or modify element at a particular position. If we wanted to choose the data type it was just choosing the array of that particular data type. Now this looks like a cool implementation except that we have one problem. We said that the array will be of some large size some max size. But what is a good max size? We can always exhaust array the list can always grow to exhaust the array. There is no good max size. So we need to have a strategy for the scenario when the list will fill up the whole array. So what do we do in that case? We need to keep that into our design. We cannot extend the same array. It is not possible to do so. So we will have to create a new array a larger array. So when the array is full we will create a new larger array and

copy all the elements from the previous array into the new array. And then we can free the memory for the previous array. Now the question is by how much should we increase the size of the new array? This whole operation of creating a new array and copying all the elements from the previous array into the new array is costly in terms of time and definitely a good design would be to avoid such big cost. So the strategy that we choose is that each time the array is full we create

a new larger array of double the size of the previous array. And why this is the best strategy is something that we will not discuss in this lesson. So we will create a larger array of double size and copy elements from previous array into this new array. This looks like a cool implementation. The study of data structures is not just about studying the operations and the implementation of these operations. It's also about analyzing the cost of these operations. So let us see what are the costs in terms of time for all these operations that we have in the dynamic list. The access to any element in this dynamic list if we want to access access it using index for read or write then this will take constant time because we have an array here. And in array elements are arranged in one contiguous block of memory using the starting address or the base address of the block of the memory of the block of memory and the index or the position of the element we

can calculate the address of that particular element and access it in constant time. Big O notation that is used to describe the time complexity of operations for constant time it is written as in terms of Big O the time complexity is written as Big O of 1. If we wanted to insert element if we wanted to insert element at the end of the array end of the list then that again will be constant time but if we would insert element at a particular position in the

list then we will have to shift elements towards higher indices. In the worst case we will have to shift all the elements to the right when we will be inserting at the first position. So the time taken for insertion will be proportional to the length of the list let's say the length of the list is n or in other words we will say that insertion will be Big O of n in terms of time complexity. If you do not know about Big O notation do not bother just understand that inserting an

element at a particular position will be a linear function in terms of the size of the list. Removing an element will again be Big O of n. Time taken will be proportional to the current size of the list n is the size of the list here okay now inserting an element at the end we just said that it will happen in constant time it is not so if the array is full then we will create a new array let's call inserting element at the end as adding an element. Adding an element will take

constant time if the list is not full but it will take time proportional to the size of the list size of the array if the array is full. So adding in the worst case will be Big O of n again as we said when the list is full we create a new copy double the size of the previous array and then we copy the previous array the elements from previous array into the new array. So prime of a see what looks like the good thing with this kind of implementation. Well the good thing is

that we can access elements at any index in constant time which is the property of the array but if we have to insert some element in between and if we have to remove element from the list then it is costly. If the list grows and strings a lot then we will also have to create a new array and have all this thing of copying elements from previous array into new array again and again. And one more problem is that a lot of time a lot of the array would be unused the

memory there is of no use but definitely the use of array as dynamic list is not efficient in terms of memory this kind of implementation is not efficient in terms of memory. This leads us to think can we have a data structure that will give us a dynamic list and use the memory more efficiently. We have one data structure that gives us good utilization of the memory and this data structure is linked list and we will study about the linked lists in the

next lesson. So that's it for this lesson thanks for watching. In this lesson we will introduce you to link the list data structure. In our previous lesson we tried to implement a dynamic list using arrays and we had some issues there. It was not most efficient in terms of memory usage in terms of memory consumption. When we use arrays we have some limitations. To be able to understand linked list well we need to understand these limitations. So I am going to tell

you a simple story to help you understand this. Now let us say this is computer's memory and each partition here is one byte of memory. Now as we know each byte of memory has an address we are showing only a section of the memory that's why it is extending towards the bottom and the top. Let's say the address increases from bottom to top. So if this byte is address 200 the next byte would be address 201 and next byte would be address 202 and so on. What I want to do is I want to draw

this memory from left to right horizontally instead of trying it from bottom to top like this. This looks better. Let's say this byte here is address 200 and as we go towards the right the address increases. So this is like 201 and we go on like 202, 203 and so on. It doesn't really matter whether we show memory from bottom to top or left to right. These are just logical ways to look at the memory. So coming back to our story memory is a crucial resource and

all the applications keep asking for it. So Mr. Computer has given this job of managing the memory to one of his components to one of his guys who he calls the memory manager. Now this guy keeps track of what part of the memory is free and what part of the memory is allocated and anyone who needs memory to store something needs to talk to this guy. Albert is our programmer and he is building an application. He needs to store some data in the memory so he

needs to talk to the memory manager. He can talk to the memory manager in a high level language like C. Let us say that he is using C to talk to the memory manager. First he wants to store an integer in the memory. So he communicates this to memory manager by declaring an integer variable something like this. The memory manager sees this declaration and he says that okay you need to store an integer variable. So I need to give you four bytes of memory because integer

variable is stored in four bytes in a typical architecture and let us say in this architecture it is stored in four bytes. So the memory manager looks for four bytes of free space in the memory and assigns it or allocates it for variable X. Address of a block of memory is the address of the first byte in the memory. So let us say this first byte of memory here is at address 217. So variable X is at address 217. So memory manager kind of communicates it back to Albert

that hey I have assigned address 217 for your variable X you can store whatever you want there and Albert can fill in any data into this variable. Now Albert needs to store a list of integers a list of numbers and he thinks that the maximum number of integers in this list will be four. So he asks the memory manager for an integer array of size four named A. Now array is always stored in memory as one contiguous block of memory. So memory manager is like okay I need to

look for a block of memory of 16 bytes for this variable this array A. So the memory manager allocates this block starting address 201 and ending address 216 for this variable A which is an array of four integers. Because array is stored as one contiguous block of memory and memory manager conveys the starting address of this block whenever Albert tries to access any of the elements in the array let's say he tries to access let's say he tries to write some value at

the fourth element in the array which he accesses as A3. Albert's application knows where to write this particular value because it knows the base address the starting address of the block A the array A and from base address using the index which is three here it calculates the address of A3. So it knows that A3 is at address 213. So to access any of the elements in the array the application takes constant time and this is one awesome thing about arrays that

irrespective of the size of the arrays the application and application can access any of the elements in the array in constant time. Now let's say Albert uses this array of four integers to store his list. So I'll fill in some values here at these positions let's say this is 8, this is 2, this is 6, this is 5, this is 4. Now Albert at some point feels that okay I need to have one more element in this list. Now he has declared an array of size 4 and he wants to add

a fifth element in the array. So he asks the memory manager that hey I want to extend my array A is it possible to do so I want to extend the same block and the memory manager is like when I allocate memory for an array I do not expect that you will ask for an extension. So I use whatever memory is available adjacent to that block for other variables. In some cases I may extend the same block but in this case I have an element and a variable X next to your block so I cannot

give you an extension. So Albert is like what all options do I have? Memory manager is like you can tell me the new size and I can recreate a new block at some new address and we will have to copy all the elements from the previous block to the new block. So Albert says that okay let's do it but the memory manager is like you still need to give me the size of the new block. Albert thinks that this time he'll give a really large size for the new array or the new block so that it does not fill up. This new block starting address 2 to 4 is allocated. Albert asks memory manager to free the previous block and this is some cost he has to copy all the elements all the numbers from the previous block into the new block and now he can add one more element to this list and he has kept his array large this time just in case he needs more numbers in the list. So the only option that Albert had was to create a as an entirely new block as an

entirely new array and Albert is still feeling bad because if the list is too small he is not using some part of the array and so memory is getting wasted and if the list again grows too much he will again have to create a new array a new block and he will again have to copy all the elements from the previous block into the new block. Albert is desperately seeking a solution to this problem and the solution to this problem is a data structure named linked list. So let us

now try to understand linked list data structure and see how it solves Albert's problem. What Albert can do is that instead of asking the memory manager for an array which will be one large contiguous block of memory he can ask memory for one unit of data at a time for one element at a time in a separate request. I am cleaning up the memory here once again let's say Albert wants to store this list of four integers in the memory. What if he requests memory for one

integer at a time. So first he pings memory manager for some memory to store number six memory manager will be like okay you need space to store an integer so you get this block of four bytes at address 204. So Albert can store number six here now Albert makes another request a separate request for number five. Let say he gets this block starting address 217 for number five because he makes a separate request he may or may not get memory adjacent to number six higher

probabilities that he will not get an adjacent memory location. So similarly Albert makes separate requests for number four and two. So let's say he gets these two blocks at address 232 and 242 respectively for numbers four and two. So as you can see when all but makes separate requests for each integer instead of getting one contiguous block of memory he gets these disjoint non-contiguous blocks of memory. So we need to store some more information here we need to store the information that this is the first element in the list and this is the second element in this list. So we need to link these blocks together somehow with an array it was very simple. We had one contiguous block of memory so so we knew where a particular element is by calculating its address using the starting address of the block and the position of the element in the array but here we need to store the information that this is the first block which stores

the first element and this is the second block which stores the second element and so on. To link these blocks together and to store the information that this is the first block in the list and this is the second block in the list what we can do is that we can store some extra information with each block. So what if we can have two parts in each block something like this and in one part of the block we store the data or the value and in the other part of the block we store the address of the next block. In this example in the first block the address part would be 2 1 7 the address of the next block that stores 5 and in this next block or the second block the address part would be 2 3 2. In the block at address 2 3 2 we will store the address 2 4 2 the address of the next block that stores number 2 and the block at 2 4 2 is the last block there is no next block after this. So in the address part we can have address as 0 0 is in

valid address 0 can be used to mark that this is the end of the list there is no link to the next node or next block after this particular block. So all but now actually has to request memory manager for a block of memory that will store two variables one and integer variable that will store the value of our element and one a pointer variable that will store the address of the next block or the next node in the list. In C he can define a type named node like this he

will have two fields in the node one to store the data this field will be an integer and one more field to store the address of the next node in the list. So Albert will ask a node Albert will ask memory for a node from the memory manager and the memory manager will be like okay you need a node that needs four bytes for an integer variable and four more bytes for the pointer variable that will store the address pointer variable also in a typical architecture is stored in

four bytes. So now memory manager gives us a block of eight bytes and we call this block a node. Now notice that the second field in the node structure is node star which means pointer to node so this field will only store an address of the next node in the list. So if we store the list like this in the memory as these non-contiguous nodes connected to each other then this is a linked list data structure. Logical view of the linked list data structure will be

something like this. Data is stored in these nodes and each node store the data as well as the link to the next node. So each node kind of points to the next node. The first node is also called the head node and the only information about the list that we keep all the time is address of the head node or address of the first node. So address of the head node kind of gives us access to the complete list. The address in the last node is null or 0 which means that the

last node does not point to any other node. Now if we want to traverse the linked list the only way to do it is we start at the head and we go to the first guy and then we ask the first guy the address of the next guy address of the next node and then we go to the next node and ask the address of the next node and this is the only way to access the elements in the linked list. If we want to insert a node in the linked list let's say we want to add number 3 at the end of the linked list then all we need to do is first create a node in the linked list. Sorry first create a node independently and separately it will get some memory location. So we created this node with value 3. Now all we need to do is fill the address properly adjust these links properly. So the address of this particular node will be filled in this node with value 2 and this node the address part can be null. So it is the last node it does not point to any other

node. Let's also show these nodes in the memory here. So I've written the address of each node in brown at top of these nodes and I've also filled in this address field of each node. Let's say the node for value 3 gets address 2 5 2. So this is how things will be in the memory and this is how the logical view will be. The linked list is always identified by the address of the first node and unlike arrays we cannot access any of the elements in constant time. In the case of arrays using the starting address of the block of memory and using the position of the element in the list. We could calculate the address of the element but in this case we have to start at the head and we have to ask this element for next element and then ask the next element who is your next. It's like playing treasure hunt. You go to the first guy and then you get the address for the second guy and then you go to the second guy and you get the address for the

third guy. So the time taken to access elements will be proportional to the size of the list. Let's say the size of the list is n. There are n elements in the list. In the worst case to traverse the last element you will go through all the elements. So time taken to access elements is proportional to n or in other words we say that this operation will cost us or rather the time complexity of this operation is big O of n. Insertion into the list. We can insert anywhere in the list. We first need to create a node and just adjust these links properly. Like say I want n at third position in the list. So all we need to do is create a node, store the value 10 in the data part, something like this. Let's say we get the node 10 at address 310. So we will adjust the address field in the second node to point to and this node with address 310 and this node will point to the node with value 4. Now to insert also we will have to traverse the list and go to that

particular position and so this will be big O of n again in terms of time complexity. The only thing is that the insertion will be a simple operation. We will not have to do all the shifts as we had to do in an array to insert something in between. We had to shift all the elements by one position towards higher indices. Similarly to delete something from this list will also be O n. So we can see some good things about linked list. There is no extra use of memory in the sense that some memory is unused. We are using some extra memory. We are using some extra memory to store the addresses but we have the benefit that we create nodes as and when we want and we can also free the nodes as and when we want. We do not have to guess the size of the list beforehand like in the case of arrays. Now we will discuss all the operations on linked list and the cost of these

operations as well as comparison with arrays in our next lessons. We will also be implementing linked list in C or C++. So this is all for a basic introduction to linked list. Thanks for watching. In our previous lesson we introduced you to linked list data structure and we saw how linked lists solve some of the problems that we have with arrays. So now the obvious question would be which one is better and array or a linked list. Well there is no such thing as one data structure

is better than another data structure. One data structure may be really good for one kind of requirement while another data structure can be really good for another kind of requirement. So it all depends upon factors like what is the most frequent operation that you want to perform with the data structure or what is the size of the data and there can be other factors as well. So in this lesson we will compare these two data structures based on some parameters based on the cost of operations that we have with these data structures. So all in all we will comparatively study the advantages and disadvantages and try to understand in which scenario we should use an array and in which scenario we should use a linked list. So I will draw two columns here one for array and another for linked list and the first parameter that we want to talk about is the cost of accessing

an element irrespective of the size of an array it takes constant time to access an element in the array. Now this is because an array is stored as one contiguous block of memory. So if we know the starting address or the base address of this block of memory let us say what we have here is an integer array and the base address is 200 the first byte in this block is at address 200 then let's say if we want to calculate the address of element at index i then it will be equal to 200 plus i into size of an integer in bytes. So size of an integer in bytes is typically 4 bytes so it will be 200 plus 4 into i. So if 0th element is at address 200 if we want to calculate the address for element at index 6 it will be 200 plus 6 into 4 which will be equal to 224. So knowing address of any element in an array is just this calculation for our application in terms of big o notation constant

time is also called big o of 1. So accessing an element in an array is big o of 1 in terms of time complexity. If you are not aware of big o notation check the description of this video for a tutorial on time complexity analysis. Now in a linked list data is not stored in a contiguous block of memory. So if we have a linked list something like this let's say we have a linked list of integers here then we have multiple blocks of memory at different addresses. Each block in

the linked list is called a node and each node has two fields one to store the data and one to store the address of the next node. So we call the second field the link field. The only information that we keep with us about a linked list is the address of the first node which we also call the head node and this is what we keep passing to all the functions also the address of the head node. To access an element in the linked list at a particular position we first need to start at

the head node or the first node and at the first node we need to see the address of the second node and then we go to the second node and see the address of the third node. In the worst case to access the last element in the list we will be traversing all the nodes in the list. In the average case we will be accessing the middle element maybe. So if n is the size of the linked list and is the number of elements in the linked list then we will traverse n by two elements. So the time taken in the average case also is proportional to number of elements in the linked list. So we can say that the time complexity in average case is big O of n. So on this parameter cost of accessing an element arrays course heavily over linked list. So if you have a requirement where you want to access elements in the list all the time then definitely array is a better choice. Now the second parameter that we want to talk about is memory requirement or

memory usage. With an array we need to know the size of the array before creating it because array is created as one contiguous block of memory. So array is a fixed size. What we typically do is create a large enough array and some part of the array stores our list and some part of the array is vacant or empty so that we can add more elements in the list. For example we have an array of seven integers here and we have only three integers in the list.

Rest four positions are unused. There would be some garbage value there. With linked list let's say we have let's say we have this linked list of integers. There is no unused memory. We ask memory for one node at a time so we do not keep any reserved space but we use extra memory for pointer variables. And this extra memory requirement for pointer variables in a linked list cannot be ignored in a typical architecture let's say integer is stored in four bytes and pointer variable also takes four bytes. So if you see the memory requirement for

this array of seven integers is 28 bytes and the memory requirement for this linked list would be eight into three where eight is the size of each node four for integer and four bytes for the pointer variable. So this is also 24 bytes. If we add one more element to the list in the array we will just use one more position. While in linked list we will create one more node and we'll take another eight bytes. So this will be 32 bytes linked list would fetch us a lot of

advantage of the data. The data part is large in size. So in this case we had a linked list of integers. So integer is only four bytes. What if we had a linked list in which a data part was some complex type that took 16 bytes. So four bytes for the link and 16 bytes for the data each node would have been 20 bytes and array of seven elements for 16 bytes of data would be 16 byte for each element would be 112 bytes and linked list of four would be only 80 bytes.

So it all depends. If the data part for the list takes a lot of memory linked list will definitely consume lot less memory. Otherwise it depends what strategy we are choosing to decide the size of the array. At any time how much array we keep unused. Now one more point with memory allocation because arrays are created as one contiguous block of memory. Sometimes when we may want to create a really really large array then maybe memory may not be available as one large block but if we are

using linked list memory may be available as multiple small blocks. So we will have this problem of fragmentation in the memory. Sometimes we may get many small units of memory but may not get one large block of memory. This may be a rare phenomenon but this is a possibility. So this is also where linked list scores. Because arrays have fixed size once array gets filled and we need more memory then there is no other option than to create a new array of larger size

and copy the content from the previous array into the new array. So this is also one cost which is not there with linked list. So we need to keep these constraints and these requirements in mind when we want to decide for one of these data structures for our requirement. Now the third parameter that we want to talk about is cost of inserting an element in the list. Remember when we are talking about arrays here we are also talking talking about the possible use

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent