ceva i

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct Nod {
struct Nod * prev;
struct Nod * next;
int val;
};


struct Lista {
struct Nod * first;
struct Nod * last;
int size;
};

struct Lista * createList() {
struct Lista * list;
list = (struct Lista*)malloc(1*sizeof(struct Lista));
list -> first = NULL;
list -> last = NULL;
list -> size = 0;
return list;
}

struct Nod * createNod(int valoare) {
struct Nod * newNod;
newNod = (struct Nod*)malloc(1*sizeof(struct Nod));
newNod -> prev = NULL;
newNod -> next = NULL;
newNod -> val = valoare;

return newNod;
}

int listIsEmpty(struct Lista * list) {
if(list -> first == NULL && list -> last == NULL) {

return 1;
}else{
return 0;
}
}

struct Lista * insert(struct Lista * list,int numar,int index) {
struct Nod *nod1;
nod1 = createNod(numar);
if(listIsEmpty(list) == 1) {
list -> first = nod1;
list -> last = nod1;
return list;
}else{
if(listIsEmpty(list) == 0) {


nod1 -> next = list -> first; //inserare la inceput
list -> first -> prev = nod1;
list -> first = nod1;
list -> size++;
return list;
}


/*nod1 -> prev = list -> last; //la sfarsit
list -> last -> next = nod1;
list -> last = nod1;
list -> size++;
return list;


int i=0; //cu index dat
struct Nod *curent;
curent = list -> first;
for(i == 0; i < index - 1; i++){
curent = curent -> next;
}
nod1 -> prev = curent;
nod1 -> next = curent -> next;
curent -> next -> prev = nod1;
curent -> next = nod1
list -> size++;
return list;
} */

}

}


struct Nod * cautaNod(struct Lista * list, int numar_dat) {
if(listIsEmpty(list) == 1) return NULL;
else {
struct Nod * curent;
curent = list -> first;
while(curent != NULL) {
if(curent-> val == numar_dat) {

return curent;
}
curent = curent -> next;
}
}
return;
}

struct Lista * deleteNod(struct Lista * list ,int numar//, int index
) {
struct Nod * curent;
curent = cautaNod( list, numar);
int i = 0;

/* while(curent -> next != NULL) {
if(i == index)
break;
curent = curent -> next;
i++;
}*/
/* if(i < index) {
printf( "index dat prea mare" );
return list;
}*/
if(curent == list -> first && curent == list -> last){
list -> first=NULL;
list -> last=NULL;
free(curent);
return list;
}

if(curent -> prev == NULL){ //pt primul nod

list -> first = curent -> next;
curent -> next -> prev = curent -> prev;
free(curent);
list->size--;
return list;
}

if(curent -> next == NULL){ //pt ultimul nod
;
curent -> prev -> next = curent -> next;
list -> last = curent ->prev;
free(curent);
list -> size--;
return list;
}

else{
curent -> prev -> next = curent -> next;
curent -> next -> prev = curent -> prev;
free(curent);
list -> size--;
return list;
}

}

void Dezaloc (struct Lista * list) {
struct Nod * curent;
curent = list->first;
while (curent -> next != NULL){
curent = curent -> next;
free(curent -> prev);
}
free(curent -> prev);
free(curent);
free(list);
}

int main() {
struct Lista * list = createList();
int a,i;
for(i=0;i<3;i++){
printf("numarul este:");
scanf("%d",&a);
list=insert(list,a,0);
}
struct Nod * curent;
curent=list->first;
while(curent!=NULL){
printf("%d\n",curent->val);
curent=curent->next;
}
list=deleteNod(list,2);

curent=list->first;
while(curent!=NULL){
printf("%d\n",curent->val);
curent=curent->next;
}
return 0;
}
Last edited on
Topic archived. No new replies allowed.