Reputation: 228
Class Listnode is a minimal implementation of linked list. It has basic operations such as Inserting at the head, tail and at a specific position. It also has deletion implementation. So far delete at head and delete at tail works but deleting at a specific index result to it replacing the value with garbage.
Here is the code
#include <iostream>
class ListNode {
friend std::ostream &operator<<( std::ostream &os, const ListNode &n ) {
ListNode *temp = n.head;
os << "[ ";
while ( temp != n.tail ) {
os << temp->data << ", ";
temp = temp->next;
}
os << temp->data << " ]";
return os;
}
private:
ListNode *prev;
int data;
ListNode *next;
int sizeOfList;
ListNode *head, *tail;
bool outOfBound( int index, int size ) {
if ( index >= sizeOfList || index < 0 )
return true;
return false;
}
public:
ListNode() : prev(nullptr), data(0), next(nullptr), sizeOfList(0), head(nullptr), tail(nullptr) {}
void create( int val ) {
ListNode *newNode = new ListNode;
newNode->data = val;
if( head == nullptr )
head = tail = newNode;
else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
++sizeOfList;
}
int size() { return sizeOfList; }
void insertAtHead( int val ) {
ListNode *newNode = new ListNode;
newNode->data = val;
newNode->next = head;
head->prev = newNode;
head = newNode;
++sizeOfList;
}
void insertAtTail( int val ) {
ListNode *newNode = new ListNode;
newNode->data = val;
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
++sizeOfList;
}
void insert( int index, int val ) {
if( index == 0 ) {
insertAtHead( val );
return;
}
if( index == sizeOfList - 1 ) {
insertAtTail( val );
return;
}
if(outOfBound( index, sizeOfList ) )
throw std::out_of_range("Exception: Out of Range.");
int i = 1;
ListNode *temp = head;
ListNode *newNode = new ListNode;
newNode->data = val;
while( i != index ) {
temp = temp->next;
++i;
}
newNode->prev = temp;
newNode->next = temp->next;
temp->next = newNode;
temp->next->prev = newNode;
++sizeOfList;
}
void deleteAtHead() {
ListNode *temp = head;
head = head->next;
head->prev = nullptr;
delete temp;
--sizeOfList;
}
void deleteAtTail() {
ListNode *temp = tail;
tail = tail->prev;
tail->next = nullptr;
delete temp;
--sizeOfList;
}
void erase( int index ) {
if( index == 0 ) {
deleteAtHead();
return;
}
if( index == sizeOfList - 1 ) {
deleteAtTail();
return;
}
if(outOfBound( index, sizeOfList ) )
throw std::out_of_range("Exception: Out of Range.");
int i = 1;
ListNode *temp = head;
while( i != index + 1 ) {
temp = temp->next;
++i;
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
--sizeOfList;
}
};
The main function
#include <iostream>
#include "doubly.hh"
int main() {
ListNode list;
for (int i = 0; i != 11; ++i )
list.create(i);
list.insertAtHead(54);
list.insertAtTail(56);
list.insert(1,67);
list.deleteAtHead();
list.deleteAtTail();
list.erase(1);
std::cout << list << std::endl;
}
On running this code, my output was
[67, -921944048, 1, 2, 3, 4, 5,6,7,8,9,10]
Upvotes: 0
Views: 95
Reputation: 1941
The problem is inside insert
funtcion. The list is being corrupted there.
In your insert function replace the order of these two lines:
temp->next = newNode; // temp->next becomes newMode
temp->next->prev = newNode; // newMode->prev points to newMode itself
They must be:
temp->next->prev = newNode;
temp->next = newNode;
Upvotes: 1