Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <exception>
- using namespace std;
- struct Node{
- int data;
- Node* next;
- }
- class SLL{
- private:
- Node* t;
- void add(int &zahl, Node* &w);
- void print_helper(Node* curr);
- void size_helper(Node* curr, int zahl);
- void clear_helper(Node* &curr);
- public:
- SLL(){
- t = nullptr;
- };
- ~SLL(){
- clear();
- };
- void print();
- void size();
- void clear();
- void append(int zahl);
- int search(int zahl);
- }
- void SLL::add(int &zahl, Node* w){
- if (w == nullptr){
- t = new Node;
- t ->data = zahl;
- }else{
- Node* curr = t;
- Node* prev = nullptr;
- while(curr != nullptr && curr -> data < zahl){
- prev = curr;
- curr = curr -> next;
- }
- if(prev == nullptr){
- Node* tmp = new Node;
- tmp -> data = zahl;
- t = tmp;
- tmp -> next = curr;
- }
- if(curr == nullptr){
- Node* tmp = new Node;
- tmp -> data = zahl;
- prev -> next = tmp;
- }
- else{
- Node* tmp = new Node;
- tmp -> data = zahl;
- tmp -> next = curr;
- prev -> next = tmp;
- }
- }
- }
- void SLL::append(int zahl){
- add(zahl, t);
- };
- void SLL::print(){
- print_helper(t);
- }
- void SLL::print_helper(Node* &curr){
- if (curr != nullptr){
- cout << curr ->data << endl;
- print_helper(curr -> next);
- }
- }
- void SLL::size(){
- size_helper(t, 0);
- }
- void SLL::size_helper(Node* &curr, int zahl){
- if (curr != nullptr){
- zahl++;
- size_helper(curr->next, zahl);
- }
- cout << zahl << endl;
- }
- void SLL::clear(){
- if(t != nullptr){
- clear_helper(t);
- }
- }
- void SLL::clear_helper(Node* &curr){
- if(curr -> next != nullptr){
- clear_helper(curr -> next);
- }else{
- delete curr;
- curr = nullptr;
- }
- }
- void SLL::search(int zahl){
- if (t == nullptr){
- throw logic_error("LEER");
- }else{
- int idx = 0;
- Node* curr = t;
- while (curr != nullptr){
- if(curr -> data == zahl){
- return idx;
- }
- idx++;
- curr = curr -> next;
- }
- throw logic_error("NIX GUT!");
- }
- }
- int main(int argc, char* argv[]){
- try{
- if (argc < 2){
- throw logic_error("too little arguments");
- }else{
- }
- return 0;
- }catch (logic_error &e){
- cerr << e.what << endl;
- return 1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement