+skyler_sdf Posted December 9, 2011 Report Posted December 9, 2011 1. Se cere sa sa steara toate numerele palindroame dintr.o lista liniara simplu inlantuita. Se va crea un meniu cu optiunile: creare lista, adaugare nod in lista, stergerea tuturor palindroamelor, transformarea listei in lista circulara. #include <stdio.h> #include <conio.h> typedef struct nod { int key; //valoare nod *next; //pointer catre nodul urmator } tNod; tNod *pFirst; //creaza lista tNod* CreateList(int someValue) { tNod *pListFirst; pListFirst = new(tNod); pListFirst->key = someValue; pListFirst->next = NULL; return pListFirst; } //adauga element in lista void AddToList(int someValue, tNod *pListFirst) { if(!pListFirst) { printf("Selectati intai optiunea 1!\n"); return; } tNod *pNod, *newNod = new(tNod); pNod = pListFirst; while(pNod->next) { pNod = pNod->next; } pNod->next = newNod; newNod->key = someValue; newNod->next = NULL; } //afiseaza lista void PrintList(tNod *pFirstList) { if(!pFirstList) return; tNod *pNod = pFirstList; while(pNod->next && pNod->next != pFirstList) { printf("%d -> ", pNod->key); pNod = pNod->next; } printf("%d\n", pNod->key); } //verifica daca numarul este palindrom bool IsPalindrom(int n) { int orig = n, inv = 0; while(n > 0) { inv = inv * 10 + (n % 10); n = n / 10; } if(inv == orig) return true; else return false; } tNod* CreateList2(int someValue) { tNod *pListFirst; pListFirst = new(tNod); pListFirst->key = someValue; pListFirst->next = NULL; return pListFirst; } //adauga element in lista2 void AddToList2(int someValue, tNod *pListFirst) { if(!pListFirst) { printf("Selectati intai optiunea 1!\n"); return; } tNod *pNod, *newNod = new(tNod); pNod = pListFirst; while(pNod->next) { pNod = pNod->next; } pNod->next = newNod; newNod->key = someValue; newNod->next = NULL; } //sterge toate palindroamele din lista tNod* EraseP(tNod *pFirstList) { tNod *pOrigFirst = pFirstList; tNod *pFirstE, *pImd; pImd = NULL; //parcurgere lista pana la primul palindrom int count =0; while(pFirstList->next && !IsPalindrom(pFirstList->key)) { if(count == 0) pImd = CreateList2(pFirstList->key); else AddToList2(pFirstList->key,pImd); pFirstList = pFirstList->next; } //daca am ajuns la final iesim if(pFirstList == NULL) return pFirstList; if(IsPalindrom(pFirstList->key)) { pFirstList = pFirstList->next; } //daca am ajuns la final iesim if(pFirstList == NULL) return pFirstList; pFirstE = pFirstList; //cat timp mai sunt elemente while(pFirstList->next ) { //daca nu e palindrom adaug in pImd if(!IsPalindrom(pFirstList->key)) { AddToList2(pFirstList->key,pImd); } pFirstList = pFirstList->next; } //prelucrare ultimul element if(!IsPalindrom(pFirstList->key)) { AddToList2(pFirstList->key,pImd); } return pImd; } //transforma lista in lista circulara void TransfToCirc(tNod *pFirstE) { while(pFirstE->next) { pFirstE = pFirstE->next; } pFirstE->next = pFirst; } //meniu void PrintMenu() { printf("\n1. Creare lista simplu inlantuita"); printf("\n2. Adaugare nod in lista simpla inlantuita"); printf("\n3. Stergere nr tip palindrom"); printf("\n4. Transf in lista circulara"); printf("\n5. Iesire\n"); } //initializeaza lista void Init() { pFirst = NULL; } int main() { Init(); int choise = 0; int n; PrintMenu(); scanf("%d", &choise); while(choise != 5) { switch(choise) { case 1: printf("Introduceti un elem: "); scanf("%d", &n); pFirst = CreateList(n); PrintList(pFirst); break; case 2: printf("Introduceti un elem: "); scanf("%d", &n); AddToList(n, pFirst); PrintList(pFirst); break; case 3: pFirst = EraseP(pFirst); PrintList(pFirst); break; case 4: TransfToCirc(pFirst); PrintList(pFirst); break; default: break; } PrintMenu(); scanf("%d", &choise); } return 1; } 2. Sa se creeze un meniu ce contine urmatoarele optiuni: creare lista dublu inlantuita, adaugare nod in lista dublu inlantuita (aceeasi observatie ca la 1.2), stergere noduri ce contin valori numere perfecte (exemplu nr perfect: 6 = 1 + 2 + 3; suma divizorilor exceptandu-l pe 6 este numarul in sine), iesire din meniu. #include <stdio.h> #include <conio.h> typedef struct nod { int key; nod *next; nod *prev; } tNod; tNod *pList; void CreateList() { if(!pList) { pList = new tNod; pList->key = -1; pList->next = NULL; pList->prev = NULL; } } //adauga element in lista bool AddToList(tNod *pListToAdd) { if(!pListToAdd) { printf("Lista necreata!\n"); return false; } int n; tNod *pNewNod; printf("Introduceti valoarea: "); scanf("%d", &n); if(n <= 0) { printf("\nNr e negativ, dati un nr pozitiv nenul!\n"); return false; } if(pListToAdd->key == -1) { pListToAdd->key = n; } else { while(pListToAdd->next) { pListToAdd = pListToAdd->next; } pNewNod = new(tNod); pListToAdd->next = pNewNod; pNewNod->prev = pListToAdd; pNewNod->next = NULL; pNewNod->key = n; } return true; } //sterge nod void eraseNode(tNod **pListToRemoveFrom, tNod *pNod) { if(pNod == *pListToRemoveFrom) { pNod = (*pListToRemoveFrom)->next; if(pNod) pNod->prev = NULL; delete(*pListToRemoveFrom); *pListToRemoveFrom = pNod; } else if(pNod->next == NULL) { pNod->prev->next = NULL; delete(pNod); } else { pNod->next->prev = pNod->prev; pNod->prev->next = pNod->next; delete(pNod); } } //verifica daca un nr e perfect bool verifyIsPerfect(int n) { int k,i; int sum = 0; for(i = 1; i <= n/2; i++) { k = n % i; if(!k) { sum += i; } } if(sum == n) return true; else return false; } // stergere patrate perfecte void erasePerfect(tNod *pListToRemovePerfect) { tNod *pNod; while(pListToRemovePerfect) { pNod = pListToRemovePerfect; pListToRemovePerfect = pListToRemovePerfect->next; if(verifyIsPerfect(pNod->key)) eraseNode(&pList, pNod); } } //printeaza lista void PrintList(tNod *pFirstList) { if(!pFirstList) return; tNod *pNod = pFirstList; while(pNod->next) { printf("%d -> ", pNod->key); pNod = pNod->next; } printf("%d\n", pNod->key); } //meniu void Menu() { printf("\n1. Creare lista dublu inlantuita"); printf("\n2. Adaugare nod in lista dublu inlantuita"); printf("\n3. Stergere noduri nr perfecte"); printf("\n4. Iesire\n\n"); } int main() { pList = NULL; int choise = 0; int n; Menu(); scanf("%d", &choise); while(choise != 4) { switch(choise) { case 1: CreateList(); printf("\nLista a fost creata!\n"); break; case 2: AddToList(pList); PrintList(pList); break; case 3: erasePerfect(pList); PrintList(pList); break; default: break; } Menu(); scanf("%d", &choise); } return 0; } 1
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now