Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef struct lista {
- char *nume;
- int cod;
- struct lista *urm;
- }nod;
- nod *rad = NULL;
- nod *adaugare_la_coada(nod *prim, char *n, int cod) {
- nod *q;
- nod *nou = (nod*)malloc(sizeof(nod));
- nou->nume = (char*)malloc(strlen(n) + 1);
- nou->urm = NULL;
- strcpy(nou->nume, n);
- nou->cod = cod;
- q = prim;
- if (prim == NULL) {
- return nou;
- }
- else {
- while (q->urm != NULL) {
- q = q->urm;
- }
- q->urm = nou;
- }
- return prim;
- }
- nod * stergere(nod *prim,char *n){
- nod *q;
- nod *aux;
- q = prim;
- if (prim == NULL) {
- printf("Lista e goala\n");
- }
- else
- {
- if (strcmp(prim->nume, n) == 0) {
- aux = prim;
- prim = prim->urm;
- free(aux);
- return prim;
- }
- else {
- while (q->urm != NULL && (strcmp(q->urm->nume,n)!=0)) {
- q = q->urm;
- }
- if (q->urm == NULL) {
- printf("S-a ajuns la finalul listei\n");
- }
- else {
- aux = q->urm;
- q->urm = q->urm->urm;
- free(aux);
- }
- }
- }
- return prim;
- }
- nod *adaugare_la_inceput(nod *prim, char *n, int cod) {
- nod *q;
- nod *nou = (nod*)malloc(sizeof(nod));
- nou->nume = (char*)malloc(strlen(n) + 1);
- nou->urm = NULL;
- strcpy(nou->nume, n);
- nou->cod = cod;
- q = prim;
- if (prim == NULL) {
- return nou;
- }
- else {
- nou->urm = prim;
- return nou;
- }
- }
- nod *adaugare_nume_dat(nod *prim, char *n, int cod) {
- nod *q=prim;
- char sir[30];
- printf("Ce cauti: "); scanf("%s", sir);
- nod *nou = (nod*)malloc(sizeof(nod));
- nou->nume = (char*)malloc(strlen(n) + 1);
- strcpy(nou->nume, n);
- nou->cod = cod;
- if (prim == NULL) printf("Lista nula");
- else {
- while (q != NULL && strcmp(q->nume, sir) != 0) {
- q = q->urm;
- }
- if (q != NULL) {
- nou->urm = q->urm;
- q->urm = nou;
- return prim;
- }
- }
- }
- void afisare(nod *prim) {
- nod *q;
- q = prim;
- while (q != NULL) {
- printf(" %s %d \n", q->nume, q->cod);
- q = q->urm;
- }
- }
- nod * adaugare(nod *prim, char *n, int cod) {
- nod *q = prim;
- nod *nou = (nod*)malloc(sizeof(nod));
- nou->nume = (char*)malloc(strlen(n)+1);
- strcpy(nou->nume, n);
- nou->cod = cod;
- if (prim == NULL) {
- prim = nou;
- nou->urm = NULL;
- return prim;
- }
- else {
- if (strcmp(nou->nume, prim->nume) < 0) {
- nou->urm = prim;
- return nou;
- }
- else {
- while (q->urm != NULL && strcmp(nou->nume, q->urm->nume) > 0) {
- q = q->urm;
- }
- nou->urm = q->urm;
- q->urm = nou;
- }
- }
- return prim;
- }
- void main() {
- int opt;
- char nume[20];
- int cod;
- do {
- printf("\n\n1.Citire\n2.Afisare\n3.Add nume dat\nAlege:");
- scanf("%d", &opt);
- switch (opt) {
- case 1:
- printf("Da nume:"); scanf("%s", nume);
- printf("Da cod:"); scanf("%d", &cod);
- rad = adaugare(rad, nume, cod);
- break;
- case 2:
- afisare(rad);
- break;
- case 3:
- printf("Da nume:"); scanf("%s", nume);
- printf("Da cod:"); scanf("%d", &cod);
- rad = adaugare_nume_dat(rad, nume, cod);
- break;
- case 4:
- printf("Da nume:"); scanf("%s", nume);
- rad = stergere(rad, nume);
- break;
- }
- } while (opt != 0);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement