Archiwum

Posts Tagged ‘struktury danych’

Kolejka FIFO (First in first out)

29 czerwca 2011 6 Komentarzy
//Kolejka FIFO (implementacja lista)
#include <stdio.h>
#include <stdlib.h>

typedef struct FIFO{
        int key;
        struct FIFO *next;
        struct FIFO *prev;
}FIFO;

void push(FIFO **head, FIFO **tail, int el)
{
     FIFO *nowy;
     nowy= (FIFO *)malloc(sizeof(FIFO));
     nowy->key=el;
     nowy->next=NULL;
     nowy->prev=NULL;

     if(*head==NULL){
         *head=nowy;
         *tail=nowy;
         }
     else{
         nowy->prev=(*tail);
         (*tail)->next=nowy;
         (*tail)=nowy;
         }
}
void pop(FIFO **head)
{
     FIFO *tmp=(*head);
     if((*head)==NULL){
        printf("Brak el...");
        }
     else if((*head)->next==NULL){
     printf("\nUsunieto %d\n", tmp->key);
     *head=NULL;}
     else{

        printf("Usunieto %d\n", tmp->key);
        (*head)=(*head)->next;
        (*head)->prev=NULL;
        free(tmp);
        }
}

void show(FIFO *head){
     if(head==NULL)printf("Brak el...");
     else while(head!=NULL){
     printf("%d   ", head->key);
     head=head->next;
     }
     }

int main()
{
    FIFO *head=NULL;
    FIFO *tail=NULL;

    push(&head,&tail,1);
    push(&head,&tail,4)
    pop(&head);
    show(head);

    system("pause");
    return 0;
}

Lista w C (struktury danych)

26 czerwca 2011 45 Komentarzy
//dodawanie, wyswietlanie, szukanie, usuwanie elementow
#include <stdio.h>
typedef struct L
{
        int key;
        struct L *next;
}L;

void show (L *head)
{
     if(head != NULL){
     while (head != NULL){
           printf("%d ", head->key);
           head=head->next;
           }
           printf("\n");
     }
     else printf("No element to show...\n");
}

void search (L *head, int Find)
{
     int i=1;
     L *tmp=head;
     while (tmp!= NULL && tmp->key!=Find){
           tmp=tmp->next;
           i++;
           }
           if (tmp!=NULL){
           printf("Element %d is on %d place ", tmp->key, i);
           printf("\n");}
     else printf("Element does not exist\n");
}

L *add (L *head, int New)
{
       L *tmp, *nowy, *cien;
       int tmp2;
       nowy = (L *)malloc(sizeof(L));
       nowy->key = New;
       nowy->next=NULL;

       if (head==NULL){
          head=nowy;
          }
          else if (head->key >= New){
               nowy->next=head;
               head=nowy;
               }
       else{
            tmp=head;
            while(tmp!=NULL && tmp->key<New){
            cien=tmp;
            tmp=tmp->next;
            }           
            cien->next=nowy;
            nowy->next=tmp;
            }
       return head;
}    

L *Delete (L *head, int Del)
{

      L *tmp, *tmp2, *pom;
      tmp = head;
      if(head==NULL){
          printf("No element to delete...\n");
          return head;
            }
      else if(head->next == NULL && head->key == Del){
           free(head);
           head = NULL;
           return head;
            }
      else{
           if (tmp->key== Del){
                   pom=tmp->next;
                   free(head);
                   return pom;
                   //
                   }
           else{
               tmp2=tmp->next;
               while (tmp2->next!= NULL && tmp2->key != Del){
                     tmp=tmp->next;
                     tmp2=tmp->next;
                     }
               if (tmp2->next== NULL){
                     printf("Element does not exist...\n");
                     }
               else if(tmp2->next==NULL){  //jesli ostatni
                    free(tmp2);
                    tmp->next = NULL;
                    //
                     }
               else{
                    tmp->next = tmp2->next;
                    free(tmp2);
                    tmp2 = NULL;
                    }
               }

           }
         return head;
}

int main ()
{
    int option=0, New, Del, Find;
    L *my_list;
    my_list = ( L *)malloc(sizeof(L));
    my_list = NULL;

    while(option!=5)
    {

            printf("\n1.ADD\n2.Show\n3.Search\n4.Delete\n5.END\n");
            scanf("%d",&option);

            switch(option)
            {
               case 1: printf("Type value\n"); scanf("%d",&New);
               my_list=add(my_list,New); break;
               case 2: show(my_list); break;
               case 3: printf("Type value to find\n"); 
                       scanf("%d",&Find);
                       search(my_list,Find); break;
               case 4: printf("Enter the value to remove\n"); 
                       scanf("%d",&Del); 
                       my_list=Delete(my_list,Del); break;
               case 5: option = 5;break;
               default: break;
            }

    }
    return 0;
}