使用下面的代码我得到了“SortedLL的重新定义”错误。 所以我从下面的头文件中删除了“class SortedLL”。但是当我从头文件中删除SortedLL时,我无法在main.cpp文件中声明SortedLL。
所以我被卡住了。我错过了什么?
已排序的链接列表头文件
#ifndef SortedLinkedList_SortedLL_h
#define SortedLinkedList_SortedLL_h
struct Node {
int data;
struct Node * next;
};
class SortedLL {
public:
int find(int data);
bool remove(int data);
int size();
bool removeOdd();
bool add(int data);
private:
struct Node * pHead;
int length;
};
#endif
已排序的链接列表CPP文件
#include <iostream>
#include "SortedLL.h"
class SortedLL {
private:
// searches the linked list linearly
int findLinear(int data, struct Node ** pPrev, struct Node ** pCurr){
// start at head of list
*pCurr = pHead;
*pPrev = NULL;
int index = 0;
while (*pCurr != NULL){
if ((*pCurr)->data == data){
// found
return index;
}
else {
*pPrev = *pCurr;
*pCurr = (*pCurr)->next;
index++;
}
}
return -1;
}
bool findInsertPointLinear(struct Node ** pPrev, struct Node ** pCurr, int data){
while (*pCurr != NULL){
// if current data is less then new data
if ((*pCurr)->data < data){
// store previous node
*pPrev = *pCurr;
// advance to next position
*pCurr = (*pCurr)->next;
}
// current value is >= new val
else {
// we found insertion point
break;
}
}
return true;
}
// returns the value at the index or NULL if no value exists
bool get(int index, int * val) {
// check if out of bounds
if (index < 0 || index > (length-1)) {
// out of bounds
return false;
}
struct Node * pCurr = pHead;
for (int i=0;i<=index;i++){
pCurr = pCurr->next;
}
if (val != NULL) {
*val = pCurr->data;
}
return true;
}
public:
bool add (int data) {
struct Node * pCurr = pHead;
struct Node * pPrev = NULL;
bool returnVal = true;
// if the list is empty
if (pHead == NULL){
// add the new node
pHead = (struct Node *)malloc(sizeof(struct Node));
pHead->data = data;
pHead->next = NULL;
// return success
length++;
return returnVal;
}
// the list is not empty, so find insertion point
if (findInsertPointLinear(&pPrev, &pCurr, data)){
// if inserting at the begininng of list
if (pPrev == NULL){
pPrev = (struct Node *)malloc(sizeof(struct Node));
if (pPrev != NULL) {
pPrev->data = data;
pPrev->next = pCurr;
// keep track of head pointer
pHead = pPrev;
}
else {
// failed to allocate memory
returnVal = false;
}
}
// if inserting at end of list
else if (pCurr == NULL && pPrev !=NULL) {
pCurr = (struct Node *)malloc(sizeof(struct Node));
if (pCurr != NULL){
pCurr->data = data;
pPrev->next = pCurr;
pCurr->next = NULL;
}
else {
// failure to allocate memory
returnVal = false;
}
}
// if inserting in the middle of list
else {
pPrev->next = (struct Node *) malloc (sizeof(struct Node));
if (pPrev->next != NULL){
pPrev->next->data = data;
pPrev->next->next = pCurr;
}
else {
// failure to allocate memory
returnVal = false;
}
}
}
// increment size of list by 1
if (returnVal == true)
length++;
return returnVal;
}
// returns index of first occurrence of data or -1 if not found
int find(int data){
struct Node * pPrev = NULL;
struct Node * pCurr = NULL;
return findLinear(data, &pPrev, &pCurr);
}
bool remove(int data) {
// try to find data
struct Node * pPrev = NULL;
struct Node * pCurr = NULL;
int removeIndex = findLinear(data, &pPrev, &pCurr);
bool returnVal = false;
// if found
if (removeIndex != -1){
// remove node
return false;
}
// if removing at the head
if (removeIndex == 0){
if (pCurr != NULL){
pHead = pCurr->next;
free(pCurr);
pCurr = NULL;
returnVal = true;
}
else {
printf("trying to remove an element that does not exist\n");
}
}
// if removing at the end
else if (removeIndex == (length-1)){
if (pCurr != NULL) {
free(pCurr);
pCurr = NULL;
if (pPrev != NULL){
pPrev->next = NULL;
}
returnVal = true;
}
else {
printf("trying to remove an element that does not exist\n");
}
}
// removing somewhere in the middle
else {
if (pPrev != NULL && pCurr != NULL){
struct Node * temp = pCurr->next;
free(pCurr);
pCurr = NULL;
pPrev->next = temp;
returnVal = true;
}
// something is wrong
else {
printf("attempting to remove an element from middle but either previous or curr element is NULL\n");
}
}
return returnVal;
}
int size(){
return length;
}
bool removeOdd(){
struct Node * pCurr = pHead;
struct Node * pPrev = NULL;
// iterate over list
while (pCurr != NULL){
// if current data element is odd
if (pCurr->data %2 != 0){
// remove it
// removing at head
if (pCurr == pHead){
pHead = pHead->next;
free(pCurr);
pCurr = pHead;
}
// if removing at the end
else if (pCurr->next == NULL){
free(pCurr);
pCurr = NULL;
pPrev->next = NULL;
}
// removing somewhere in the middle
else {
struct Node * temp = pCurr->next;
free(pCurr);
pPrev->next = temp;
// advance pCurr to next node
pCurr = temp;
}
}
// if current data element even
else {
pPrev = pCurr;
pCurr = pCurr->next;
}
}
return true;
}
}SortedLL;
答案 0 :(得分:3)
正如@ PaulMcKenzie指出的那样,您已经在标题和.cpp文件中定义了SortedLL类。你想要的.cpp只是方法定义。类似的东西:
int SortedLL ::findLinear(int data, struct Node ** pPrev, struct Node ** pCurr) {
...
}