C双向列表删除项目

时间:2015-11-22 22:16:34

标签: c list

我有问题。下面的代码创建了一个双向循环列表。当我想删除specyfic键值的项目时,一切都正常。当我想在删除另一个项目后添加项目时,问题就出现了。当我显示列表时,它不会出现。你有什么想法吗?

    #include <iostream>
#include <time.h>
#include <vector>
#include <stdlib.h>


struct List
{
        int key;
        double val1, val2, val3;
        struct List *next, *prev;
}*element, *temp, *head;

bool ifrepeat = false,ifdelete;

void AddOne(struct List *&head1, int x)
{
    List *tmp = head1;

    if (head1 != NULL)
    {
        do
        {
            if (head1->key == x)
            {
                ifrepeat = true;
            }
            head1 = head1->next;
        } while (head1 != tmp);
    }
    if (ifrepeat)
    {
        printf("Podana wartosc klucza juz istnieje!\n");
        ifrepeat = false;
    }
    else
    {

        head1 = tmp;
        if (head1 == NULL)
        {

            head1 = (struct List*)malloc(sizeof(struct List));
            head1->key = x;
            head1->val1 = 1 + (rand() % 1000);
            head1->val2 = 1 + rand() % 1000;
            head1->val3 = 1 + rand() % 1000;
            head1->next = head1;
            head1->prev = head1;

        }
        else
        {
            if (element == NULL)
            {
                element = (struct List*)malloc(sizeof(struct List));
                element->key = x;
                head->next = element;
                element->prev = head1;
                element->next = head1;
                head1->prev = element;
                temp = element;
            }

            else
            {
                element = (struct List*)malloc(sizeof(struct List));
                element->key = x;
                temp->next = element;
                element->prev = temp;
                element->next = head1;
                head1->prev = element;
                temp = element;
            }
        }
    }
}

void ShowBegin(struct List *head)

{
    if (head == NULL)
        printf("Lista jest pusta!");
    else
    {
        struct List *temphead;

        temphead = head;
        printf("%d ", head->key);
        head = head->next;
        while (head != temphead)
        {
            printf("%d ", head->key);
            head = head->next;
        }
        printf("\n");
    }
}

void AddMany(struct List *&head1, int x)
{
    AddOne(head, 10+(rand()%1000)*(rand()%1000));
    int tmpkey;
    List *tmp = head1;






    for (int i = 0; i < x; i++)
    {
        tmpkey = 10 + (rand() % 100)*(rand() % 1000);
        while (head1 != tmp)
        {
            while (head->key == tmpkey)
            {
                tmpkey = 10 + (rand() % 100)*(rand() % 1000);
                head1 = tmp;
            }
            head1 = head1->next;
        }

            AddOne(head, tmpkey);
            head1 = tmp;
            head1 = head1->next;


    }



}

void Sort(struct List *&head1)
{
    int max;
    List *maxPos,*tmp;

    maxPos = head1;
    max = head1->key;

    while (true)
    {
        head1 = head1->next;
        if (head1->key > max)
        {
            tmp = head1;
            head1 = maxPos;
            maxPos = tmp;
            max = maxPos->key;
            head1 = head;
        }

    }


}

void CreateList()
{
    head = NULL;
    element = NULL;
    temp = NULL;
}

void Delete(struct List *&head1, int x)
{

    struct List *temphead;

    temphead = head1;

    if (head == NULL)
    {
        printf("Lista jest pusta!\n");
        return;
    }
    else
    {



        head1 = head1->next;

        if (head1 == temphead)
        {
            if (head1->key == x)
            {
                free(head1);
                head1 = NULL;
                ifdelete = true;
            }
            else
                ifdelete = false;

        }
        else
        {
            while (head1 != temphead)
            {
                if (head1->key == x)
                {


                    List *temp;
                    temp = head1;
                    head1->prev->next = head1->next;
                    head1->next->prev = head1->prev;
                    head1 = head1->next;
                    while (head1 != temphead)
                    {

                    }

                    free(temp);
                    ifdelete = true;
                    break;

                }
                else
                    ifdelete = false;



                head1 = head1->next;
            }


        }
        if (!ifdelete)
        {
            printf("Element o podanym kluczu nie istnieje!\n");

        }
    }
}

void ShowEnd(struct List *&head)
{
    if (head == NULL)
        printf("Lista jest pusta!");
    else
    {
        struct List *temphead;

        temphead = head;

        head = head->prev;
        while (head != temphead)
        {
            printf("%d ", head->key);
            head = head->prev;
        }
        printf("%d", temphead->key);
        printf("\n");
    }
}

void DeleteAll(struct List *&head)
{
    free(head);
    head = NULL;
}



int main()
{
    int X=1000, k1=5, k2=2, k3=10, k4=-1;

    /*FILE* plik = fopen("inlab02.txt", "r");
    if (plik == NULL)
        return -257;
    fscanf(plik, "%d %d %d %d %d", &X, &k1, &k2, &k3, &k4);
    fclose(plik);*/

    srand(time(NULL));

    clock_t begin, end;
    double time_spent;
    begin = clock();

    CreateList();
    Delete(head, k1);
    ShowBegin(head);
    AddMany(head, 10);
    AddOne(head, k2);
    Delete(head, k2);

    AddOne(head, k4);

    //Delete(head, k4);
    ShowBegin(head);
    //ShowEnd(head);
    //DeleteAll(head);
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("Program wykonal obliczenia w %f", time_spent);
    getchar();
    return 0;
}

0 个答案:

没有答案