Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Type
- TVectorType = LongInt;
- TVector = Record
- Size, Capacity : LongInt;
- Nodes : Pointer;
- End;
- PVectorType = ^TVectorType;
- Var
- A : TVector;
- Procedure InitVector(Var V : TVector);
- Begin
- V.Size := 0;
- V.Capacity := 0;
- V.Nodes := Nil;
- End;
- Procedure PushBack(Var V : TVector; Element : TVectorType);
- Var
- Elem: PVectorType;
- Begin
- If V.Size = V.Capacity Then
- Begin
- If V.Capacity = 0 Then Inc(V.Capacity);
- Inc(V.Capacity, V.Capacity);
- V.Nodes := ReAllocMem(V.Nodes, V.Capacity * SizeOf(TVectorType));
- End;
- Elem := V.Nodes;
- Inc(Elem, V.Size);
- Elem^ := Element;
- Inc(V.Size);
- End;
- Procedure ShrinkToFit(Var V : TVector);
- Begin
- V.Nodes := ReAllocMem(V.Nodes, V.Size * SizeOf(TVectorType));
- V.Capacity := V.Size;
- End;
- Function PopBack(Var V : TVector) : PVectorType;
- Var
- X : PVectorType;
- Begin
- Dec(V.Size);
- X := V.Nodes;
- Inc(X, V.Size);
- PopBack := X;
- End;
- Function VectorAt(Var V : TVector; Index : LongInt) : PVectorType;
- Var
- X : PVectorType;
- Begin
- X := V.Nodes;
- Inc(X, Index);
- VectorAt := X;
- End;
- Procedure PrintVector(Var V : TVector);
- Var
- I : LongInt;
- Elem: PVectorType;
- Begin
- WriteLn('Vector size is: ', V.Size);
- Elem := V.Nodes;
- For I := 0 To V.Size - 1 Do
- Begin
- Write(Elem^, ' ');
- Inc(Elem);
- End;
- End;
- Procedure FreeVector(Var V : TVector);
- Begin
- FreeMem(V.Nodes);
- End;
- Begin
- InitVector(A);
- PushBack(A, 1);
- PushBack(A, 2);
- PushBack(A, -1);
- PushBack(A, -3);
- PushBack(A, -3);
- PushBack(A, -3);
- PushBack(A, -3);
- PushBack(A, -3);
- PushBack(A, -3);
- PushBack(A, -3);
- WriteLn(A.Capacity);
- ShrinkToFit(A);
- PushBack(A, -1);
- WriteLn(A.Capacity);
- VectorAt(A, 2)^ := 5;
- PrintVector(A);
- FreeVector(A);
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement