Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Programul creeaza o lista dublu inlantuita */
- #include <stdio.h>
- #include <conio.h>
- #include <malloc.h>
- #include <string.h>
- #include <stdlib.h>
- struct lista
- {int k;
- struct lista *next,*prev;
- };
- struct lista *cons_ldi(int k,struct lista *l)
- { struct lista *aux;
- aux=(struct lista *)malloc(sizeof(struct lista));
- aux->k=k;
- aux->next=l;
- aux->prev=NULL;
- if(l) l->prev=aux;
- return(aux);
- }
- struct lista *init_ldi(struct lista *l)
- {
- int ci,k;
- char c,ck,cc[5];
- l=NULL;
- printf("Doriti sa incepeti initializarea listei [n pentru terminare]?");
- scanf("%c",&c);
- if (c!='n')
- k=1;
- else
- k=-1;
- while (k!=-1)
- {
- printf(" o cheie : ");
- scanf("%s",cc);
- if (strcmp(cc,"n"))
- k=1;
- else
- k=-1;
- ci=atoi(cc);
- if(k!=-1)
- l=cons_ldi(ci,l);
- }
- return(l);
- }
- void tipar_ldi(struct lista *l)
- { struct lista *p;
- printf("NULL");
- for (p=l;p;p=p->next)printf("<=>%d",p->k);
- printf("<=>NULL\n");
- }
- struct lista *inserare(struct lista *l, int poz, int inf){
- struct lista *aux;
- struct lista *p;
- aux=(struct lista *)malloc(sizeof(struct lista));
- aux->k=inf;
- if(poz==1){
- aux->prev=NULL;
- aux->next=l;
- if(l!=NULL)
- l->prev=aux;
- l=aux;
- }
- else{
- p=l;
- for(int i=1;i<=poz-2&&p!=NULL;i++)
- p=p->next;
- aux->next=p->next;
- if(p->next!=NULL)
- p->next->prev=aux;
- p->next=aux;
- aux->prev=p;
- }
- return l;
- };
- int main()
- {
- struct lista *l;
- int k;
- l=init_ldi(l);
- printf(" \n \n LISTA INITIALA : ");
- tipar_ldi(l);
- l=inserare(l,3,3333);
- printf(" \n \n LISTA DUPA INSERTIE : ");
- tipar_ldi(l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement