עבור לתוכן

עזרה ב++C, מימוש רשימה מקושרת עם TEMPLATE

Featured Replies

פורסם

אז ככה, מימשתי רשימה מקושרת :


template <class T> class List
{
private:
template <class T> class ListNode
{
private:
T object;
ListNode<T> *nextNode;

public:
T &get() { return object; };
void set(T &object) { this->object = object; };

ListNode<T> *getNext() { return nextNode; };
void setNext(ListNode<T> *nextNode) { this->nextNode = nextNode; };
};

int size;
ListNode<T> *headNode;
ListNode<T> *currentNode, *lastCurrentNode;

public:
// Constructor
List()
{
headNode = new ListNode<T>;
headNode->setNext(NULL);

currentNode = NULL;
size = 0;
};
// Copy Constructor
List(const List& list)
{
size=list.size;
headNode= new ListNode<T>;
headNode->setNext(NULL);
currentNode = NULL;
}
// Destructor
~List()
{
ListNode<T> *pointerToDelete, *pointer = headNode;

while (pointer != NULL) {
pointerToDelete = pointer;
pointer = pointer->getNext();
delete pointerToDelete;
}
};

T &get()
{
if (currentNode == NULL)
start();

return currentNode->get();
};

void add(T &addObject)
{
ListNode<T> *newNode = new ListNode<T>;

newNode->set(addObject);

newNode->setNext(headNode->getNext());
headNode->setNext(newNode);

size++;
};

void insert(T &addObject)
{
ListNode<T> *tempNode;
ListNode<T> *newNode = new ListNode<T>;

tempNode=currentNode->getNext();
currentNode->set(addObject);

newNode->set(addObject);

newNode->setNext(tempNode);


size++;
};


void remove()
{
lastCurrentNode->setNext(currentNode->getNext());

delete currentNode;

currentNode = lastCurrentNode;

size--;
};

void start()
{
lastCurrentNode = headNode;
currentNode = headNode;
};

bool next()
{

// If the currentNode now points at nothing, we've reached the end
if (currentNode == NULL)
return false;

// Update the last node and current node
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();

// If currentNode points at nothing or there is nothing added, we can immediately return false
if (currentNode == NULL || size == 0)
return false;
else
return true;
};

int getSize() { return size; };

};

ואני חייב עזרה עם הפונקציה INSERT שאמורה להכניס "תא" באמצע הרשימה... הבעייה עם המימוש שהפונקציה ADD מוסיפה תמיד בראש הרשימה ומשום מה אני מתבחבש עם זה, ולא מוצא את הבעייה...

עזרה תתקבל בברכה..

פורסם

נראה לי שב- insert אתה צריך להחליף את השורה

currentNode->set(addObject);

ב-

currentNode->setNext(newNode)

חוצמזה (יותר קשור לתכנון שלך), זה לא כ"כ בריא שלטיפוס הנתונים שלך יש "מצב" (שהוא ה- currentNode), עדיף להשתמש ב- iterator כדי לעבור עליו.

פורסם
  • מחבר

לא עובד... עוד מישהו?

פורסם

תנסה:



newNode->set(addObject);

newNode->setNext(currentNode->getNext());

currentNode->setNext(newNode);

פורסם
  • מחבר

תודה, גם את זה ניסיתי...

הכל עובר קומפיליציה אבל תמיד נופל בזמן ריצה

פורסם

תבדוק מתי הוא נופל לך בדיבאג.

פורסם

כמו שנאמר, הדרך הכי טובה במקרים כאלו, זה לעשות תוכנית בדיקה ממש פשוטה, שבד"כ עובדת על קלט הכי קטן שאפשר (או אם זה עובד, להגדיל עד שרואים את הבעיה), ואז לעשות DEBUG באזורים שנראים לך בעייתים (עבודה עם פונטרים לרוב).

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים