C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C++ PushBack/PushFront methods Posted by rjdfhorn_06 on 14 Oct 2008 at 8:44 PM
I'm trying to implement a PutFront method that places an integer at the FRONT of an array. So for example:

array before PutFront(1):
5
10
15
array after PutFront(1):
1
5
10
15

I've already created a PutBack method that puts an integer at the back of an array like this: (this is all in a source file, my main file will test these functions)

size_t xArray::PutBack(const int c){
if ( len == size ) {
int* temp = data;
size = size * 2;
data = new int[size];
for ( size_t i = 0; i < len; i++ ) {
data[i] = temp[i];
}
delete[] temp;
data[len] = c;
}
else {
data[len] = c;
}
return len = len+1;
}

I'm thinking that the PutFront method should be somewhat similar to the PutBack method. Can someone please help me create PutFront. This is all I have:(there is a header file with these private variables:

private:
size_t len;
size_t size;

//data of int array
int* data;
int* start;

size_t xArray::PushFront(const int c){
int* temp = data;
size = size * 2;
data = new int[size];
for ( size_t i = size; i < len; i++ ) {
data[i] = temp[i];
}
delete[] temp;
data[len] = c;
}
Report
Re: C++ PushBack/PushFront methods Posted by Lundin on 15 Oct 2008 at 12:36 AM
Copy the value into the first item of new array, then copy index i from the old array to index i+1 in the new array.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.