prob ch

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

struct Elev {
char *nume;
char *cnp;
int nota;
};

struct Nod {
struct Nod * prev;
struct Nod * next;
char * nume/struct elev test;
};


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(char * nume_dat/struct elev persoana) {
struct Nod * newNod;
newNod = (struct Nod*)malloc(1*sizeof(struct Nod));
newNod -> prev = NULL;
newNod -> next = NULL;

newNod -> nume =(char*)malloc(strlen(nume_dat)*sizeof(char));
strcpy(newNod -> nume,nume_dat);

newNod -> test = persoana;

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,char *nume/struct elev persoana) {
struct Nod *nod1;
nod1 = createNod(nume/persoana);
if(listIsEmpty(list) == 1) {
list -> first = nod1;
list -> last = nod1;
}else{
if(listIsEmpty(list) == 0) {


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


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



}

}


struct Nod * cautaNod(struct Lista * list, char * nume_dat) {
if(listIsEmpty(list) == 1) return NULL;
else {
struct Nod * curent;
curent = list -> first;
while(curent != NULL) {
if(strcmp(curent-> nume ,nume_dat)==0) {
printf("Nume gasit!");
return curent;
}
curent = curent -> next;
}
}
return NULL;
}
void dezalocNod (struct Nod * nod){
free(nod -> nume);
free(nod);
}
void dezalocLista (struct Lista * list) {
struct Nod * curent;
curent = list->first;
while (curent -> next != NULL){
curent = curent -> next;
dezalocNod(curent -> prev);
}
dezalocNod(curent -> prev);
dezalocNod(curent);
free(list);
}

struct Lista * deleteNod(struct Lista * list ,char * nume) {
struct Nod * curent;
curent = cautaNod( list, nume);
printf("numele gasit:%s\n",curent->nume);

if(curent == list -> first && curent == list -> last){
list -> first=NULL;
list -> last=NULL;
dezalocNod(curent);
return list;
}

if(curent -> prev == NULL){
printf("Este primul nod\n");
list -> first = curent -> next;
curent -> next -> prev = curent -> prev;
dezalocNod(curent);
list->size--;
return list;
}

if(curent -> next == NULL){
printf("E ultimul nod\n");
list -> last = curent ->prev;
curent -> prev -> next = curent -> next;
dezalocNod(curent);
list -> size--;
return list;
}

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

}

int main() {

struct Lista * list = createList();
struct Elev exemplu;
char nume[20],cnp[13];
int i,nota;

for(i=0;i<3;i++){
printf("numele este:");
scanf("%s", nume);
if(nume[0]=='.')
break;
printf("cnp este:");
scanf("%s", cnp);
printf("nota este:");
scanf("%d", nota);

exemplu.nume=malloc(strlen(nume)*sizeof(char));
exemplu.cnp=malloc(strlen(cnp)*sizeof(char));

strcpy(exemplu.nume,nume);
strcpy(exemplu.cnp,cnp);
exemplu.nota=nota

list=insert(list,exemplu);
}

struct Nod * curent=list->first;

while(curent!=NULL){
printf("%s\n",curent->nume/curent ->test .nume);
curent=curent->next;
}


//struct Lista * deleteNod(struct Lista * list ,char * nume) {
list=deleteNod(list,"vasile");
curent=list->first;
printf("\n");

while(curent!=NULL){
printf("%s\n",curent->nume/curent ->test .nume);
curent=curent->next;
}

dezalocLista (list);
return 0;
}


Last edited on
Topic archived. No new replies allowed.