Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "intLinkedList.h"
- #include <iostream>
- #include <cstring>
- Element::Element(){
- next=nullptr;
- }
- Element::Element(int num): value{num}{
- next=nullptr;
- }
- Element::Element(const Element & el){
- next=el.next;
- value=el.value;
- }
- Element::~Element(){
- }
- IntLinkedList::IntLinkedList():head{nullptr},tail{nullptr}{
- strcpy(name,"LX");
- }
- IntLinkedList::IntLinkedList(const char *nazwa):head{nullptr},tail{nullptr}{
- strcpy(name,nazwa);
- }
- IntLinkedList::~IntLinkedList(){
- Element *ptr = head;
- while(ptr!=tail){
- ptr=ptr->next;
- delete head;
- head=ptr;
- }
- delete tail;
- head=nullptr;
- tail=nullptr;
- if(isEmpty()){
- std::cout << "Destruktor "<< name <<": Lista pusta" << std::endl;
- }
- }
- bool IntLinkedList::isEmpty(){
- if (head==nullptr){
- return true;
- }
- else{
- return false;
- }
- }
- bool IntLinkedList::contains(int num){
- if(!isEmpty()){
- Element *ptr = head;
- while((ptr!=nullptr)){
- if(ptr->value==num){
- return true;
- }
- ptr=ptr->next;
- }
- return false;
- }
- else
- return false;
- }
- int IntLinkedList::size(){
- if (isEmpty()){
- return 0;
- }
- else{
- Element * ptr = head;
- int counter = 0;
- while(ptr!=tail){
- counter++;
- ptr=ptr->next;
- }
- counter++;
- return counter;
- }
- }
- void print(IntLinkedList &lista){
- }
- void IntLinkedList::print(){
- if(!isEmpty()){
- std::cout << "[";
- int size = this->size();
- Element *ptr = head;
- for(int i=0;i<size-1;i++){
- std::cout << ptr->value << ", ";
- ptr=ptr->next;
- }
- std::cout << tail->value << "]" << std::endl;
- }
- }
- void IntLinkedList::append(Element &el){
- if(isEmpty()){
- prepend(el);
- }
- else{
- tail->next=⪙
- tail=⪙
- }
- }
- void IntLinkedList::append(IntLinkedList &lista){}
- void IntLinkedList::prepend(Element &el){
- if (isEmpty()){
- head=⪙
- tail=⪙
- }
- else{
- el.next=head;
- head=⪙
- }
- }
- void IntLinkedList::prepend(int num){
- if (isEmpty()){
- Element * newElement = new Element(num);
- head=newElement;
- tail=newElement;
- }
- else{
- Element * newElement = new Element(num);
- newElement->next=head;
- head=newElement;
- }
- }
- void IntLinkedList::addSorted(IntLinkedList &lista){
- }
- void IntLinkedList::addSorted(Element &el, bool uniq){
- if(uniq==true){
- if(contains(el.value)){}
- else{
- if(isEmpty() || el.value<head->value){
- prepend(el);
- }
- else{
- if(el.value>tail->value){
- tail->next=⪙
- tail=⪙
- }
- else{
- Element *ptr=head;
- while(ptr->value<el.value){
- ptr=ptr->next;
- }
- el.next=ptr->next;
- ptr->next=⪙
- }
- }
- }
- }
- else{
- if(isEmpty() || el.value<head->value){
- prepend(el);
- }
- else{
- if(el.value>tail->value){
- tail->next=⪙
- tail=⪙
- }
- else{
- Element *ptr=head;
- while(ptr->value<el.value){
- ptr=ptr->next;
- }
- el.next=ptr->next;
- ptr->next=⪙
- }
- }
- }
- }
- void IntLinkedList::addSorted(int num,bool uniq){
- if(uniq==true){
- if(contains(num)){}
- else{
- if(isEmpty() || num<head->value){
- prepend(num);
- }
- else{
- if(num>tail->value){
- Element *el = new Element(num);
- tail->next=el;
- tail=el;
- }
- else{
- Element *ptr=head;
- while(ptr->value<num){
- ptr=ptr->next;
- }
- Element *el = new Element(num);
- el->next=ptr->next;
- ptr->next=el;
- }
- }
- }
- }
- else{
- if(isEmpty() || num<head->value){
- prepend(num);
- }
- else{
- if(num>tail->value){
- Element *el = new Element(num);
- tail->next=el;
- tail=el;
- }
- else{
- Element *ptr=head;
- while(ptr->value<num){
- ptr=ptr->next;
- }
- Element *el = new Element(num);
- el->next=ptr->next;
- ptr->next=el;
- }
- }
- }
- }
- void IntLinkedList::removeFirst(){
- if(!isEmpty()){
- Element *ptr = new Element(*(head->next));
- delete head;
- head=ptr;
- }
- }
- void IntLinkedList::removeLast(){
- if(!isEmpty()){
- if(size()==1){
- delete tail;
- tail=nullptr;
- head=nullptr;
- }
- else{
- Element *ptr=head;
- for(int i=0;i<size()-2;i++){
- ptr=ptr->next;
- }
- delete tail;
- tail = ptr;
- tail->next=nullptr;
- }
- }
- }
- void IntLinkedList::removeValue(int num){
- if(!isEmpty() && contains(num)){
- Element *ptr = head;
- Element *before = head;
- while(ptr->value!=num){
- before=ptr;
- ptr=ptr->next;
- }
- if (ptr==tail){
- removeLast();
- }
- else if (ptr==head){
- ptr=ptr->next;
- delete head;
- head=ptr;
- }
- else{
- before->next=ptr->next;
- delete ptr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement