Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LISTE DUBLU INLANTUITE
- typedef struct node
- {
- int key;
- struct node *next;
- struct node *prev;
- } NodeDL;
- typedef struct
- {
- NodeDL *first;
- NodeDL *last;
- }list_header;
- 1) Afisare de la inceput
- void print_forward(list_header L)
- {
- NodeDL *p;
- p=L.first;
- while(p)
- {
- printf("%d ",p->key);
- p=p->next;
- }
- printf("\n");
- }
- 2) Afisare de la sfarsit
- void print_backward(list_header L)
- {
- NodeDL *p;
- p=L.last;
- while(p)
- {
- printf("%d ",p->key);
- p=p->prev;
- }
- printf("\n");
- }
- 3) Cautarea unui nod
- NodeDL *search(list_header L, int givenKey)
- {
- NodeDL *p;
- p=L.first;
- while(p)
- {
- if(p->key==givenKey)
- return p;
- p=p->next;
- }
- return NULL;
- }
- 4) Inserarea in fata primului nod
- void insert_first(list_header *L, int givenKey)
- {
- NodeDL *p;
- p=(NodeDL*)malloc(sizeof(NodeDL));
- p->key=givenKey;
- if(L->first==NULL)
- {
- p->next=NULL;
- p->prev=NULL;
- L->first=p;
- L->last=p;
- }
- else
- {
- p->next=L->first;
- p->prev=NULL;
- L->first->prev=p;
- L->first=p;
- }
- }
- 5) Inserarea dupa ultimul nod
- void insert_last(list_header *L, int givenKey)
- {
- NodeDL *p;
- p=(NodeDL*)malloc(sizeof(NodeDL));
- p->key=givenKey;
- if(L->first==NULL)
- {
- p->next=NULL;
- p->prev=NULL;
- L->first=p;
- L->last=p;
- }
- else
- {
- L->last->next=p;
- p->next=NULL;
- p->prev=L->last;
- L->last=p;
- }
- }
- 6) Inserarea dupa un key
- void insert_after_key(list_header *L, int afterKey, int givenKey)
- {
- if(L->first==NULL)
- {
- printf("The list is NULL");
- }
- else
- {
- int gasit=0;
- NodeDL *p,*q;
- p=L->first;
- while(p)
- {
- if(p->key==afterKey && gasit==0)
- {
- gasit=1;
- q=p;
- }
- p=p->next;
- }
- if(gasit==0)
- {
- printf("The list don't have this key");
- }
- else
- {
- NodeDL *f;
- f=(NodeDL *)malloc(sizeof(NodeDL));
- f->key=givenKey;
- if(q->next==NULL)
- {
- q->next=f;
- f->prev=q;
- f->next=NULL;
- L->last=f;
- }
- else
- {
- f->next=q->next;
- q->next->prev=f;
- q->next=f;
- f->prev=q;
- }
- }
- }
- }
- 7) Stergerea primului nod
- void delete_first(list_header *L)
- {
- if(L->first==NULL)
- {
- printf("The list is NULL");
- }
- else
- {
- if(L->first->next==NULL)
- {
- L->first=NULL;
- L->last=NULL;
- free(L->first);
- }
- else
- {
- NodeDL *p;
- p=L->first->next;
- L->first->next=NULL;
- L->first=NULL;
- free(L->first);
- L->first=p;
- L->first->prev=NULL;
- }
- }
- }
- 8) Stergerea ultimului nod
- void delete_last(list_header *L)
- {
- if(L->first==NULL)
- {
- printf("The list is NULL");
- }
- else
- {
- if(L->first->next==NULL)
- {
- L->first=NULL;
- L->last=NULL;
- free(L->first);
- }
- else
- {
- NodeDL *q=L->last;
- NodeDL *p=q->prev;
- q=NULL;
- free(q);
- p->next=NULL;
- L->last=p;
- }
- }
- }
- 10) Stergerea unei key date
- void delete_key(list_header *L, int givenKey)
- {
- if(L->first->key==givenKey)
- {
- delete_first(L);
- }
- else
- {
- int gasit=0;
- NodeDL *p,*q;
- p=L->first;
- while(p->next)
- {
- if(p->next->key==givenKey && gasit==0)
- {
- q=p;
- gasit=1;
- }
- p=p->next;
- }
- if(gasit==0)
- {
- printf("The list don't have this key");
- }
- else
- {
- if(q->next==L->last)
- {
- delete_last(L);
- }
- else
- {
- NodeDL *f;
- f=q->next;
- q->next->next->prev=q;
- q->next=q->next->next;
- f->next=NULL;
- f->prev=NULL;
- f=NULL;
- free(f);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment