Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
rgz_asd.docx
Скачиваний:
12
Добавлен:
11.03.2015
Размер:
54.12 Кб
Скачать

ListTwo.H

#ifndef _LIST_TWO_H_

#define _LIST_TWO_H_

const int ListTwoOk = 0;

const int ListTwoEmpty = 1; 

const int ListTwoNoMem = 2;

const int ListTwoEnd = 3;

const int ListTwoBegin = 4; 

extern int ListTwoError;

typedef int listTwoBaseType;

struct elementTwo {

    listTwoBaseType data;

    elementTwo *pred;

    elementTwo *next;

};

typedef struct ListTwo {

    elementTwo *Start;

    elementTwo *End; 

    elementTwo *ptr;

};

void InitListTwo(ListTwo *L); 

void PredPut(ListTwo *L, listTwoBaseType E);

void PostPut(ListTwo *L, listTwoBaseType E); 

void PredGet(ListTwo *L, listTwoBaseType *E);

void postGet(ListTwo *L, listTwoBaseType *E);

void MovePtrListTwoLeft(ListTwo *L);

void MovePtrListTwoRight(ListTwo *L);

void BeginPtrListTwo(ListTwo *L);

void EndPtrListTwo(ListTwo *L);

void DoneListTwo(ListTwo *L);

int EmptyListTwo(ListTwo *L); 

#endif

ListTwo.C

#include <stdio.h>

#include <stdlib.h>

#include "ListTwo.h"

int ListTwoError;

void initListTwo(ListTwo *L)

{

    L->Start = (elementTwo *) malloc(sizeof(elementTwo));

 

    if (!L->Start)

{

        listTwoError = listTwoNoMem;

        return;

    }

  

    L->End = (elementTwo *) malloc(sizeof(elementTwo));

 

    if (!L->End)

{

        listTwoError = listTwoNoMem;

        free((void *) L->Start);         

        L->Start = NULL;

        return;

    }

    L->Start->next = L->End;

    L->Start->pred = NULL;

    L->End->nextLink = NULL;

    L->End->predLink = L->Start;

    L->ptr = L->Start;

    listTwoError = listTwoOk;

}

 

void PredPut(ListTwo *L, listTwoBaseType E)

    if (L->ptr == L->Start)

{

        listTwoError = listTwoBegin;

        return;

    }

    ElementTwo *pntr = (elementTwo *) malloc(sizeof(elementTwo)); 

    if (!pntr)

{

        listTwoError = listTwoNoMem;

        return;

    }

    pntr->data = E; 

    pntr->pred = L->ptr->pred;

    pntr->next = L->ptr;    

 L->ptr->pred->next = pntr;

    L->ptr->post= pntr; // Текущий эл-т указывает слева на новый

}

void postPut(ListTwo *L, listTwoBaseType E)

{

    if (L->ptr == L->End)

{

        listTwoError = listTwoEnd;

        return;

    }

    elementTwo *pntr = (elementTwo *) malloc(sizeof(elementTwo));

    if (!pntr)

{

        listTwoError = listTwoNoMem;

        return;

    }

    pntr->data = E;

    pntr->next = L->ptr->next;

    pntr->pred = L->ptr;

    L->ptr->next->pred = pntr; 

    L->ptr->next = pntr;

}

void PredGet(ListTwo *L, listTwoBaseType *E)

{

    if (EmptyListTwo(L))

{

        return;

    }

    if (L->ptr == L->Start)

{

        listTwoError = listTwoBegin;

        return;

    }

    *E = L->ptr->predLink->data; 

    elementTwo *pntr = L->ptr->pred;

    L->ptr->predLink = pntr->predLink;

    pntr->predLink->nextLink = L->ptr;

    free((void *) pntr);

}

void postGet(ListTwo *L, listTwoBaseType *E)

{

    if (EmptyListTwo(L)) {

        return;

    }

    if (L->ptr == L->End)

{

        listTwoError = listTwoEnd;

        return;

    }

    *E = L->ptr->next->data;

    elementTwo *pntr = L->ptr->next;

    L->ptr->next= pntr->next;

    pntr->next->pred = L->ptr;

    free((void *) pntr);

}

 

void movePtrListTwoLeft(ListTwo *L)

{

    if (L->ptr == L->Start)

{

        listTwoError = listTwoBegin;

        return;

    }

    L->ptr = L->ptr->predLink;

}

void movePtrListTwoRight(ListTwo *L)

{

    if (L->ptr == L->End)

{

        listTwoError = listTwoEnd;

        return;

    }

    L->ptr = L->ptr->nextLink;

}

void BeginPtrListTwo(ListTwo *L)

{

    while (L->ptr != L->Start) {

        movePtrListTwoLeft(L);

    }

}

void EndPtrListTwo(ListTwo *L)

{

    while (L->ptr != L->End)

{

        movePtrListTwoRight(L);

    }

}

void doneListTwo(ListTwo *L)

{

    BeginPtrListTwo(L);

    ListTwoBaseType E; // Создание временного элемента

 

    while (!EmptyListTwo(L))

{

        PostGet(L, &E);

    }

     free((void *) L->Start);

    free((void *) L->End);

}

int EmptyListTwo(ListTwo *L)

{

    if (L->Start->next == L->End)

{

        listTwoError = listTwoEmpty;

        return 1;

    }

    return 0;

}

Модуль дека на отображение на ДЛС:

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