Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <string>
- typedef struct Lista {
- char nume[20];
- int varsta;
- Lista* next;
- };
- Lista *first, *last;
- Lista* MemoryAloc() {
- Lista *pointer;
- pointer = (Lista*)malloc(sizeof(Lista));
- return pointer;
- }
- void HeadIntializing() {
- first = MemoryAloc();
- printf("Introduceti in nod datele : \n");
- printf("Nume : ");
- gets_s(first->nume);
- printf("Varsta : ");
- scanf_s("%d", &first->varsta);
- last = first;
- last->next = NULL;
- }
- void AppendNode() {
- //evitam umplerea bufferului
- while (getchar() != '\n');
- Lista* newNode;
- //INTRODUCEM DATELE IN NOD
- newNode = MemoryAloc();
- printf("Introduceti in nod datele : \n");
- printf("Nume : ");
- gets_s(newNode->nume);
- printf("Varsta : ");
- scanf_s("%d", &newNode->varsta);
- //Facem legatura intre ultimul nod si nodul nou creat
- last->next = newNode;
- last = newNode;
- last->next = NULL;
- }
- void AddNodeAtTheBegining() {
- if (first == NULL) {
- HeadIntializing();
- }
- else {
- while (getchar() != '\n');
- Lista *newNode;
- newNode = MemoryAloc();
- printf("Introduceti in nod datele : \n");
- printf("Nume : ");
- gets_s(newNode->nume);
- printf("Varsta : ");
- scanf_s("%d", &newNode->varsta);
- newNode->next = first;
- first = newNode;
- }
- }
- void AddNodeAtEnd() {
- if (first == NULL) {
- printf("\nLista nu exista!");
- }
- else {
- //Evitam umplerea bufferului pentru gets_s
- while (getchar() != '\n');
- Lista* newNode;
- newNode = MemoryAloc();
- printf("Introduceti in nod datele : \n");
- printf("Numele : ");
- gets_s(newNode->nume);
- printf("Varsta : ");
- scanf_s("%d", &newNode->varsta);
- last->next = newNode;
- last = newNode;
- last->next = NULL;
- }
- }
- void SearchAfterName(const char nameSearch[10]) {
- if (first == NULL) {
- printf("\nNu exista elemente in lista");
- }
- else {
- Lista* crossing;
- int k;
- for (crossing = first; crossing; crossing = crossing->next) {
- if ((k = strcmp(crossing->nume, nameSearch)) == 0) {
- printf("S-a gasit!");
- break;
- }
- }
- if(k != 0){
- printf("Nu s-a gasit!");
- }
- }
- }
- void SearchAfterAge(int varsta) {
- if (first == NULL) {
- printf("\nLista nu exista!");
- }
- else {
- Lista* crossing;
- int k = 0;
- for (crossing = first; crossing; crossing = crossing->next) {
- if (crossing->varsta == varsta){
- printf("S-a gasit!");
- k = 1;
- break;
- }
- }
- if (k == 0) {
- printf("\nNu s-a gasit!\n");
- }
- }
- }
- void RemoveNodes(int varsta) {
- Lista *crossing;
- Lista *temp;
- for (crossing = first, temp = first; crossing; crossing = crossing->next) {
- if (first != NULL) {
- if (crossing == first && crossing->varsta == varsta) {
- if (first->next) {
- first = first->next;
- free(crossing);
- crossing->next = first;
- temp = first;
- continue;
- }
- else {
- free(first);
- first = NULL;
- }
- }
- else
- if (crossing->varsta == varsta) {
- temp->next = crossing->next;
- free(crossing);
- crossing = NULL;
- }
- if (crossing == NULL) {
- crossing = temp;
- }
- if (crossing != temp) {
- temp = temp->next;
- }
- }
- else {
- printf("\nLista a ramas fara/nu are elemente!\n");
- break;
- }
- }
- }
- void Display() {
- Lista *crossing;
- printf("\n");
- if (first != NULL) {
- for (crossing = first; crossing; crossing = crossing->next) {
- printf("Nume : %s\n", crossing->nume);
- printf("Varsta : %d\n\n", crossing->varsta);
- }
- }
- else {
- printf("\nLista este goala!\n");
- }
- }
- void main() {
- HeadIntializing();
- //AppendNode();
- //AddNodeAtTheBegining();
- AppendNode();
- // AddNodeAtEnd();
- Display();
- // RemoveNodes(19);
- // Display();
- SearchAfterAge(19);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement