Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <conio.h>
- using namespace std;
- typedef struct nod {
- int numar;
- nod *urm;
- };
- nod *prim = NULL, *ultim =NULL;
- nod *alloc(){
- return new nod;
- }
- void citire(int dimensiune) {
- prim = alloc(); //am alocat memorie pentru prim ,capul listei
- cout << "Numar = ";
- cin >> prim->numar; //adaug numar in capul listei (prim)
- ultim = prim; //primul e si ultimul
- ultim->urm = NULL;
- for (int i = 0; i < dimensiune - 1;i++) {
- nod *temp;
- temp = alloc();
- cout << "Temp = ";
- cin >> temp->numar;
- ultim->urm = temp;
- ultim = temp;
- ultim->urm = NULL;
- }
- }
- void afisare() {
- nod *temp = prim;
- cout << "\nAfisare: \n";
- while (temp != NULL) {
- cout << temp->numar<<" ";
- temp = temp->urm;
- }
- }
- void adaugarefinal() {
- nod *temp;
- temp = alloc();
- cout << "\nnod = ";
- cin >> temp->numar;
- ultim->urm = temp;
- ultim = temp;// ultimul se muta pe noul nod numit temp
- ultim ->urm = NULL;
- }
- void adaugareinceput() {
- nod *temp;
- temp = alloc();
- cout << "\n noul prim = ";
- cin >> temp->numar;
- temp->urm = prim;
- prim = temp;// prim se muta pe temp
- }
- void adaugarelapozitienod(int pozitie) {//adaugare dupa respectiva pozitie data de noi
- int i = 1;
- nod * temp = prim;
- int exista = 1;
- while (i != pozitie) {
- i++;
- if (temp == NULL) {
- exista = 0;
- break;
- }
- temp = temp->urm;// null->urm = ceva->urm
- }
- if (exista == 1) {
- nod *nouNod;
- nouNod = alloc();
- cout << "\nceva numar = ";
- cin >> nouNod->numar;
- nouNod->urm = temp->urm;
- temp->urm = nouNod;
- }
- else {
- cout << "\nNu exista pozitia data " << pozitie << endl;
- }
- }
- void eliminareprimul() {
- if (prim != NULL) {
- nod *temp = prim;
- if (prim->urm != NULL) {
- prim = prim->urm;
- delete temp;
- }
- else
- cout << "\nNu exista un al 2lea nod\n";
- }
- }
- void eliminareultim() {
- nod *temp = prim;
- while (temp->urm != ultim) {
- temp = temp->urm; // conform while asta , temp devine penultimul gen
- }
- ultim = temp;
- temp = ultim->urm;
- delete temp;
- ultim->urm = NULL;
- }
- void eliminareDePeOPozData(int pozitie) { // eliminam nodul dupa pozitia "data" (dupa inseamna urmatorul nod)
- if (prim != NULL) {
- nod *temp;
- temp = prim;
- int i = 1;
- int exista = 1;
- while (i != pozitie) {
- i++;
- if (temp == NULL){
- exista = 0;
- break;
- }
- temp = temp->urm; // tot ce face while asta este sa mute "temp" pe pozitia data de noi,
- }
- if (exista == 1) {
- nod *nod_temp;
- nod_temp = temp->urm;
- temp->urm = temp->urm->urm;
- delete nod_temp;
- }
- else
- cout << "\n Nu exista nodul dupa pozitia: " << pozitie << endl;
- }
- }
- void main() {
- int n ;
- int pozitie = 3;
- int pozitie_eliminare = 5;
- cout << "n = ";
- cin >> n;
- eliminareprimul();
- citire(n);
- afisare();
- eliminareDePeOPozData(pozitie_eliminare);
- adaugarefinal();
- afisare();
- adaugareinceput();
- afisare();
- adaugarelapozitienod(pozitie);
- afisare();
- eliminareDePeOPozData(pozitie_eliminare);// toate functiile ar trebuii sa se numeasca "eliminare DUPA o pozitie data"
- afisare();
- eliminareprimul();
- afisare();
- eliminareultim();
- afisare();
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement