Dear friends,
These days ,I am learning something related to linear list.In implmenting the function of realloc.I have been pullzed for several days,although I can use "include" method to directly use the function.
However , I deeply wonder that how can I write my own code to solve such questions in c++.
Willing to receive yours help.
Thanks a lot.
-------------------tiredoy
- 1 Contributor
- forum1 Reply
- 25 Views
- 6 Years Discussion Span
- comment Latest Post by tiredoy
#include<iostream>
#include"malloc.h"
using namespace std;
class myList{
public:
int* list;
int size;
};
class LList
{
private:
int Size;//index value that represents the exist data.
int OldSize;
int MaxSize;
int StepIncrz;
myList* myll;
public:
void Remalloc()
{
myList* newptr;
OldSize=MaxSize;
MaxSize+=StepIncrz;
newptr->list=new int[MaxSize];
//copy old to new
for(int i=0;i<OldSize;++i)
{newptr->list[i]=myll->list[i];}
myll=newptr;
};
LList()
{
Size=0;
StepIncrz=30;
myll->list=new int[10];
MaxSize=10;
};
bool CheckFull()
{return (Size>=MaxSize);};
void addValue(int data)
{
if(CheckFull())
{
Remalloc();
}
myll->list[Size]=data;
++Size;
};
int GetData(int position)
{
if(position >=Size)
{
cout<<"Error!";
return 0;}
else {
return myll->list[position];
}
}
};
void main()
{
LList a;
a.addValue(3);
a.addValue(5);
a.addValue(44);
a.addValue(23);
a.addValue(12);
a.addValue(56);
for(int j=0;j<=5;++j)
{cout<<a.GetData(j);}
}