אבל מקווה שהתוכנית הבאה שכתבתי תענה על השאלות שלך:
#include <iostream> #include <string> using namespace std;#define LIST_ENTRY(ptr, member) \ ((Sorted*)((char *)(ptr)-(unsigned long)(&((Sorted*)0)->member))) typedef struct Head { struct Head *next, *prev; } LL; inline void __add(LL *head, LL *elm) { elm->next = head->next; head->next->prev = elm; head->next = elm; elm->prev = head; } inline void addNext(LL *head, LL *next) { __add(head, next); } inline void addPrev(LL *head, LL *prev) { __add(head->prev, prev); } class Sorted { public: Sorted(string str) : mStr(str) { mSorted.next = mSorted.prev = &mSorted; mUnSorted.next = mUnSorted.prev = &mUnSorted; } virtual ~Sorted() {} void add(string strIn) { addPrev(&mUnSorted, &(new Sorted(strIn))->mUnSorted); for (LL *next = mSorted.next; next->next != mSorted.next; next = next->next) if (LIST_ENTRY(next, mSorted)->mStr >= strIn) { addPrev(next, &(new Sorted(strIn))->mSorted); return; } addPrev(&mSorted, &(new Sorted(strIn))->mSorted); } void printForEach() { cout << " -------------- SORTED -------------- \n"; for (LL *next = mSorted.next; next->next != mSorted.next; next = next->next) cout << LIST_ENTRY(next, mSorted)->mStr << endl; cout << " ------------- UNSORTED ------------- \n"; for (LL *next = mUnSorted.next; next->next != mUnSorted.next; next = next->next) cout << LIST_ENTRY(next, mUnSorted)->mStr << endl; } LL mSorted; LL mUnSorted; string mStr; }; int main() { Sorted S("dummy"); S.add("1"); S.add("3"); S.add("2"); S.printForEach(); return 0; }
[\code]בצלחה
|
בברכה,
עידן