Ім'я файлу: 4.docx
Розширення: docx
Розмір: 18кб.
Дата: 13.05.2020
скачати

Файл List.h:

#pragma once

typedef int datatype;

struct Item

{

datatype data;

Item* next;

};

struct Items

{

datatype data;

Items* next;

Items* previous;

};
//stack:

void push(int );

void pop();

void show();
//queue:

void enqueue(int);

void dequeue();

void showqueue();
//double list(deq):

void add_begin(int);

void add_end(int);

void del_begin();

void del_end();

Items* search(int);

void add_mid(int, int);

void del_mid(int);

void showlist();

Файл List.cpp:

#include "List.h"

#include
using namespace std;

Item* head = NULL, * front = NULL, *rear = NULL, *tempq = NULL;

Items* temp = NULL, *first = NULL, *last =NULL;

//---------------------------------stack--------------------------------

void push(int data)

{

Item* temp = new Item;

temp->data = data;

temp->next = head;

head = temp;

}

void pop()

{

if (head == NULL) {

cout << "There are no items to remove in the stack";

}

if (head != NULL) {

Item* temp = head;

head = temp->next;

delete temp;

}

}

void show()

{

Item* temp = head;

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

}

}

//---------------------------------queue--------------------------------

void enqueue(int data)

{

Item* tempq = new Item;

tempq->data = data;

tempq-> next = NULL;

if (front == NULL) {

front = tempq;

}

else {

rear->next = tempq;

}

rear = tempq;

}

void dequeue()

{

{

if (front == NULL) {

cout << "There are no items to remove in the stack";

}

if (front != NULL) {

Item* tempq = front;

front = tempq->next;

delete tempq;

}

}

}
void showqueue()

{

{

Item* tempq = front;

while (tempq != NULL) {

cout << tempq->data << " ";

tempq = tempq->next;

}

}

}

//---------------------------------doule list--------------------------------

void add_begin(int data)

{

Items* temp = new Items;

temp->data = data;

temp->next = first;

temp->previous = NULL;

if (first != NULL) {

first->previous = temp;

}

else {

last = temp;

}

first = temp;

};
void add_end(int data)

{

Items* temp = new Items;

temp->data = data;

temp->next = NULL;

temp->previous = last;

if (first != NULL) {

last->next = temp;

}

else {

first = temp;

}

last = temp;

}

void del_begin()

{

if (first == NULL) {

cout << "There are no items to remove in the stack";

}

else {

Items* temp = first;

first = temp->next;

if (first != NULL) {

first->previous = NULL;

}

else {

last = NULL;

}

delete temp;

}

};

void del_end()

{

if (last == NULL) {

cout << "There are no items to remove in the stack";

}

else {

Items* temp = last;

last = temp->previous;

if (first != NULL) {

last->next = NULL;

}

else {

first = NULL;

}

delete temp;

}

}
Items* search(int k)

{

Items* temp = first;

for (int i = 1; i < k; i++) {

temp = temp->next;

}

if (temp != NULL) {

return temp;

}

else

{

cout << "There are no " << k + 2 << "elements ar this deq" << endl;

return 0;

}

};

void add_mid(int data, int k)

{

Items* dat;

if (k == 1) {

add_begin(data);

}

else {

dat = search(k - 1);

if (dat == NULL)

return;

else {

if (dat == last)

add_end(data);

else {

Items* pkey = search(k);

Items* temp = new Items;

temp->data = data;

temp->next = pkey->next;

temp->previous = pkey;

pkey->next = temp;

(temp->next)->previous = temp;

}

}

}

}
void del_mid(int k)

{

Items* dat;

if (k == 1) {

del_begin();

return;

}

dat = search(k);

if (dat == NULL) { return; }

else {

if (dat == first) { del_begin(); }

if (dat == last) del_end();

else {

Items* pkey = search(k);

(pkey->previous)->next = pkey->next;

(pkey->next)->previous = pkey->previous;

delete pkey;

}

}

}
void showlist()

{

Items* temp = first;

while (temp != NULL) {

cout << temp->data << " ";

temp = temp->next;

}

}

скачати

© Усі права захищені
написати до нас