Advertisement
Motocelium

Operatii liste simplu inlantuite

Nov 12th, 2024 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.02 KB | None | 0 0
  1. // main.c
  2.  
  3. #include "functii.h"
  4.  
  5. /* functie creare a unui nod */
  6. struct NOD * creare_nod(){
  7.     struct NOD * nod;
  8.  
  9.     /* alocare memorie nod*/
  10.     nod = (struct NOD * ) malloc(sizeof(struct NOD));
  11.  
  12.     if (nod == NULL) {
  13.         printf("Eroare: memoria nu a putut fi alocata!\n");
  14.         return NULL;
  15.     }
  16.  
  17.     /* citire valori nod */
  18.     printf("\nIntroduceti cuvant:");
  19.     scanf("%s", nod -> cuvant);
  20.  
  21.     nod -> next = NULL;
  22.  
  23.     return nod;
  24. }
  25.  
  26. /* functie populare lista cu n cuvinte */
  27. struct NOD * populare_lista(struct NOD * head, int n){
  28.     int i;
  29.     for (i = 0; i < n; i++){
  30.         head = adaugare_nod_sfarsit_lista(head);
  31.     }
  32.     return head;
  33. }
  34.  
  35. /* functie afisare lista cuvinte */
  36. void afisare_lista(struct NOD * head){
  37.     int i = 0;
  38.     struct NOD * nod_curent;
  39.  
  40.     if (head == NULL) {
  41.         printf("Atentie, lista este goala!\n");
  42.         return;
  43.     }
  44.  
  45.     nod_curent = head;
  46.  
  47.     while (nod_curent != NULL) {
  48.         /* afisare valoare curenta si pozitionare nod urmator */
  49.         printf("%d: %s\n", i++, nod_curent -> cuvant);
  50.         nod_curent = nod_curent -> next;
  51.     }
  52. }
  53.  
  54. /* functie adaugare cuvant la sfarsitul listei */
  55. struct NOD * adaugare_nod_sfarsit_lista(struct NOD * head) {
  56.     int i = 0;
  57.     struct NOD * nod_curent, * nod_nou;
  58.  
  59.     if (head == NULL) {
  60.         printf("Atentie: lista este goala.");
  61.         head = creare_nod();
  62.         printf("Cuvantul a fost adaugat.\n");
  63.         return head;
  64.     }
  65.  
  66.     /*parcurge lista element cu element pentru a ajunge la ultimul nod*/
  67.     nod_curent = head;
  68.  
  69.     while (nod_curent != NULL) {
  70.         if (nod_curent -> next == NULL) {
  71.             /* creare si inserare nod nou in lista */
  72.             nod_nou = creare_nod();
  73.             nod_curent -> next = nod_nou;
  74.  
  75.             printf("Cuvantul a fost adaugat.\n");
  76.             return head;
  77.         }
  78.         nod_curent = nod_curent -> next;
  79.     }
  80. }
  81.  
  82. struct NOD * adaugare_nod_inceput_lista(struct NOD *head){
  83.     struct NOD *nod_nou = creare_nod();
  84.     nod_nou->next = head;
  85.     head = nod_nou;
  86.     return head;
  87. }
  88.  
  89. struct NOD * adaugare_cuvant_la_pozitia_n(struct NOD *head){
  90.     int n, i = 0;
  91.     printf("Introdu pozitia la care doresti sa fie introdus cuvantul:");
  92.     scanf("%d", &n);
  93.    
  94.     if(n == 0)
  95.         return adaugare_nod_inceput_lista(head);
  96.    
  97.     struct NOD *nod_nou = creare_nod(), *nod_curent = head;
  98.     while(i<n-1  && nod_curent->next!=NULL)
  99.         nod_curent = nod_curent->next, i++;
  100.        
  101.    
  102.     if(i<n-1)
  103.         printf("Pozitia specificata este mai mare decat numarul de elemente din lista, NU se poate adauga elementul\n");
  104.     else{
  105.         nod_nou->next = nod_curent->next;
  106.         nod_curent->next = nod_nou;
  107.     }
  108.     return head;
  109. }
  110.  
  111. struct NOD *stergere_primul_element(struct NOD *head){
  112.     struct NOD *aux = head;
  113.     head = head->next;
  114.     free(aux);
  115.     printf("Am sters cu succes elementul\n");
  116.     return head;
  117. }
  118.  
  119. struct NOD *stergere_ultimul_element(struct NOD *head){
  120.     struct NOD *aux = head;
  121.     if(aux == NULL){
  122.         printf("Lista este goala, nu putem sterge niciun element\n");
  123.         return head;
  124.     }
  125.     if(aux->next == NULL){
  126.         printf("Am sters elementul, acum lista este vida");
  127.         free(aux);
  128.         head = NULL;
  129.         return head;
  130.     }
  131.     while(aux->next->next)
  132.         aux = aux->next;
  133.        
  134.     free(aux->next);
  135.     aux->next = NULL;
  136.     printf("Am sters cu succes elementul\n");
  137.     return head;
  138. }
  139.  
  140. struct NOD * stergere_cuvant_pozitia_n(struct NOD *head){
  141.     int poz=0, n;
  142.     struct NOD *nod_curent = head, *nod;
  143.    
  144.     printf("\nIntrodu pozitia elementului pe care vrei sa il stergi: ");
  145.     scanf("%d", &n);
  146.    
  147.     while(poz < n-1 && nod_curent -> next) nod_curent = nod_curent ->next, poz++;
  148.    
  149.     nod = nod_curent -> next;
  150.     nod_curent -> next = nod -> next;
  151.     free(nod);
  152.     return head;
  153. }
  154.  
  155. struct NOD * stergere_lista(struct NOD *head){
  156.     struct NOD * next_node = head;
  157.    
  158.     while(head){
  159.         next_node = head->next;
  160.         free(head);
  161.         head = next_node;
  162.     }
  163.    
  164.     printf("\nLista a fost stearsa cu succes!\nATENTIE, LISTA ESTE GOALA");
  165.     return head;
  166. }
  167.  
  168. //functii.h
  169.  
  170. #ifndef FUNCTII_H
  171. #define FUNCTII_H
  172.  
  173. #include <stdlib.h>
  174. #include <stdio.h>
  175.  
  176. struct NOD {
  177.     char cuvant[20];
  178.     struct NOD * next;
  179. };
  180.  
  181. struct NOD * creare_nod();
  182. struct NOD * adaugare_nod_sfarsit_lista(struct NOD *);
  183. struct NOD * adaugare_nod_inceput_lista(struct NOD *);
  184. struct NOD * populare_lista(struct NOD *, int);
  185. void afisare_lista(struct NOD *);
  186. struct NOD * adaugare_cuvant_la_pozitia_n(struct NOD *);
  187. struct NOD * stergere_primul_element(struct NOD *);
  188. struct NOD * stergere_ultimul_element(struct NOD *);
  189. struct NOD * stergere_cuvant_pozitia_n(struct NOD *);
  190. struct NOD * stergere_lista(struct NOD*);
  191. #endif
  192.  
  193. //functii.c
  194.  
  195. #include "functii.h"
  196.  
  197. /* functie creare a unui nod */
  198. struct NOD * creare_nod(){
  199.     struct NOD * nod;
  200.  
  201.     /* alocare memorie nod*/
  202.     nod = (struct NOD * ) malloc(sizeof(struct NOD));
  203.  
  204.     if (nod == NULL) {
  205.         printf("Eroare: memoria nu a putut fi alocata!\n");
  206.         return NULL;
  207.     }
  208.  
  209.     /* citire valori nod */
  210.     printf("\nIntroduceti cuvant:");
  211.     scanf("%s", nod -> cuvant);
  212.  
  213.     nod -> next = NULL;
  214.  
  215.     return nod;
  216. }
  217.  
  218. /* functie populare lista cu n cuvinte */
  219. struct NOD * populare_lista(struct NOD * head, int n){
  220.     int i;
  221.     for (i = 0; i < n; i++){
  222.         head = adaugare_nod_sfarsit_lista(head);
  223.     }
  224.     return head;
  225. }
  226.  
  227. /* functie afisare lista cuvinte */
  228. void afisare_lista(struct NOD * head){
  229.     int i = 0;
  230.     struct NOD * nod_curent;
  231.  
  232.     if (head == NULL) {
  233.         printf("Atentie, lista este goala!\n");
  234.         return;
  235.     }
  236.  
  237.     nod_curent = head;
  238.  
  239.     while (nod_curent != NULL) {
  240.         /* afisare valoare curenta si pozitionare nod urmator */
  241.         printf("%d: %s\n", i++, nod_curent -> cuvant);
  242.         nod_curent = nod_curent -> next;
  243.     }
  244. }
  245.  
  246. /* functie adaugare cuvant la sfarsitul listei */
  247. struct NOD * adaugare_nod_sfarsit_lista(struct NOD * head) {
  248.     int i = 0;
  249.     struct NOD * nod_curent, * nod_nou;
  250.  
  251.     if (head == NULL) {
  252.         printf("Atentie: lista este goala.");
  253.         head = creare_nod();
  254.         printf("Cuvantul a fost adaugat.\n");
  255.         return head;
  256.     }
  257.  
  258.     /*parcurge lista element cu element pentru a ajunge la ultimul nod*/
  259.     nod_curent = head;
  260.  
  261.     while (nod_curent != NULL) {
  262.         if (nod_curent -> next == NULL) {
  263.             /* creare si inserare nod nou in lista */
  264.             nod_nou = creare_nod();
  265.             nod_curent -> next = nod_nou;
  266.  
  267.             printf("Cuvantul a fost adaugat.\n");
  268.             return head;
  269.         }
  270.         nod_curent = nod_curent -> next;
  271.     }
  272. }
  273.  
  274. struct NOD * adaugare_nod_inceput_lista(struct NOD *head){
  275.     struct NOD *nod_nou = creare_nod();
  276.     nod_nou->next = head;
  277.     head = nod_nou;
  278.     return head;
  279. }
  280.  
  281. struct NOD * adaugare_cuvant_la_pozitia_n(struct NOD *head){
  282.     int n, i = 0;
  283.     printf("Introdu pozitia la care doresti sa fie introdus cuvantul:");
  284.     scanf("%d", &n);
  285.    
  286.     if(n == 0)
  287.         return adaugare_nod_inceput_lista(head);
  288.    
  289.     struct NOD *nod_nou = creare_nod(), *nod_curent = head;
  290.     while(i<n-1  && nod_curent->next!=NULL)
  291.         nod_curent = nod_curent->next, i++;
  292.        
  293.    
  294.     if(i<n-1)
  295.         printf("Pozitia specificata este mai mare decat numarul de elemente din lista, NU se poate adauga elementul\n");
  296.     else{
  297.         nod_nou->next = nod_curent->next;
  298.         nod_curent->next = nod_nou;
  299.     }
  300.     return head;
  301. }
  302.  
  303. struct NOD *stergere_primul_element(struct NOD *head){
  304.     struct NOD *aux = head;
  305.     head = head->next;
  306.     free(aux);
  307.     printf("Am sters cu succes elementul\n");
  308.     return head;
  309. }
  310.  
  311. struct NOD *stergere_ultimul_element(struct NOD *head){
  312.     struct NOD *aux = head;
  313.     if(aux == NULL){
  314.         printf("Lista este goala, nu putem sterge niciun element\n");
  315.         return head;
  316.     }
  317.     if(aux->next == NULL){
  318.         printf("Am sters elementul, acum lista este vida");
  319.         free(aux);
  320.         head = NULL;
  321.         return head;
  322.     }
  323.     while(aux->next->next)
  324.         aux = aux->next;
  325.        
  326.     free(aux->next);
  327.     aux->next = NULL;
  328.     printf("Am sters cu succes elementul\n");
  329.     return head;
  330. }
  331.  
  332. struct NOD * stergere_cuvant_pozitia_n(struct NOD *head){
  333.     int poz=0, n;
  334.     struct NOD *nod_curent = head, *nod;
  335.    
  336.     printf("\nIntrodu pozitia elementului pe care vrei sa il stergi: ");
  337.     scanf("%d", &n);
  338.    
  339.     while(poz < n-1 && nod_curent -> next) nod_curent = nod_curent ->next, poz++;
  340.    
  341.     nod = nod_curent -> next;
  342.     nod_curent -> next = nod -> next;
  343.     free(nod);
  344.     return head;
  345. }
  346.  
  347. struct NOD * stergere_lista(struct NOD *head){
  348.     struct NOD * next_node = head;
  349.    
  350.     while(head){
  351.         next_node = head->next;
  352.         free(head);
  353.         head = next_node;
  354.     }
  355.    
  356.     printf("\nLista a fost stearsa cu succes!\nATENTIE, LISTA ESTE GOALA");
  357.     return head;
  358. }
Tags: sda liste
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement