Консультация № 161442
26.02.2009, 09:51
0.00 руб.
0 1 1
Здравствуйте, будьте добры, помогите мне сделать программу на с++, дек с плавающей головой и плавающим хвостом

Обсуждение

Неизвестный
26.02.2009, 21:31
общий
26.02.2009, 21:39
это ответ
Здравствуйте, Эстеркин Виктор!
Вот, наверное то, что вам нужно:



Приложение:

// Deq.h
//---------------------------------------------------------------------------

#ifndef DeqH
#define DeqH
class ListNode {

public:
ListNode();
~ListNode();

ListNode* nextPtr;
ListNode* prevPtr;

int value;

};

class Deq{

public:
Deq();
~Deq();

void Put(int newValue);
void PutAtTheBeginning(int newValue);
void DeleteHead();
void DeleteTail();
int GetNumber(int Number);
int Get();
int length;

private:
bool isEmpty();

ListNode* head;
ListNode* tail;

int nodeValue;
};
//---------------------------------------------------------------------------
#endif



//Deq.cpp
//---------------------------------------------------------------------------


#pragma hdrstop

#include "Deq.h"

//---------------------------------------------------------------------------


// Конструктор класса Deq
Deq::Deq() {
head = tail = 0;
length = 0;
};

// Конструктор класса ListNode
ListNode::ListNode() {
};

// Деструктор класса Deq
Deq::~Deq() {
if (!isEmpty()) {
ListNode* currentPtr = head, *newTmp;

while (currentPtr != 0){
newTmp = currentPtr;
currentPtr = currentPtr->nextPtr;
delete newTmp;
};

};

};

// Деструктор класса ListNode
ListNode::~ListNode() {};

// Проверка списка на наличие значений
bool Deq::isEmpty() {
return !(length);
};

//Ф-ия добавления элемента в конец дека
void Deq::Put(int newValue) {
if (isEmpty()) {
head = new ListNode();
head->value = newValue;
head->nextPtr = head->prevPtr = 0;
tail = head;
nodeValue = tail->value;
}
else {
ListNode* newTmp = new ListNode();
newTmp->value = newValue;
tail->nextPtr = newTmp;
newTmp->prevPtr = tail;
newTmp->nextPtr = 0;
tail = newTmp;
nodeValue = newTmp->value;
};
length++;
};


// Ф-ия добавления элемента в начало дека
void Deq::PutAtTheBeginning(int newValue){
if (isEmpty()) {
head = new ListNode();
head->value = newValue;
head->nextPtr = head->prevPtr = 0;
tail = head;
nodeValue = tail->value;
}
else {
ListNode* newTmp = new ListNode;
newTmp->value = newValue;
head->prevPtr = newTmp;
newTmp->prevPtr = 0;
newTmp->nextPtr = head;
head = newTmp;
nodeValue = newTmp->value;
};
length++;
};

//Ф-ия удаления конца списка
void Deq::DeleteTail(){
if (length == 1)
delete tail;
else {
tail = tail->prevPtr;
delete tail->nextPtr;
tail->nextPtr = 0;
};
length--;
};

//Ф-ия удаления начала списка
void Deq::DeleteHead(){
if (length == 1)
delete head;
else {
head = head->nextPtr;
delete head->prevPtr;
head->prevPtr = 0;
};
length--;
};




//Ф-ия получения элемента массива
int Deq::GetNumber(int Number){
ListNode* currentPtr = head;
for (int i = 1; i<Number; i++){
currentPtr = currentPtr->nextPtr;
};
return currentPtr->value;
};

//Ф-ия возвращения элемента списка
int Deq::Get() {
return nodeValue;
};
#pragma package(smart_init)



//Main.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Deq.h"
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Deq d;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Button3->Enabled = false;
Button4->Enabled = false;

};

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (Edit1->Text == ""){
ShowMessage("Ââåäèòå ÷èñëî");
}
else {
int newNumber = StrToInt(Edit1->Text);
d.Put(newNumber);
Label1->Caption = Label1->Caption + IntToStr(d.Get()) + " ";

Button3->Enabled = true;
Button4->Enabled = true;
};
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{

if (Edit1->Text == ""){
ShowMessage("Ââåäèòå ÷èñëî");
}
else {
int newNumber = StrToInt(Edit1->Text);
d.Put(newNumber);
Label1->Caption = IntToStr(d.Get()) + " " + Label1->Caption;

Button3->Enabled = true;
Button4->Enabled = true;
};
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{ d.DeleteTail(); Label1->Caption = "";
for (int i = 1; i<=d.length; i++){
Label1->Caption = Label1->Caption + " " + IntToStr(d.GetNumber(i));
};
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{ d.DeleteHead(); Label1->Caption = "";
for (int i = 1; i<=d.length; i++){
Label1->Caption = Label1->Caption + " " + IntToStr(d.GetNumber(i));
};
}
//---------------------------------------------------------------------------
Форма ответа