I\'m not sure what exactly is wrong with my linked list? Can anyone help me out?
ID: 657572 • Letter: I
Question
I'm not sure what exactly is wrong with my linked list? Can anyone help me out? I'd really appreciate it!
// week10LinkedList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
using namespace std;
template
class llNode{
public:
datatype datavalue;
llNode *Next;
};
template
class cLinkedList{
public:
cLinkedList();
bool listEmpty(void);
void addBegin(datatype datavalue);
datatype returnBegin(void);
void removeBegin(void);
void moveCurrentBegin(void);
void moveCurrentNext(void);
datatype returnCurrent(void);
bool currentNull(void);
void insertCurrent(datatype data);
void removeCurrent(void);
void insertEnd(datatype data);
datatype returnEnd(void);
void removeEnd(void);
int countnodes(void);
bool searchList(datatype x);
bool operator == (cLinkedList obj);
cLinkedList operator +(cLinkedList obj);
~cLinkedList();
private:
llNode *head,
*current;
};
template
cLinkedList::cLinkedList(){ //Constructor
head = NULL;
current = NULL;
}
//Contructor
template
bool cLinkedList::listEmpty(){ //List Empty
bool tempvalue = true;
if(head != NULL){
tempvalue = false;
}
return tempvalue;
}
template
void cLinkedList::addBegin(datatype data){ //Add Begin
llNode *temp;
temp = new llNode;
temp->datavalue = data;
temp->Next = head;
head = temp;
}
template
datatype cLinkedList:: returnBegin(void){ //Return Begin - Must check list
return head->datavalue;
}
template
void cLinkedList::removeBegin(void){ //Remove Begin
if(!listEmpty()){
llNode *temp;
temp = head;
head = head->Next;
delete temp;
}
template
cLinkedList cLinkedList::operator+(cLinkedList obj){
cLinkedList newlist;
if(!listEmpty()){
moveCurrentBegin();
while(current != NULL){
newlist.insertEnd(current->datavalue);
moveCurrentNext();
}
}
if(!obj.listEmpty()){
obj.moveCurrentBegin();
while(current != NULL){
newlist.insertEnd(obj.current->datavalue);
obj.moveCurrentnext();
}
}
return newlist;
};
template
bool cLinkedList::operator==(cLinkedList obj){
bool llequal = false;
if(countnodes() == obj.countnodes()){
llequal = true;
}
return llequal;
};
template
bool cLinkedList::searchList(datatype x){
bool finddata = false;+
if(!listEmpty()){
moveCurrentBegin();
while(current != NULL && !finddata){
if(current->datavalue == x){
finddata = true;
}
moveCurrentNext();
}
}
return finddata;+
};
template
int cLinkedList::countnodes(void){
int count = 0;
if(!listEmpty()){
moveCurrentBegin();
count++;
while(current->Next != NULL){
current = current->Next;
count++;
}
}
return count;
};
template
void cLinkedList::removeCurrent(void){
if(current != NULL){
if(current = head){
removeBegin();
current = head;
}else{
llNode *temp;
temp = head;
while( temp->Next != current){
temp = temp->Next;
}
temp->Next = current->Next;
delete current;
current = temp;
}
}
};
template
void cLinkedList::insertCurrent(datatype data){
if(current != NULL){
if(listEmpty()){
addBegin(data);
}else{
llNode *temp;
temp = new llNode;
temp->datavalue = data;
temp->Next = current->Next;
current = temp;
}
}
};
template
bool cLinkedList::currentNull(void){
bool temp = true;
if (current != NULL){
temp = false;
}
return temp;
};
template
datatype cLinkedList::returnCurrent(void){
datatype tempdata;
tempdata = current->datavalue;
return tempdata;
};
template
void cLinkedList::moveCurrentNext(void){
if(!listEmpty() && current != NULL){
current = current->Next;
}
};
template
void cLinkedList::removeEnd(void){
if(!listEmpty()){
moveCurrentBegin();
while(current->Next->Next != NULL){
moveCurrentNext();
}
delete current->Next;
current->Next = NULL;
}
};
template
datatype cLinkedList::returnEnd(void){
if(!listEmpty()){
moveCurrentBegin();
while(current->Next != NULL){
moveCurrentNext();
}
datatype tempdata =returnCurrent();
}
return tempdata;
};
template
void cLinkedList::insertEnd(datatype data){
if(listEmpty()){
addBegin(data);
}else{
llNode *temp;
temp = new llNode;
temp->datavalue = data;
temp->Next = NULL;
moveCurrentBegin();
while(current->Next != NULL){
moveCurrentNext();
}
current->Next = temp;
}
};
template
void cLinkedList::moveCurrentBegin(void){
current = head;
}
//Remove Begin
int _tmain(int argc, _TCHAR* argv[])
{
cLinkedList testList;
if(testList.listEmpty()){
cout<<"The list is empty"< testList.addBegin(6);
if(testList.listEmpty()){
cout<<"The list is empty"< }else{
cout<<"The list is not empty"< if(testList.returnBegin());
cout<<"The list still isn't empty"< testList.removeBegin();
if(testList.listEmpty()){
cout<<"Beginning removed, list empty"< }
}
}
return 0;
}
Explanation / Answer
The templates aren't complete.
template <typename datatype>
class IINode { ... };
Forgot parameters
temp = new llNode();
Current is not good, but your assignment or whatever may require it.
Your + operator should just be an insertEnd call. The loop is going to instantly terminate in the +.
Two lists are equal if list a contains all elements of list b and list b contains all elements of list a. To do this check, compare the node count for each list and then see if list a contains all of list b. This'll be the fastest way ; ).
Syntax error: bool finddata = false;+
Syntax error: return finddata;+
countnodes
while (current != null) { ++count; current = current->next; }
insertCurrent
You forget to do current->Next = temp
currentNull
return current == null;
returnCurrent
return current->datavalue;
moveCurrentNext
if (current != NULL) { current = current->next; }
returnEnd
You declare the variable you are returning in a scope, meaning that it gets lost. Need to declare it outside of the if-statement. NULL will only work on pointers.
Run this through a compiler and first fix all of your syntax errors, lol...