Sep 28, 2009 at 3:28pm UTC
Hi! I'm fairly new to this website.
I've been encountering a segmentation fault error with the code below. I'm developing the methods of a double-linked list.
When I try to use removeElemInPosition(int i), that is a function that removes an element in the position 'i' from a list, the console displays a "Segmentation Fault" error, but this only happens when I am trying to remove an element (from a non-empty list) that isn't the first, last or the last but one. I know what a segmentation fault error is, but I can't figure out why it is happening here.
Here's the code (the removeElemInPosition( int i ) method is at the very beginning): //Sorry that the implementations are within the class definition.
#ifndef __LIST_H__
#define __LIST_H__
#include <iostream>
#include <cassert>
using namespace std;
template<class T>
class List {
public:
void removeElemInPosition(int i) {
Node* currentPtr = firstPtr;
int j = 0;
assert(!(isEmpty()));
if( i==0 ){
removeFirst();
}else{ if( i==(width()-1)){
removeLast();
}else{ while( j <= i ){
if( currentPtr->elem != elemInPosition(i) ){
currentPtr = currentPtr->nextPtr;
j++;
}else{
(currentPtr->nextPtr)->previousPtr = currentPtr->previousPtr;
(currentPtr->previousPtr)->nextPtr = currentPtr->nextPtr;
delete currentPtr;
j++;
}
}
size--;
}
}
}
List(){
size=0;
firstPtr=NULL;
lastPtr=NULL;
}
bool operator==(const List& l) const{
int i1= l.width();
int i2 = width();
bool result = (i1==i2);
int j=0;
while(result && (j<width())) {
if(elemInPosition(j) == l.elemInPosition(j)){
result = true;
}else{
result = false;
}
j++;
}
return result;
}
bool operator!=(const List& l) const{
int i1 = l.width();
int i2 = width();
bool result = (i1 != i2);
int j = 0;
while((!(result) && j<width())){
if(elemInPosition(j) != l.elemInPosition(j)){
result = true;
}else{
result = false;
}
j++;
}
return result;
}
void addFront(const T& e){
Node *tempPtr = new Node;
tempPtr->elem=e;
if(isEmpty()){
firstPtr = tempPtr;
lastPtr = tempPtr;
}else{
tempPtr->nextPtr = firstPtr;
tempPtr->previousPtr= NULL;
firstPtr=tempPtr;
}
size++;
}
void addBack(const T& e){
Node *tempPtr = new Node;
tempPtr->elem=e;
if(isEmpty()){
firstPtr = tempPtr;
lastPtr = tempPtr;
}else {
lastPtr->nextPtr= tempPtr;
tempPtr->previousPtr = lastPtr;
lastPtr = tempPtr;
}
size++;
}
int width() const{
return size;
}
bool isEmpty() const{
return (size ==0);
}
const T& first() const{
assert(!isEmpty());
return firstPtr->elem;
}
const T& last() const{
assert(!isEmpty());
return lastPtr->elem;
}
void removeFirst(){
Node* nxt = new Node;
assert((!isEmpty()));
if(width() == 1){
firstPtr=NULL;
lastPtr=NULL;
size = 0;
}else{
nxt = firstPtr;
firstPtr=firstPtr->nextPtr;
delete nxt;
size--;
}
}
void removeLast(){
Node* prev = new Node;
assert(!(isEmpty()));
if(width()==1){
lastPtr = NULL;
firstPtr = NULL;
size=0;
}else {
prev = lastPtr;
lastPtr = lastPtr->previousPtr;
delete prev;
size--;
}
}
void showList(ostream& out) const{
int i = 0;
if (isEmpty()){
out <<"[ ]";
}else{
out <<"[";
Node* temp = new Node;
temp = firstPtr;
while(i<width()) {
out << temp->elem;
if(i==width()-1){
out << "]";
}else{
out<<",";
temp=temp->nextPtr;
}
i++;
}
}
}
T elemInPosition(int i) const {
Node* temp = new Node;
temp = firstPtr;
T e;
int j = 0;
if( i==(width()-1)){
e = lastPtr->elem;
}else{
while(j<width()){
if(j==i){
e = temp->elem;
j = width();
}else{
temp=temp->nextPtr;
}
j++;
}
}
return e;
}
private:
class Node{
public:
T elem;
Node* previousPtr;
Node* nextPtr;
private:
};
int size;
Node* firstPtr;
Node* lastPtr;
};
#endif