Question

In: Computer Science

C++ and leave comments explaining. Thank you You are given two STL lists X and P...

C++ and leave comments explaining. Thank you

You are given two STL lists X and P where n P is already in sorted order. Write a valid C++ function

printPositions(X,P) that prints the elements in X specified by P. For example, if P = 0, 3, 7, 8, the elements in positions 0 (head of the list), 3, 7, and 8 in

X are printed. You may use only the public STL container operations. Also specify the running time of your algorithm using Big-Oh notation.

Hint: You may wish to use iterators while keeping track of the position of items being iterated through.

Solutions

Expert Solution

Ans:-

Maybe you are as of now utilizing C++ as your principle programming dialect to take care of TopCoder issues. This implies you have effectively utilized STL just, on the grounds that exhibits and strings are passed to your capacity as STL articles. You may have seen, however, that numerous coders figure out how to compose their code substantially more rapidly and succinctly than you.

Alternately maybe you are not a C++ software engineer, but rather need to end up distinctly one in view of the colossal usefulness of this dialect and its libraries.

Despite what kind of opinion you're maintaining, this article can offer assistance. In it, we will audit a portion of the capable components of the Standard Template Library (STL) – an extraordinary device that, occasionally, can spare you a ton of time in a calculation rivalry.

The least difficult approach to get acquainted with STL is to start from its holders.

Holders

At whatever time you have to work with numerous components you require some sort of compartment. In local C however not C++ there was just a single kind of compartment: the cluster.

The issue is not that exhibits are constrained Instead, the principle issue is that numerous issues require a holder with more prominent usefulness.

Add some string to a compartment.

Expel a string from a compartment.

Figure out if a string is available in the compartment.

Give back various particular components in a holder.

Emphasize through a compartment and get a rundown of included strings in some request.

Obviously, one can execute this usefulness in an ordinal exhibit. Be that as it may, the insignificant execution would be extremely wasteful. You can make the tree-of hash-structure to fathom it fasterly, yet think a bit: does the execution of such a compartment rely on upon components we will store? Do we need to re-execute the module to make it useful, for instance, for focuses on a plane yet not strings?

If not, we can build up the interface for such a compartment once, and after that utilization wherever for information of any sort. That, to put it plainly, is the possibility of STL holders.

Before we start

At the point when the program is utilizing STL, it ought to #include the proper standard headers. For most holders the title of standard header coordinates the name of the compartment, and no expansion is required. For instance, in the event that you will utilize stack, simply include the accompanying line toward the start of your program:

#include <stdio.h>

int product(int an, int b, int file, int measure)

{ int temp = 0;

for (file = 0; record < estimate; index++)

temp += a * b;

return temp;

}

principle()

{ int a[10];/* instate somehow */

int b[10];/* instate somehow */

int i;

printf("%d\n",product(a[i],b[i],i,10));

return 0;

}

STL Lists

A few focuses about the STL list holder: •implements a great rundown information structure

•supports an element bidirectional straight rundown

•unlike a C++ exhibit, the items the STL list contains can't be gotten to specifically.

•is characterized as a format class, implying that it can be altered to hold objects of any sort.

•responds like an unsorted rundown . Be that as it may, there are capacities accessible for sorting the rundown

Programm:-

#include<stdio.h>

#include<stdlib.h>

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

/* capacity to embed a new_node in a rundown. Take note of that this

work anticipates that a pointer will head_ref as this can change the

leader of the information connected rundown (like push())*/

void sortedInsert(struct node** head_ref, struct node* new_node)

{

struct node* current;

/* Special case for the head end */

in the event that (*head_ref == NULL || (*head_ref)- >data >= new_node->data)

{

new_node->next = *head_ref;

*head_ref = new_node;

}

else

{

/* Locate the hub before the purpose of addition */

current = *head_ref;

while (current->next!=NULL &&

current->next->data < new_node->data)

{

current = current->next;

}

new_node->next = current->next;

current->next = new_node;

}

}

/* An utility capacity to make another hub */

struct hub *newNode(int new_data)

{

/* allot hub */

struct node* new_node =

(struct node*) malloc(sizeof(struct hub));

/* put in the information */

new_node->data = new_data;

new_node->next = NULL;

return new_node;

}

/* Function to print connected rundown */

void printList(struct hub *head)

{

struct hub *temp = head;

while(temp != NULL)

{

printf("%d ", temp->data);
}


Related Solutions

C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u, y) counts...
C++ mainDisplay.cpp Write the function displayAt that receives the two STL lists; vList and pList. The...
C++ mainDisplay.cpp Write the function displayAt that receives the two STL lists; vList and pList. The function should display the elements in vList that are in positions specified by pList. For example, if pList has the elements (2, 5, 6, 8) then the elements in positions 2, 5, 6, and 8 in vList are displayed. Use only the public STL container operations.
Write a general example of interrupts in C language with comments. Thank you
Write a general example of interrupts in C language with comments. Thank you
If answer can be shown using a c++ program and leave comments it will be very...
If answer can be shown using a c++ program and leave comments it will be very appreciated!!! A bank charges $10 per month plus the following check fees for a commercial checking account: $0.10 each for fewer than 20 checks $0.08 each for 20-39 checks $0.06 each for 40-59 checks $0.04 each for 60 or more checks The bank also charges an extra $15.00 if the balance of the account falls below $400 (before any check fees are applied). Write...
Can you please do this in C asap. Promise to leave a awesome rating! Thank you...
Can you please do this in C asap. Promise to leave a awesome rating! Thank you in advance. Question and template below Instructions- LINKED LIST In C 'objects' => structs (struct - collection of data, not methods) Linked List Collection of objects which are all connected to each other Each Object 'points' to the next item in the list Node (struct) ' O ' - data - nextNode Our linked list will store data(structs containing integers) in order from smallest...
C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists,...
C++ PROJECT Create a sudoku game that involves the following concepts: STL library. (Maps, Sets, Lists, Stacks and Queues), with Iterators and Algorithms Show especially Especially Algorithm/Iterators/Containers in the STL Minimum of 900 lines Include Full documentation
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
Card Shuffling and Dealing (C++) All in one file with comments explaining please! Create a program...
Card Shuffling and Dealing (C++) All in one file with comments explaining please! Create a program to shuffle and deal a deck of cards. The program should consist of a class Card, class DeckOfCards and a driver program. Class Card should provide: a)      Data members face and suit of type int. b)      A constructor that receives two ints representing the face and suit and uses them to initialize the data members. c)      Two static arrays of strings representing the faces...
Profit = Revenue - Cost P(x) = R(x) - C(x) Given: R(x) = x^2 - 30x...
Profit = Revenue - Cost P(x) = R(x) - C(x) Given: R(x) = x^2 - 30x Given: C(x) = 5x + 100 X is hundreds of items sold / P, R, C are in hundreds of dollars (1) Determine the Initial Cost? (2) Determine the maximum Profit and number of items required for that profit? (3) Determine the maximum Revenue and number of items required for that revenue? (4) Find the break even points, P(x) = 0. What do the...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a square root is achieved using the sqrt() function, but a warning will be issued when the argument is negative. Consider the following code which is designed to test whether a given value is positive before checking whether the square root of the value is less than 5. testValue <-7 (testValue > 0) & (sqrt(testValue) < 5) ## [1] TRUE testValue <--7 (testValue > 0)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT