#include "intLinkedList.h"
IntLinkedList::IntLinkedList()
{
head=NULL;
tail=NULL;
}
IntLinkedList::~IntLinkedList()
{
isEmpty();
}
int IntLinkedList::size()
{
int size=0;
struct Element* temp = head;
if(temp==NULL)
return size;
while (temp != tail) {
head = temp->next;
size++;
}
return size;
}
int IntLinkedList::isEmpty()
{
if(head==NULL)
return 1;
else
return 0;
}
void IntLinkedList::print()
{
struct Element* temp = head;
int size=0;
while (temp != tail) {
head = temp->next;
size++;}
std::cout << "[";
for(int i=0;i<size;i++){
std::cout << temp->value;
if(i<(size-1))
std::cout << ",";
}
std::cout << "]\n";
}
void IntLinkedList::append(int val)
{
struct Element* new_el= new struct Element;
if (new_el==NULL){
printf("Blad!\n");
return;
}
new_el->value=val;
head->next=NULL;
if(head==NULL){
head=new_el;
}else{
struct Element *temp=head;
while(new_el->next != NULL){
new_el=new_el->next;
}
temp->next=NULL;
}
}
plik nagłówkowy
#pragma once
#include <iostream>
struct Element{
int value;
struct Element *next;
};
class IntLinkedList{
private:
struct Element* head;
struct Element* tail;
public:
IntLinkedList();
~IntLinkedList();
int size(); //funkcja zwracajaca rozmiar listy
int isEmpty(); //funkcja sprawdzająca czy lista jest pusta czy nie
void print(); // drukuje listę w postaci [w1, w2, w3]
void append(int); //wstawia wartość na koniec listy
};
Mianowicie mam problem z rozwiązaniem problemu:
segmentation fault pojawia mi się przy wywołaniu funkcji append
valgrind pokazuje:
"Invalid write of size 8
==323== at 0x108D0C: IntLinkedList::append(int)
.....
HEAP SUMMARY:
==323== in use at exit: 16 bytes in 1 blocks
==323== total heap usage: 3 allocs, 2 frees, 76,816 bytes allocated
...."