Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Zvit_Kursak_2_list-6.docx
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
216.52 Кб
Скачать

Список літератури

  1. Грегори К. Использование Visual С++. Специальное издание. - М.: «Диалектика», 1999.

  2. Мешков А.В., Тихомиров Ю.В. Visual С++ и MFC. Пер. с англ. – 2-е изд. перераб. и доп. – СПб.: БХВ - Петербург, 2002. – 1040 с.

  3. Страуструп Б. Язык программирования С++. Третье издание. - М.: «Издательство Бином», 1999.

  4. Трамбле Ж., Соренсон П. Введение в структуры данных. – М.:Машиностроение, 1982

  5. Уильям Топп, Уильям Форд. Структуры данных в С++. – М.:Бином, 2000 - 700 с

Додаток а Конструювання атд – список

файл element.h

#include<iostream>

usingnamespace std;

class element

{

public:

int value;

element *prew;

element *next;

};

файл list.h

#include"element.h"

class List

{

public:

List(){

head=0;

tail=0;

}

virtual~List(){

head=head->next;

while(head){

delete head->prew;

head=head->next;

}

}

void push_first(int x){

element *temp = new element;

temp->value = x;

temp->prew = 0;

temp->next = head;

head = temp;

if(!head->next)

tail=head;

}

void push_last(int x){

element *temp = new element;

temp->value = x;

temp->next = 0;

tail->next=temp;

temp->prew = tail;

tail = temp;

}

void push_after(element *pos,int x){

element *temp = new element;

temp->next = pos->next;

pos->next->prew = temp;

pos->next = temp;

temp->prew = pos;

temp->value = x;

}

void push(int x){

element *temp=head;

if(temp==0){

push_first(x);

return;

}

if(temp->value > x){

push_first(x);

return;

}

while(temp->next){

if(temp->next->value > x){

push_after(temp, x);

return;

}

temp=temp->next;

}

push_last(x);

}

void Show(){

element *temp = head;

cout<<"\nList: ";

while(temp){

cout<<temp->value<<" ";

temp = temp->next;

}

cout<<endl;

}

void del_elem_pos(element *pos){ //видаляє елемент за позицією pos

if(pos == head){

head=pos->next;

pos->prew=0;

}

else

if(pos == tail){

tail=pos->prew;

pos->prew->next=0;

}

else{

pos->prew->next = pos->next; //тут відбувається переприсвоєння вказівників

pos->next->prew = pos->prew; //тобто таким чином вилучається даний елемент

}

delete pos; //тут видаляється динамічна память, виділена для даного елемента

}

void del_elem(int x){ //видаляє елемент із значенням x

if(head==0){

cout<<"\nU spysku nema elementiv!\n";

return;

}

element *temp = head;

bool k=true;

if(temp->value == x){

element *s_tmp = temp->next;

del_elem_pos(temp);

temp=s_tmp;

k=false;

}

while(temp){

if(temp->value == x){

element *s_tmp = temp->prew;

del_elem_pos(temp);

temp=s_tmp;

k=false;

}

temp = temp->next;

}

if(k){

cout<<"\nU spysku nema takoho elementa!\n";

}

}

void unionList(List *first, List *second){

head=first->head;

first->tail->next=second->head;

second->head->prew = first->tail;

tail=second->tail;

}

private:

element *head, *tail;

};

файл main.cpp

#include"List.h"

int main(void)

{

List *A=new List;

int kilk;

int temp;

cout<<"Vvedit rozmir poslidovnosti: ";

cin >>kilk;

for(int i=0; i<kilk; i++){

cout <<"Vvedit element: ";

cin >> temp;

if(temp>0){

cout <<"push("<< temp <<")\n";

A->push(temp);

}

if(temp<0){

cout <<"delete("<< -temp <<")";

A->del_elem(-temp);

}

A->Show();

}

return 0;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]