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; }
Kategorie:Listy
Tagi: C++, fifo, implementacja, informatyka, kod, kolejka, lista, programowanie, struktury danych
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; }
Kategorie:C++
Tagi: C++, implementacja, informatyka, kolejka, lista, programowanie, stos, struktury danych
Od autora:
Blog zawiera kody do programów napisanych w C, C++, oraz inne ciekawe informacje związane z informatyką
Statystyki:
- 120 140 unikalnych odwiedzin
Najpopularniejsze wpisy
Kategorie
- C (31)
- Algorytm Euklidesa (NWD) (2)
- BruteForce (1)
- Ch. Tw. (1)
- Drzewa (1)
- Funkcja (2)
- Grafy (2)
- Hello World (1)
- Listy (2)
- Max (3)
- Palindrom (1)
- Parzystość (1)
- Rówania modularne (1)
- Rekurencja (2)
- Sito Eratostenesa (1)
- Sortowanie (5)
- Stos (1)
- Struktury (1)
- Tablice Hashujace (1)
- Zapis do pliku (1)
- Znak zodiaku (1)
- C++ (63)
- Obiektowe (8)
- Definiowanie klas (4)
- Lista (1)
- Polimorfizm (1)
- Przeładowanie operatorow (1)
- Stos (1)
- Proceduralne (48)
- Ciągi (9)
- Czy liczba jest… (2)
- Czy rok jest przestępny (1)
- Dane z pliku (2)
- Dzień tygodnia (1)
- Elementy tablicy wieksze od wczytnej liczby (1)
- Fahrentheit & Celsjusz (1)
- Graficznie (2)
- Gry (1)
- Imie wspak (1)
- Inkrementacja i Dekrementacja (1)
- Kalkulator (1)
- Lotto (1)
- Matrix (1)
- Mnożenie macierzy MOD (1)
- Odwrotnosc modularna (1)
- Pola i obwody (2)
- Równania (3)
- Rekurencja (5)
- Schemat Hornera (2)
- Silnia (2)
- Sito Eratostenesa (1)
- Sortowanie (1)
- Suma liczb (1)
- Symbol newtona (1)
- Systemy liczbowe (3)
- Tablice (2)
- Wizytowka (1)
- Zadania z treścią (1)
- Złożoność obliczeniowa (1)
- Obiektowe (8)
- Muzyka (1)
- PoGGaduchy (2)
Komentarze