DS & A (Lists - Array Based)
| CHAPTER 9 |
"The List Data Structure", Yep, that's exactly what today's chapter is gonna be about. So, without any further ado let's have a sneak peek of today's chapter.- Intro to Lists
- List ADT
- List Implementation (Array-Based)
- Pros and Cons of Array-Based List
A list in Data Structures can be defined as a sequence of ordered values.
General Properties:
- Can be Sorted or Unsorted
- Can be Finite or Infinite
Note: We will be working with finite lists.
Example
Example
- The List is of size n.
- Each element has a datatype
- Atomic Data Type (Discussed in Chapter 1)
- or Structure Data Type (Discussed in Chapter 1)
- The List can be empty. (Empty Lists)
- List length is the number of elements stored.
- The beginning of the List is called Head.
- The end of the List is called Tail.
- Retrieve value.
- Insert/Delete/Modify value.
- Search for a value.
List ADT
- template<typename e> class List
- {
- //Empty the list
- virtual void clear() = 0;
- //Insert value of e into the list at a position before the current position
- virtual bool insert(const e&) = 0;
- //Append e to the list
- virtual bool append(const e&) = 0;
- //Remove element at the current position
- virtual bool remove(e&) = 0;
- //set current position to the beginning
- virtual void setStart() = 0;
- //Set current position to the end
- virtual void setEnd() = 0;
- //Set current position to the previous element
- virtual void prev() = 0;
- //Set current position to the next element
- virtual void next() = 0;
- //Size of list to the left of current element
- virtual int leftLength() const = 0;
- //Size of list to the right of current element + 1(inclusion of current element)
- virtual int rightLength() const = 0;
- //Set current position to pos
- virtual bool setPos(int pos) = 0;
- //Get value at current position
- virtual bool getValue(e& item) const = 0;
- //Print list
- virtual void print() const = 0;
- };
- Array-Based List Implementation
- #include <iostream.h>
- #include "name_of_the_ListADT_file.h"
- template<typename e>
- class AList:public List<e>
- {
- private:
- int maxSize; // maximum size of list
- int listSize; // number of stored values in the list
- int fence; // position of fence
- e* listArray; // array holding list
- public:
- AList(int size = 0)
- {
- maxSize = size;
- listSize = fence = 0;
- listArray = new e[maxSize];
- }
- ~AList()
- {
- delete []listArray;
- }
- //Mandatory redeclaration because these functions are pure virtual in the base class
- void clear() override;
- bool insert(const e&) override;
- bool append(const e&) override;
- bool remove(e&) override;
- void setStart() override;
- void setEnd() override;
- void prev() override;
- void next() override;
- int leftLength() const override;
- int rightLength() const override;
- bool setPos(int pos) override;
- bool getValue(e& item) const override;
- void print() const override;
- };
- template<typename e>
- void AList<e>::clear()
- {
- listSize = fence = 0;
- delete []listArray;
- listArray = new e[maxSize];
- }
- template<typename e>
- bool AList<e>::insert(const e& item)
- {
- if(maxSize == listSize)
- {
- return false;
- }
- for(int i = listSize; i>fence; i--)
- {
- listArray[i] = listArray[i-1];
- }
- listArray[fence] = item;
- listSize++;
- return true;
- }
- template<typename e>
- bool AList<e>::append(const e& item)
- {
- if(listSize == maxSize)
- {
- return false;
- }
- listArray[listSize++] = item;
- return true;
- }
- template<typename e>
- bool AList<e>::remove(e& item)
- {
- if(rightLength() == 0)
- {
- return false;
- }
- item = listArray[fence];
- for(int i=fence; i<listSize; i++)
- {
- listArray[i] = listArray[i+1];
- }
- listSize--;
- return true;
- }
- template<typename e>
- void AList<e>::setStart()
- {
- fence = 0;
- }
- template<typename e>
- void AList<e>::setEnd()
- {
- fence = listSize;
- }
- template<typename e>
- void AList<e>::prev()
- {
- if(fence == 0) return;
- fence--;
- }
- template<typename e>
- void AList<e>::next()
- {
- if(fence == listSize-1) return;
- fence++;
- }
- template<typename e>
- int AList<e>::leftLength() const
- {
- return fence;
- }
- template<typename e>
- int AList<e>::rightLength() const
- {
- return listSize-fence;
- }
- template<typename e>
- bool AList<e>::setPos(int pos)
- {
- if(pos>=0 && pos<=listSize)
- {
- fence = pos;
- return true;
- }
- return false;
- }
- template<typename e>
- bool AList<e>::getValue(e& item) const
- {
- if(listSize == 0)
- {
- return false;
- }
- item = listArray[fence];
- return true;
- }
- template<typename e>
- void AList<e>::print() const
- {
- std::cout<<"[ ";
- for(int i=0; i<listSize; i++)
- {
- std::cout<<((i==fence)?"| ":"")<<listArray[i]<<((i==listSize-1)?" ]\n":", ");
- }
- }
- Array-Based List Structure Visualization
- Testing the Array-Based list implementation
- int main()
- {
- AList<int> newList(10); //creating a list object
- //Adding values to the list
- newList.append(1);
- newList.append(12);
- newList.append(21);
- newList.append(132);
- newList.append(321);
- newList.append(89);
- newList.append(567);
- newList.append(43);
- newList.append(233);
- newList.append(67);
- //The following values won't be added to the list since maxSize = 10
- newList.append(33);
- newList.append(54);
- newList.append(78);
- newList.print();
- newList.setPos(8); //changing the position of the fence
- std::cout<<"\nAfter setting position to 8\n";
- newList.print();
- int value; //temporary variable to store values
- //getting the previous value
- newList.prev(); //setting the fence to the PREVIOUS value
- newList.getValue(value); //value is passed by reference
- std::cout<<"\nAccessing Previous Value: "<<value<<std::endl;
- //getting the next value
- newList.next(); //setting the fence to the NEXT value
- newList.getValue(value); //value is passed by reference
- std::cout<<"Accessing Next Value : "<<value<<std::endl;
- //Removing the current value at the fence
- newList.remove(value); //stores the value of the removed item
- std::cout<<"\nAfter removing a value: \n";
- newList.print();
- std::cout<<"\nDisplaying Removed Value: "<<value<<std::endl;
- newList.setStart(); //sets the fence to the beginning of the list
- std::cout<<"\nAfter setting position to the start:\n";
- newList.print();
- //Inserting the removed value(i.e.,233) at the beginning of the List
- newList.insert(value);
- std::cout<<"\nAfter Inserting removed value to the list again:\n";
- newList.print();
- return 0;
- }
Pros and Cons Of Pointer-Based List
Pros:
- No overhead if all array positions are filled. (Overhead is the amount of memory needed to maintain a data structure).
Cons:
- The array must be allocated in advance.
- Waste of memory if all array positions are not filled.
( If you like the content here, follow TSP on social media to get notified on future updates)
Comments
Post a Comment