06 February, 2015

Data Structures: Implementation of Linked Lists using Arrays in C++

Implementation of "Linked Lists" using Arrays


    To insert element at 
        
        
        1) 1st postition
        2) last position
        3) user defined position
   
    To delete element at
       
       1) 1st postition
       2) last position
       3) user defined position


Arrays.

Well what we know about them is that
  • it is a fundamental construct.
  • stores a sequence of values that are all of the same type. We want not just to store values but also to be able to quickly access each individual value.
  • Now if we want to access the individual elements we can do so specifying the index value of that particular element.
What else do we know about them.
  • Zero-based indexing. We always refer to the first element of an array a[] as a[0], the second as a[1], and so forth.
  • Array length. Once we create an array, its size is fixed. You can refer to the length of an a[] in your program with the code a.length
Now I can Set array values at compile time. When we have a small number of literal values that we want to keep in array, we can initialize it by listing the values between curly braces, separated by a comma. For example, we might use the following code in a program that processes playing cards.


And when we do something like this

The array created is nothing but a dense list. i.e we cannot modify the size of the array after compilation. Which has its own advantages and disadvantages.

So we will be implementing A linked List using arrays for a change. Shall we?

Now we the correct way of implementing "Linked Lists" is by using pointers. You might wanna take a look a this. A simple C++ implementation of Linked Lists

1 comment: