PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 26, 2022

[FIXED] How to insert an element at some specific position in the list using classes?

 October 26, 2022     c++, oop     No comments   

Issue

#include<iostream>
using namespace std;
template<typename T>
class List
{
public:
    T *values;
    int capacity;
    int counter;
public:
    List()
    {
        values = NULL;
        capacity = 0;
        counter = 0;
    }
    List(int cap)
    {
        capacity = cap;
        values = new T[cap];
        counter = 0;
    }
    bool insert(T item)
    {
        if (isFull() == false)
        {
            values[counter] = item;
            counter++;
            return true;
        }
        return false;
    }
    bool insertAt(T item, int index)
    {
        
        if (isFull() == false && index < counter)
        {
            capacity++;
            for (int i = capacity; i > index; i--)
                values[i] = values[i - 1];
            
            values[index] = item;
            
            return true;
        }
        return false;
    }
    bool isFull()
    {
        if (counter == capacity)
        {
            return true;
        }
        return false;
    }
    void print()
    {
        for (int i = 0; i < capacity; i++)
        {
            cout << values[i] << " ";
        }
    }
    
};
int main()
{
    List<int> obj1(5);
    obj1.insert(1); //0
    obj1.insert(2); //1
    obj1.insert(3); //2
    obj1.insert(4); //3
    
    obj1.insertAt(3, 1);
    obj1.values[1];
    obj1.print();
    
}

Kindly look into this program I have to insert an element at given position. But when I run this program I am getting garbage at the end element of an array. Kindly check and let me know where is the problem? Please check the insertAt function I think this function has some logical error. I have added the main function when I call print function it give garbage at the last index


Solution

bool insertAt(T item, int index)
    {
        
    
        if (!isFull() && index < counter)
        {
            for (int i = counter; i > index; i--) {
                values[i] = values[i - 1];
            }
            values[index] = item;
            counter++;
            return true;
        }
        return false;
    }

void print()
    {
        for (int i = 0; i < counter; i++)
        {
            cout << values[i] << " ";
        }
    }

I have corrected these two function. Try these functions instead of the one you implemented



Answered By - Mudassir Waheed
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing