Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.c
- #include "functii.h"
- /* functie creare a unui nod */
- struct NOD * creare_nod(){
- struct NOD * nod;
- /* alocare memorie nod*/
- nod = (struct NOD * ) malloc(sizeof(struct NOD));
- if (nod == NULL) {
- printf("Eroare: memoria nu a putut fi alocata!\n");
- return NULL;
- }
- /* citire valori nod */
- printf("\nIntroduceti cuvant:");
- scanf("%s", nod -> cuvant);
- nod -> next = NULL;
- return nod;
- }
- /* functie populare lista cu n cuvinte */
- struct NOD * populare_lista(struct NOD * head, int n){
- int i;
- for (i = 0; i < n; i++){
- head = adaugare_nod_sfarsit_lista(head);
- }
- return head;
- }
- /* functie afisare lista cuvinte */
- void afisare_lista(struct NOD * head){
- int i = 0;
- struct NOD * nod_curent;
- if (head == NULL) {
- printf("Atentie, lista este goala!\n");
- return;
- }
- nod_curent = head;
- while (nod_curent != NULL) {
- /* afisare valoare curenta si pozitionare nod urmator */
- printf("%d: %s\n", i++, nod_curent -> cuvant);
- nod_curent = nod_curent -> next;
- }
- }
- /* functie adaugare cuvant la sfarsitul listei */
- struct NOD * adaugare_nod_sfarsit_lista(struct NOD * head) {
- int i = 0;
- struct NOD * nod_curent, * nod_nou;
- if (head == NULL) {
- printf("Atentie: lista este goala.");
- head = creare_nod();
- printf("Cuvantul a fost adaugat.\n");
- return head;
- }
- /*parcurge lista element cu element pentru a ajunge la ultimul nod*/
- nod_curent = head;
- while (nod_curent != NULL) {
- if (nod_curent -> next == NULL) {
- /* creare si inserare nod nou in lista */
- nod_nou = creare_nod();
- nod_curent -> next = nod_nou;
- printf("Cuvantul a fost adaugat.\n");
- return head;
- }
- nod_curent = nod_curent -> next;
- }
- }
- struct NOD * adaugare_nod_inceput_lista(struct NOD *head){
- struct NOD *nod_nou = creare_nod();
- nod_nou->next = head;
- head = nod_nou;
- return head;
- }
- struct NOD * adaugare_cuvant_la_pozitia_n(struct NOD *head){
- int n, i = 0;
- printf("Introdu pozitia la care doresti sa fie introdus cuvantul:");
- scanf("%d", &n);
- if(n == 0)
- return adaugare_nod_inceput_lista(head);
- struct NOD *nod_nou = creare_nod(), *nod_curent = head;
- while(i<n-1 && nod_curent->next!=NULL)
- nod_curent = nod_curent->next, i++;
- if(i<n-1)
- printf("Pozitia specificata este mai mare decat numarul de elemente din lista, NU se poate adauga elementul\n");
- else{
- nod_nou->next = nod_curent->next;
- nod_curent->next = nod_nou;
- }
- return head;
- }
- struct NOD *stergere_primul_element(struct NOD *head){
- struct NOD *aux = head;
- head = head->next;
- free(aux);
- printf("Am sters cu succes elementul\n");
- return head;
- }
- struct NOD *stergere_ultimul_element(struct NOD *head){
- struct NOD *aux = head;
- if(aux == NULL){
- printf("Lista este goala, nu putem sterge niciun element\n");
- return head;
- }
- if(aux->next == NULL){
- printf("Am sters elementul, acum lista este vida");
- free(aux);
- head = NULL;
- return head;
- }
- while(aux->next->next)
- aux = aux->next;
- free(aux->next);
- aux->next = NULL;
- printf("Am sters cu succes elementul\n");
- return head;
- }
- struct NOD * stergere_cuvant_pozitia_n(struct NOD *head){
- int poz=0, n;
- struct NOD *nod_curent = head, *nod;
- printf("\nIntrodu pozitia elementului pe care vrei sa il stergi: ");
- scanf("%d", &n);
- while(poz < n-1 && nod_curent -> next) nod_curent = nod_curent ->next, poz++;
- nod = nod_curent -> next;
- nod_curent -> next = nod -> next;
- free(nod);
- return head;
- }
- struct NOD * stergere_lista(struct NOD *head){
- struct NOD * next_node = head;
- while(head){
- next_node = head->next;
- free(head);
- head = next_node;
- }
- printf("\nLista a fost stearsa cu succes!\nATENTIE, LISTA ESTE GOALA");
- return head;
- }
- //functii.h
- #ifndef FUNCTII_H
- #define FUNCTII_H
- #include <stdlib.h>
- #include <stdio.h>
- struct NOD {
- char cuvant[20];
- struct NOD * next;
- };
- struct NOD * creare_nod();
- struct NOD * adaugare_nod_sfarsit_lista(struct NOD *);
- struct NOD * adaugare_nod_inceput_lista(struct NOD *);
- struct NOD * populare_lista(struct NOD *, int);
- void afisare_lista(struct NOD *);
- struct NOD * adaugare_cuvant_la_pozitia_n(struct NOD *);
- struct NOD * stergere_primul_element(struct NOD *);
- struct NOD * stergere_ultimul_element(struct NOD *);
- struct NOD * stergere_cuvant_pozitia_n(struct NOD *);
- struct NOD * stergere_lista(struct NOD*);
- #endif
- //functii.c
- #include "functii.h"
- /* functie creare a unui nod */
- struct NOD * creare_nod(){
- struct NOD * nod;
- /* alocare memorie nod*/
- nod = (struct NOD * ) malloc(sizeof(struct NOD));
- if (nod == NULL) {
- printf("Eroare: memoria nu a putut fi alocata!\n");
- return NULL;
- }
- /* citire valori nod */
- printf("\nIntroduceti cuvant:");
- scanf("%s", nod -> cuvant);
- nod -> next = NULL;
- return nod;
- }
- /* functie populare lista cu n cuvinte */
- struct NOD * populare_lista(struct NOD * head, int n){
- int i;
- for (i = 0; i < n; i++){
- head = adaugare_nod_sfarsit_lista(head);
- }
- return head;
- }
- /* functie afisare lista cuvinte */
- void afisare_lista(struct NOD * head){
- int i = 0;
- struct NOD * nod_curent;
- if (head == NULL) {
- printf("Atentie, lista este goala!\n");
- return;
- }
- nod_curent = head;
- while (nod_curent != NULL) {
- /* afisare valoare curenta si pozitionare nod urmator */
- printf("%d: %s\n", i++, nod_curent -> cuvant);
- nod_curent = nod_curent -> next;
- }
- }
- /* functie adaugare cuvant la sfarsitul listei */
- struct NOD * adaugare_nod_sfarsit_lista(struct NOD * head) {
- int i = 0;
- struct NOD * nod_curent, * nod_nou;
- if (head == NULL) {
- printf("Atentie: lista este goala.");
- head = creare_nod();
- printf("Cuvantul a fost adaugat.\n");
- return head;
- }
- /*parcurge lista element cu element pentru a ajunge la ultimul nod*/
- nod_curent = head;
- while (nod_curent != NULL) {
- if (nod_curent -> next == NULL) {
- /* creare si inserare nod nou in lista */
- nod_nou = creare_nod();
- nod_curent -> next = nod_nou;
- printf("Cuvantul a fost adaugat.\n");
- return head;
- }
- nod_curent = nod_curent -> next;
- }
- }
- struct NOD * adaugare_nod_inceput_lista(struct NOD *head){
- struct NOD *nod_nou = creare_nod();
- nod_nou->next = head;
- head = nod_nou;
- return head;
- }
- struct NOD * adaugare_cuvant_la_pozitia_n(struct NOD *head){
- int n, i = 0;
- printf("Introdu pozitia la care doresti sa fie introdus cuvantul:");
- scanf("%d", &n);
- if(n == 0)
- return adaugare_nod_inceput_lista(head);
- struct NOD *nod_nou = creare_nod(), *nod_curent = head;
- while(i<n-1 && nod_curent->next!=NULL)
- nod_curent = nod_curent->next, i++;
- if(i<n-1)
- printf("Pozitia specificata este mai mare decat numarul de elemente din lista, NU se poate adauga elementul\n");
- else{
- nod_nou->next = nod_curent->next;
- nod_curent->next = nod_nou;
- }
- return head;
- }
- struct NOD *stergere_primul_element(struct NOD *head){
- struct NOD *aux = head;
- head = head->next;
- free(aux);
- printf("Am sters cu succes elementul\n");
- return head;
- }
- struct NOD *stergere_ultimul_element(struct NOD *head){
- struct NOD *aux = head;
- if(aux == NULL){
- printf("Lista este goala, nu putem sterge niciun element\n");
- return head;
- }
- if(aux->next == NULL){
- printf("Am sters elementul, acum lista este vida");
- free(aux);
- head = NULL;
- return head;
- }
- while(aux->next->next)
- aux = aux->next;
- free(aux->next);
- aux->next = NULL;
- printf("Am sters cu succes elementul\n");
- return head;
- }
- struct NOD * stergere_cuvant_pozitia_n(struct NOD *head){
- int poz=0, n;
- struct NOD *nod_curent = head, *nod;
- printf("\nIntrodu pozitia elementului pe care vrei sa il stergi: ");
- scanf("%d", &n);
- while(poz < n-1 && nod_curent -> next) nod_curent = nod_curent ->next, poz++;
- nod = nod_curent -> next;
- nod_curent -> next = nod -> next;
- free(nod);
- return head;
- }
- struct NOD * stergere_lista(struct NOD *head){
- struct NOD * next_node = head;
- while(head){
- next_node = head->next;
- free(head);
- head = next_node;
- }
- printf("\nLista a fost stearsa cu succes!\nATENTIE, LISTA ESTE GOALA");
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement