Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int val;
- struct node *next;
- } NodeT;
- int hFunction(int key) //H=dispersie in engleza
- {
- return key%M;
- }
- void insert(NodeT* hTable[M], int key)
- {
- int numarCasuta=hFunction(key);
- NodeT* nod=(NodeT*)malloc(sizeof(NodeT));
- nod->val=key;
- nod->next=hTable[numarCasuta];//vechiul first anterior
- //legam noul nod de vechiul prim element anterior
- hTable[numarCasuta]=nod; //36 primul element inserat, anterior
- }
- NodeT* search(NodeT* hTable[M], int key)
- {
- int numarCasuta=hFunction(key);
- NodeT* p=hTable[numarCasuta]; //parcurgem lista de la acea casuta
- while(p!=NULL)
- if(p->val==key)
- {
- //returnam elementul din lista
- return p;
- }
- p=p->next;
- return NULL;
- }
- void deleteKey(NodeT* hTable[M], int key)
- {
- }
- //afisarea tuturor elmentelor din tebela de dispersie
- void showAll(NodeT* hTable[M])
- {
- int i;
- for(i = 0; i < M; i++)
- {
- printf(" %d :",i);
- //verificam daca la slotul i am adaugat ceva
- if(hTable[i] != NULL) //{
- {
- NodeT *p;
- p = hTable[i];
- while (p != NULL) //afisarea listei!
- {
- printf(" %d ",p->val);
- p = p->next;
- }
- } //}
- printf("\n");
- }
- printf("\n");
- }
- int main()
- {
- //initializare
- NodeT* hTable[M];
- for(int i = 0; i < M; i++)
- {
- hTable[i] = NULL;
- }
- //test inserare
- int vals[] = {36, 18, 6, 43, 72, 10, 5, 15};
- for(int i=0; i<sizeof(vals)/sizeof(int); i++)
- insert(hTable, vals[i]);
- showAll(hTable);
- // 0:
- // 1: 15 43 36
- // 2: 72
- // 3: 10
- // 4: 18
- // 5: 5
- // 6: 6
- //test search
- //test search
- NodeT* cautare43 = search(hTable, 43);
- if (cautare43 != NULL) {
- printf("%d a fost gasit!\n", cautare43->val);
- }
- NodeT* cautare7 = search(hTable, 7);
- if (cautare7 != NULL) {
- printf("%d a fost gasit!\n", cautare7->val);
- } else {
- printf("7 nu a fost gasit\n");
- }
- //test stergere
- int todel[] = {43, 36, 10, 61, -5};
- for(int i=0; i<sizeof(todel)/sizeof(int); i++)
- deleteKey(hTable, todel[i]);
- showAll(hTable);
- // 0:
- // 1: 15
- // 2: 72
- // 3:
- // 4: 18
- // 5: 5
- // 6: 6
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement