Advertisement
Dr4noel

Liste scrise altfel

May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5.  
  6. typedef struct Lista {
  7.     char nume[20];
  8.     int varsta;
  9.     Lista* next;
  10. };
  11.  
  12. Lista *first, *last;
  13.  
  14. Lista* MemoryAloc() {
  15.     Lista *pointer;
  16.     pointer = (Lista*)malloc(sizeof(Lista));
  17.     return pointer;
  18. }
  19.  
  20. void HeadIntializing() {
  21.     first = MemoryAloc();
  22.     printf("Introduceti in nod datele : \n");
  23.     printf("Nume : ");
  24.     gets_s(first->nume);
  25.     printf("Varsta : ");
  26.     scanf_s("%d", &first->varsta);
  27.     last = first;
  28.     last->next = NULL;
  29. }
  30.  
  31. void AppendNode() {
  32.     //evitam umplerea bufferului
  33.     while (getchar() != '\n');
  34.     Lista* newNode;
  35.     //INTRODUCEM DATELE IN NOD
  36.     newNode = MemoryAloc();
  37.     printf("Introduceti in nod datele : \n");
  38.     printf("Nume : ");
  39.     gets_s(newNode->nume);
  40.     printf("Varsta : ");
  41.     scanf_s("%d", &newNode->varsta);
  42.     //Facem legatura intre ultimul nod si nodul nou creat
  43.     last->next = newNode;
  44.     last = newNode;
  45.     last->next = NULL;
  46. }
  47.  
  48. void AddNodeAtTheBegining() {
  49.     if (first == NULL) {
  50.         HeadIntializing();
  51.     }
  52.     else {
  53.         while (getchar() != '\n');
  54.         Lista *newNode;
  55.         newNode = MemoryAloc();
  56.         printf("Introduceti in nod datele : \n");
  57.         printf("Nume : ");
  58.         gets_s(newNode->nume);
  59.         printf("Varsta : ");
  60.         scanf_s("%d", &newNode->varsta);
  61.         newNode->next = first;
  62.         first = newNode;
  63.     }
  64.  
  65. }
  66.  
  67. void AddNodeAtEnd() {
  68.     if (first == NULL) {
  69.         printf("\nLista nu exista!");
  70.     }
  71.     else {
  72.         //Evitam umplerea bufferului pentru gets_s
  73.         while (getchar() != '\n');
  74.         Lista* newNode;
  75.         newNode = MemoryAloc();
  76.         printf("Introduceti in nod datele : \n");
  77.         printf("Numele : ");
  78.         gets_s(newNode->nume);
  79.         printf("Varsta : ");
  80.         scanf_s("%d", &newNode->varsta);
  81.         last->next = newNode;
  82.         last = newNode;
  83.         last->next = NULL;
  84.     }
  85. }
  86.  
  87. void SearchAfterName(const char nameSearch[10]) {
  88.     if (first == NULL) {
  89.         printf("\nNu exista elemente in lista");
  90.     }
  91.     else {
  92.         Lista* crossing;
  93.         int k;
  94.         for (crossing = first; crossing; crossing = crossing->next) {
  95.             if ((k = strcmp(crossing->nume, nameSearch)) == 0) {
  96.                 printf("S-a gasit!");
  97.                 break;
  98.             }
  99.         }
  100.         if(k != 0){
  101.             printf("Nu s-a gasit!");
  102.         }
  103.     }
  104. }
  105.  
  106. void SearchAfterAge(int varsta) {
  107.     if (first == NULL) {
  108.         printf("\nLista nu exista!");
  109.     }
  110.     else {
  111.         Lista* crossing;
  112.         int k = 0;
  113.         for (crossing = first; crossing; crossing = crossing->next) {
  114.             if (crossing->varsta == varsta){
  115.                 printf("S-a gasit!");
  116.                 k = 1;
  117.                 break;
  118.             }
  119.         }
  120.         if (k == 0) {
  121.             printf("\nNu s-a gasit!\n");
  122.         }
  123.     }
  124. }
  125.  
  126. void RemoveNodes(int varsta) {
  127.     Lista *crossing;
  128.     Lista *temp;
  129.     for (crossing = first, temp = first; crossing; crossing = crossing->next) {
  130.         if (first != NULL) {
  131.             if (crossing == first && crossing->varsta == varsta) {
  132.                 if (first->next) {
  133.                     first = first->next;
  134.                     free(crossing);
  135.                     crossing->next = first;
  136.                     temp = first;
  137.                     continue;
  138.                 }
  139.                 else {
  140.                     free(first);
  141.                     first = NULL;
  142.                 }
  143.             }
  144.             else
  145.                 if (crossing->varsta == varsta) {
  146.                     temp->next = crossing->next;
  147.                     free(crossing);
  148.                     crossing = NULL;
  149.                 }
  150.  
  151.             if (crossing == NULL) {
  152.                 crossing = temp;
  153.             }
  154.             if (crossing != temp) {
  155.                 temp = temp->next;
  156.             }
  157.         }
  158.         else {
  159.             printf("\nLista a ramas fara/nu are elemente!\n");
  160.             break;
  161.         }
  162.     }
  163. }
  164.  
  165. void Display() {
  166.     Lista *crossing;
  167.     printf("\n");
  168.     if (first != NULL) {
  169.         for (crossing = first; crossing; crossing = crossing->next) {
  170.             printf("Nume : %s\n", crossing->nume);
  171.             printf("Varsta : %d\n\n", crossing->varsta);
  172.         }
  173.     }
  174.     else {
  175.         printf("\nLista este goala!\n");
  176.     }
  177. }
  178.  
  179. void main() {
  180.     HeadIntializing();
  181.     //AppendNode();
  182.     //AddNodeAtTheBegining();
  183.     AppendNode();
  184. //  AddNodeAtEnd();
  185.     Display();
  186. //  RemoveNodes(19);
  187. //  Display();
  188.     SearchAfterAge(19);
  189.     _getch();
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement