Question

Explain in your own words the algorithm (= sequence of steps) you would use in order...

Explain in your own words the algorithm (= sequence of steps) you would use in order to implement a function Vector::pop_front() and a function List::operator [](int i). Although these functions are not provided in the Standard Template Library (STL) for vector and list, we could certainly create them. In your explanations above, state the likely (and good) reason why the STL does not include these functions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


push_back() and pop_back() functions is included in vector as it takes O(1) time to execute.
- If we want to insert an element in the beginning (push_front()) of the vector then we will have to shift all the existing elements by 1 in the right direction and then put the element at the beginning.
- Similarly if we want to delete an element from the beginning of vector (pop_front()) then we have to shift all existing elements by 1 in the left direction because the starting index becomes vacant.
So both the operations will take O(n) time to perform and this is why STL does not include these functions in vector.

Algorithm for function Vector::pop_front():
Step 1:
Read vector V
Step 2: for i = 0 to V.size() -1 repeat step 3
Step 3: V[i] = V[i+1]
Step 4: Delete last value using V.pop_back();

Since List does not provide random access of it's node. So if we want to access any node of list we have to traverse from the beginning of the list (list.begin()).
So to access the n-th node, the execution will take O(n) time and this is why STL does not include this function too.

Algorithm for function List::operator[](int i)
Step 1:
Read list L
Step 2: Read i which is passed in parameter
Step 3: Set the iterator it as list.begin using list <int> :: iterator it = list.begin();
Step 4: for i = 0 to i repeat step 5
Step 5: increase it by 1 using it++
Step 6: return the value of iterator (*it)

Add a comment
Know the answer?
Add Answer to:
Explain in your own words the algorithm (= sequence of steps) you would use in order...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion,...

    I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...

  • Exercise 1: Sorting void mysort(vector & v); Write a function called mysort that rearranges elements of...

    Exercise 1: Sorting void mysort(vector & v); Write a function called mysort that rearranges elements of a vector v so that they form an increasing sequence of values. Allow for different vector positions to contain the same value. Develop your own sorting algorithm; do not use a predefined sort routine from the standard library. Implement a simple sorting algorithm rather than an efficient algorithm. Write code that thoroughly tests your function. Express your tests using assertions. Exercise 2: Comparison For...

  • 22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what...

    22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what a word ladder is: Word Ladder Your program must use a list to store the dictionary of words and then use stacks of strings and a queue of these stacks to solve for and output the word ladder. You are required to use the Standard Template Library (STL) list, stack, and queue. You must implement the WordLadder class. The class is declared in WordLadder.h....

  • In your own words how you would describe the function of the following organelles? What are...

    In your own words how you would describe the function of the following organelles? What are the function of the lysosome? What are the function of the rough and smooth endoplasmic reticulum? What are the function of the ribosomes? What are the functions of the mitochondria? What are the functions of the cell membrane?

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • In your own words, in about 4-5 sentences, explain how you would solve a typical equilibrium...

    In your own words, in about 4-5 sentences, explain how you would solve a typical equilibrium problem. List all the steps, and describe the equations/mathematics you would use, but do not actually write down the equations.

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • In 500 words or less describe with your own words the steps included in a Web session. You can us...

    In 500 words or less describe with your own words the steps included in a Web session. You can use an example describing the internal steps in the system from the moment a person wishes to visit a website to the moment the website information is displayed on the computer screen for the user. You may use a point-form numbered list to describe each step of the process. Make sure to describe concepts such as: Web Browser, URL, Doman name,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT