Dynamiczna lista dwukierunkowa - wskaźniki

0

czesc. dostałem kod od kolegi na liste. napisałem swoja liste na podstawie tego, co dałem rade sam wywniskowwac i nauczyc sie z jego kodu + internetu. ale nadal nie moge znaleźć - jak mi sie wydaje - podstawowych informacji na temat wskaznikow... czy mógłby mi ktos wytlumaczyc jakim cudem w kodzie uzyte zostalo tmp jako struktura (koleka nie jest zbytnio zyczliwy)? wgl w tym kodzie jest masa błędów, których nie potrafie sam znaleźć :/ pomozcie proszuuu

ps. jak macie pisac "eee, bylo juz" "wez sie naucz a nie dupe zawracasz" to sie nie odzywajcie tutaj. tego mi nie potrzeba...

#include <stdio.h>
#include <stdlib.h>

struct elista
{
    int x;
    struct elista* next;
    struct elista* prev;
};
struct lista
{
    struct elista* head;
    struct elista* tail;
};

struct lista* dodaj(struct lista* q);
struct lista* usun(struct lista* q);
void drukowanie(struct lista* q);
void szukaj(struct lista* q, int el);

int main()
{
    int wybor=1, x, zm;

    struct lista* q;
    q=malloc(sizeof(struct lista));
    q->head=q->tail=NULL;

    while(wybor!=0)
    {
        system("cls");
        printf("1. Dodanie elementu do list.\n2. Usuwanie elementu z listy.\n3. Wyszukiwanie elementu na liscie.\n4. Zawartosc listy.\n0. Wyjscie.\nWpisz swoj wybor: ");
        scanf("%d",&wybor);
        if(wybor==1)
            q=dodaj(q);
        if(wybor==2)
            q=usun(q);
        if(wybor==3)
        {
            system("cls");
            if(q->head!= NULL)
            {
                printf("Podaj szukana wartosc: ");
                scanf("%d",&zm);
                szukaj(q, zm);
            }
            else
            {
                printf("Lista jest pusta!");
                system("PAUSE");
            }
        }
        if(wybor==4)
        {
            system("cls");
            if(q->head!=NULL)
            {
                drukowanie(q);
            }
            else
            {
                printf("Lista jest pusta!");
                system("PAUSE");
            }
        }
        if(wybor==0)
        {
            system("cls");
            printf("Zakonczenie pracy programu...");
        }
        if(wybor!=1 && wybor!=2 && wybor!=0 && wybor!=3 && wybor!=4)
        {
            printf("\nNie ma opcji o takim numerze!\n");
            system("PAUSE");
        }
    }
    return 0;
}

struct lista* dodaj(struct lista* q)
{
    int wybor=1, poz, wartosc, i;
    while(wybor!=0)
    {

        system("cls");
        printf("1. Dodaj na wybrana pozycje.\n2. Dodaj za podana pozycje.\n0. Powrot do menu glownego.\nWybor: ");
        scanf("%d",&wybor);

        if(wybor==1)
        {
            system("cls");
            printf("Podaj pozycje: ");
            scanf("%d",&poz);
            printf("Podaj wartosc: ");
            scanf("%d",&wartosc);
            struct elista* z=malloc(sizeof(struct elista));
            z->next=z->prev=NULL;
            z->x=wartosc;
            struct elista * tmp;
            tmp=q->head;

            if(q->head==NULL)
            {
                z->next=NULL;
                z->prev=NULL;
                q->head=z;
                q->tail=z;
            }
            else
            {
                if(poz==1)
                {
                    z->next=tmp;
                    tmp->prev=z;
                    z->prev=NULL;
                    q->head=z;
                }
                else
                {
                    while(tmp->next!=NULL && i<poz-1)
                    {
                        tmp=tmp->next;
                        i++;
                    }
                    if(tmp->next!=NULL)
                    {
                        z->next=tmp->next;
                        tmp->next->prev=z;
                        tmp->next=z;
                        z->prev=tmp;
                    }
                    else
                    {
                        tmp->next=z;
                        z->prev=tmp;
                        z->next=NULL;
                        q->tail=z;
                    }
                }
            }
        }
        if(wybor==2)
        {
            system("cls");
            printf("Podaj wartosc elementu do zamiany: ");
            scanf("%d",&poz);
            printf("Podaj wartosc do wpisania za ten element: ");
            scanf("%d",&wartosc);

            struct elista* z=malloc(sizeof(struct elista));
            z->next=z->prev=NULL;
            z->x=wartosc;
            struct elista * tmp;
            tmp=q->head;
            if(q->head==NULL)
            {
                z->next=NULL;
                z->prev=NULL;
                q->head=z;
                q->tail=z;
            }
            else
            {
                while(tmp->next!=NULL && tmp->x!=poz)
                {
                    tmp=tmp->next;
                }
                if(tmp->next!=NULL)
                {
                    z->next=tmp->next;
                    tmp->next->prev=z;
                    tmp->next=z;
                    z->prev=tmp;
                }
                else
                {
                    printf("\nNie ma takiego elementu!\n");
                    system("PAUSE");
                }
            }
        }
        if(wybor==0)
        {
            printf("Nastapi przejscie do funkcji gownej...\n");
            system("PAUSE");
        }
        if(wybor!=1 && wybor!=2 && wybor!=0)
        {
            printf("\nNie ma opcji o takim numerze!\n");
            system("PAUSE");
        }

    }


    return q;
};

struct lista* usun(struct lista* q)
{
    int wybor=1, wartosc, i;
    while(wybor!=0)
    {
        system("cls");
        printf("1. Usun element z podanej pozycji.\n2. Usun element o podanej wartosci.\n0. Powrot.\nWybor: ");
        scanf("%d",&wybor);
        if(wybor==1)
        {
            struct elista* tmp;
            tmp=q->head;
            system("cls");
            printf("Podaj pozycje z ktorej usunac element: ");
            scanf("%d",&wartosc);

            if(q->head==NULL)
            {
                printf("Lista jest pusta!\n");
                system("PAUSE");
            }
            else
            {
                if(wartosc==1)
                {
                    tmp=tmp->next;
                    tmp->prev=NULL;
                    q->head=tmp;
                }
                else
                {
                    while(tmp->next!=NULL && i<wartosc)
                    {
                        tmp=tmp->next;
                        i++;
                    }
                    if (tmp->next!=NULL)
                    {
                        tmp->prev->next=tmp->next;
                        tmp->next->prev=tmp->prev;
                    }
                    else
                    {
                        if((i-wartosc)==0)
                        {
                            tmp=tmp->prev;
                            tmp->next=NULL;
                            q->tail=tmp;
                        }
                        else
                        {
                            printf("Nie ma takiej pozycji na liscie!\n");
                            system("PAUSE");
                        }
                    }
                }
            }
        }
        if(wybor==2)
        {
            struct elista* tmp;
            tmp=q->head;
            system("cls");
            printf("Podaj wartosc elementu do usuniecia: ");
            scanf("%d",&wartosc);
            while(tmp->x!=wartosc && tmp->next!=NULL)
            {
                tmp=tmp->next;
            }
            if(tmp==q->head)
            {
                tmp=tmp->next;
                tmp->prev=NULL;
                q->head=tmp;
            }
            else
            {
                if(tmp->next!=NULL)
                {
                    tmp->prev->next=tmp->next;
                    tmp->next->prev=tmp->prev;
                }
                else
                {
                    if(tmp->x==wartosc)
                    {
                        tmp=tmp->prev;
                        tmp->next=NULL;
                        q->tail=tmp;
                    }
                    else
                    {
                        printf("Nie ma takiego elementu!\n");
                        system("PAUSE");
                    }
                }
            }
        }
        if(wybor==0)
        {
            printf("Nastapi przejscie do funkcji gownej...\n");
            system("PAUSE");
        }
        if(wybor!=1 && wybor!=2 && wybor!=0)
        {
            printf("\nNie ma opcji o takim numerze!\n");
            system("PAUSE");
        }
    }

    return q;
};

void szukaj(struct lista* q, int el)
{
    struct elista *tmp;
    tmp=q->head;
    int i=1;

    while(tmp->x!=el && tmp->next!=NULL)
    {
        i++;
        tmp=tmp->next;
    }

    if (tmp->next!=NULL)
    {
        printf("Szukany element znajduje sie na pozycji: %d.",i);
    }
    else if(tmp->x==el)
    {
        printf("Szukany element znajduje sie na pozycji: %d.",i);
    }
    else
    {
        printf("Na liscie nie ma pozycji o podanej wartosci!\n");
    }
    system("PAUSE");

}

void drukowanie(struct lista* q)
{
    int wybor=1;
    while(wybor!=0)
    {
        system("cls");
        printf("1. Drukuj od poczatku.\n2. Drukuj od konca.\n0. Powrot.\nWybor: ");
        scanf("%d",&wybor);
        if(wybor==1)
        {
            struct elista * tmp=q->head;
            while(tmp!=NULL)
            {
                printf("%d, ", tmp->x);
                tmp=tmp->next;
            }
            system("PAUSE");
        }
        if(wybor==2)
        {

            struct elista * tmp=q->tail;
            while(tmp!=NULL)
            {
                printf("%d, ", tmp->x);
                tmp=tmp->prev;
            }
            system("PAUSE");
        }

        if(wybor==0)
        {
            printf("Nastapi przejscie do funkcji gownej...\n");
            system("PAUSE");
        }
        if(wybor!=1 && wybor!=2 && wybor!=0)
        {
            printf("\nNie ma opcji o takim numerze!\n");
            system("PAUSE");
        }
    }
}

0

Tych kilka lekcji powinno rozjaśnić ci co to są pointery.
Gynvael Coldwind - pointery

1 użytkowników online, w tym zalogowanych: 0, gości: 1