Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include<fstream>
- using namespace std;
- struct nastepny;
- struct lista
- {
- nastepny *pierwszy;
- lista();
- void dodaj();
- void pokaz();
- void usun(int wart);
- };
- lista::lista()
- {
- pierwszy = NULL;
- }
- struct nastepny
- {
- nastepny *next;
- int liczba;
- nastepny();
- };
- nastepny::nastepny()
- {
- next = 0;
- }
- void lista::dodaj()
- {
- nastepny *nowy = new nastepny();
- int x;
- cin >> x;
- nowy->liczba=x;
- nastepny *buf;
- buf = pierwszy;
- pierwszy = nowy;
- pierwszy->next = buf;
- }
- void lista::pokaz()
- {
- cout <<endl<< "Lista"<<endl;
- nastepny *temp=pierwszy;
- while (temp)
- {
- cout<<temp->liczba<<endl;
- temp = temp->next;
- }
- }
- void lista::usun(int wart)
- {
- nastepny *poprzedni = pierwszy;
- if (poprzedni)
- {
- nastepny *temp = pierwszy->next;
- if (poprzedni->liczba == wart)
- {
- delete poprzedni;
- pierwszy = temp;
- }
- else {
- while (temp)
- {
- if (temp->liczba == wart)
- {
- poprzedni->next = temp->next;
- delete temp;
- cout << "Usunięto podany element"<<endl;
- break;
- }
- else
- {
- poprzedni = temp;
- temp = temp->next;
- }
- }
- }
- }
- }
- int main()
- {
- lista lista1;
- cout << "Podaj 1 liczbe: ";
- lista1.dodaj();
- cout << "Podaj 2 liczbe: ";
- lista1.dodaj();
- cout << "Podaj 3 liczbe: ";
- lista1.dodaj();
- lista1.pokaz();
- cout << "Podaj liczbe do usunięcia: ";
- int x;
- cin >> x;
- lista1.usun(x);
- lista1.pokaz();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement