Sonic and Sega Retro Message Board: Help with some C++ for a class - implementing a dequeue - Sonic and Sega Retro Message Board

Jump to content

Hey there, Guest!  (Log In · Register) Help
  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

Help with some C++ for a class - implementing a dequeue This is driving me effing nuts!

#16 User is offline Black Squirrel 

Posted 20 October 2016 - 04:10 PM

  • wikiman
  • Posts: 3620
  • Joined: 27-December 03
  • Gender:Male
  • Location:Northumberland, England
  • Project:thinking of a better member title
  • Wiki edits:20,569
const is another thing I'd probably ignore in this case until the very end. Because again, further layer of complication that will mess with your understanding of linked lists.


Basically it's just:
- make a new node
- give it a value
- tell the node at the front of the list to point to your new node


best bet is to get it working, then worry about which bits might be accidentally modified later.

#17 User is offline Travelsonic 

Posted 25 October 2016 - 06:16 PM

  • Posts: 701
  • Joined: 01-March 05
OK update: I went to the prof. last week, was told I can go use C++ (which means going to an implementation using classes) - just about got the damn thing working. Such a relief., being able to use what I am used to, have done before. Not that I don't want to learn how to do this in C, but I've got some catching up to do, and the fact I can do this with C++ instead of having to do it in C means I can do that very quickly.
This post has been edited by Travelsonic: 25 October 2016 - 06:18 PM

#18 User is offline Travelsonic 

Posted 25 October 2016 - 06:29 PM

  • Posts: 701
  • Joined: 01-March 05
That said, pardon the double post, I am still having one minor issue with popping nodes off the back of the list (and maybe pushing them on the back as well) - pushing and popping off the front of the list I have down OK (I think), doing so from the other end gives me segmentaton faults though.

Here are the linked list class function implementations I have done:

#include "list.h"
// list CLASS FUNCTION IMPLEMENTATIONS:
// CONSTRUCTOR(S) AND DESTRUCTOR:
// Constructor(s):
list::list(){
head = NULL;
tail = NULL;
}
// Destructor:
list::~list(){
while(head != tail){
popFront();
}
delete head;
delete tail;
head = NULL;
tail = NULL;
}
// ACCESSORS AND MUTATORS:
// Accessors:
// Return true if the only element on the list is the header, otherwise return false
bool list::isEmpty(){
return ((head == NULL) || ((head->getNext()) == NULL) || ((head->getNext()) == head)) ? true : false;
}
// Return true if the value appears in the list, otherwise, return false
bool list:: findInList(const listInfo value){
node * tmpNode; // Create a temporary node for iterating through the nodes in the list
bool result = false; // Create a second variable to hold our return value, initialized false so it only changes if we find our value,
// Iterate through the list checking each node's value
// Since the list can be circular, checks against the node being
// NULL, OR having a next node pointing back at the head.
for(tmpNode = head; ((tmpNode != NULL) && (tmpNode->getNext() != head)); tmpNode = tmpNode->getNext()){
if((tmpNode->getData()) == value){result = true;}
}
delete [] tmpNode; // Free the memory allocated to tmpNode
tmpNode = NULL;
return result;
}
// Return the length of the list
size_t list::getLength(){
const node * cursor;
size_t len = 0;
for (cursor = head; ((cursor != NULL) && (cursor->getNext() != head)); cursor = cursor->getNext()){
++len;
}
return len;
}
// Return the length of the list - const version of the function
size_t list::getLength() const{
const node * cursor;
size_t len = 0;
for (cursor = head; ((cursor != NULL) && (cursor->getNext() != head)); cursor = cursor->getNext()){
++len;
}
return len;
}
// Return the head node of our list
node * list::getHead(){
return head;
}
// Return the head node of our list - const version of the function
const node * list::getHead() const{
return head;
}
// Return the tail node of our list
node * list::getTail(){
return tail;
}
// Return the tail node of our list - const version of the function
const node * list::getTail() const{
return tail;
}
// Mutators:
// Initialize the list with the header pointing at itself
void list::initializeList(const listInfo & inData){
if((head != NULL) && (tail != NULL)){return;} // Prevents us from just calling it again without clearing the list first.
head = new node(inData, head, head);
tail = head;
}
// Adds a value to the front of the list
void list::pushFront(const listInfo & inData){
if(head == NULL){initializeList(inData); return;}
head = new node(inData, head, tail);
head->getNext()->setPrev(head);
}
// Adds a value to the back of the list
void list::pushBack(const listInfo & inData){
if(tail == NULL){initializeList(inData); return;}
tail->setNext(new node(inData));
tail = tail->getNext();
}
// Remove and return the value at the front of the list
listInfo list::popFront(){
if(head==NULL){return -1;}
listInfo nodeData = head->getData(); // Retrieve the data from the head node
node * removed = head;
head = head->getNext();
delete removed;
return nodeData;
}
// Remove and return the value at the back of the list
listInfo list::popBack(){
if(tail==NULL){return -1;}
listInfo nodeData = tail->getData(); // Retrieve the data from the head node
node * removed = tail;
tail = tail->getPrev();
delete removed;
return nodeData;
}
// Clear the list, setting the head (and tail) to NULL
void list::clearList(){
if(head == NULL){return;}
while(head != tail){
popFront();
}
head = NULL;
tail = NULL;
}


Probably some mundane, stupid fucking thing I am messing up...
This post has been edited by Travelsonic: 25 October 2016 - 06:29 PM

  • 2 Pages +
  • 1
  • 2
    Locked
    Locked Forum

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users