Mala pomoc

4

Mala pomoc

offline
  • Pridružio: 04 Sep 2003
  • Poruke: 24135
  • Gde živiš: Wien

Slazem se, prevideo sam nesto, a to je da bi tekuci bio tipa elem trebalo je negde da stoji
elem *tekuci = new elem;


Evo moje implementacije LinkedList, ako nekome zatreba.


LinkedList.h
/********************************************************  ***** LinkedList                             *****  ********************************************************/ #ifndef _LINKEDLIST_H_ #define _LINKEDLIST_H_ template <class T> class LinkedList : public List<T> { public:       struct Node    {           T data;           Node *next;    }; Node* head; // Methods    LinkedList();    LinkedList(T list[]);    virtual ~LinkedList();    bool add(const T value);    bool add(int index, const T value);    T* elementAt(int index);    T* getFirstElement(void);    bool removeFirstElement(void);    bool removeAt(int index);    const int size(void);    const int member(const T value);    bool fill(const T values[]);    T* getValues(void);        void print(void);    char * toString(void); }; #endif //_LINKEDLIST_H_

LinkedList.cpp
/******************************************************** ***** LinkedList                  ***** ********************************************************/ using namespace std; #include <sstream> #include "LinkedList.h" #include <iostream> template LinkedList<int>; template LinkedList<double>; template LinkedList<float>; template LinkedList<char>; // default constructor template <class T>    LinkedList<T>::LinkedList() {    head = NULL; }; // constructor with initial data template <class T>    LinkedList<T>::LinkedList(T list[]) {    head = NULL;    fill(list); }; // default destructor template <class T>    LinkedList<T>::~LinkedList(){    Node* help;    while (head != NULL)    {       help = head;       head = head->next;       delete help;    } }; // adds element at last position // adding successful ==> return true // adding failed ==> return false template <class T> bool LinkedList<T>::add(const T value){    Node* help = new Node;    help->next = head;    help->data = value;    head = help;    if ((T*)head == getFirstElement()) { return true; }    else { return false; } }; // adds element at given index // adding successful ==> return true // adding failed ==> return false template <class T> bool   LinkedList<T>::add(int index, const T value){    Node* help;    Node* help2;    Node* toIns = new Node;    int counter = 1;    help = head;    while (counter < index){       help = help->next;       help2 = help->next;       counter++;    }    toIns->data = value;    toIns->next = help2;    help->next = toIns;    if (elementAt(index) == (T*)toIns) {return true;}    else { return false; };    }; // get element at given index // index out of bounds ==> return null pointer // element at index available ==> return pointer on element template <class T> T*   LinkedList<T>::elementAt(int index){    Node* help;    int counter = 0;    help = head;    while (counter < index){       help = help->next;       counter++;    }    if (help == NULL) { return NULL; }    else { return (T*)help; } }; // get element at first position // list not empty ==> return pointer on first element // list is empty ==> return null pointer template <class T> T*   LinkedList<T>::getFirstElement(void){    if (head == NULL) { return NULL; }    else { return (T*)head; } }; // remove element at first position // list not empty ==> return true // list is empty ==> return false template <class T> bool   LinkedList<T>::removeFirstElement(void){    if (head != NULL) {       Node* help = head;       head=head->next;       delete help;       return true;    }    else { return false;}; }     // remove alement at given index // index out of bounds ==> return false // element at index available & remove successful ==> return true template <class T> bool   LinkedList<T>::removeAt(int index){    Node* help;    Node* help2;    int counter = 1;    bool flag = true;    help = head;    if (help->next != NULL) {       while (counter < index){          if (help->next == NULL) {             flag = false;             break;}          help = help->next;          counter++;       }    }    if (flag != false){       help2 = help->next->next;       delete help->next;       help->next = help2;       return true;    }    else {return false;} };     // returns the current size of the list template <class T> const int   LinkedList<T>::size(void){    int   n = 0;    Node* help;    help = head;    while (help != NULL) {       help = help->next;       ++n;    }    return n; };     // returns the index of the given element if its in the list // returns -1 if the list doesn't contain the given element template <class T> const int   LinkedList<T>::member(const T value){    Node* help = head;    int counter = 0;    while (help != NULL && help->data != value) {       help = help->next;       counter++;    }    if ( help != NULL ) return counter;    else return -1; }; // fill the list with the given values from an array // successful ==> return true // failed ==> return false template <class T> bool   LinkedList<T>::fill(const T values[]){    int counter = 0;    bool flag = false;    while (values[counter]) {       flag = false;       if (add ( values[counter] ) != false) {   // taking value true if add succeed          flag = true;       }       counter++;    }    return flag; }; // returns an array of the values in the list (as a pointer) // list is empty = return null pointer // list not empty = return address on first element template <class T> T*   LinkedList<T>::getValues(void){        int arrSize = size();    int counter = 0;    if (arrSize == 0) {return NULL;}    arrSize++;    T* array = new T[arrSize];    array[--arrSize] = 0;    Node* help= head;    while (help != NULL) {       array[counter] = help->data;       help = help->next;       counter++; }    return array; };     // debugging method to print the content of the // list (human readable!) template <class T> void   LinkedList<T>::print(void){    int counter = 0;    while (elementAt(counter) != NULL) {       cout << "element " << counter << " is " << *elementAt(counter) << "\n";       counter++;    } }; // debugging method to return the content of the // list in string form (return value is a pointer) template <class T> char*   LinkedList<T>::toString(void){        ostringstream outstream;    int counter = 0;    while (elementAt(counter) != NULL)    {       outstream << *elementAt(counter) << " ";       counter++;    };       return (char*)outstream.str().c_str(); };



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Pridružio: 18 Apr 2003
  • Poruke: 5001
  • Gde živiš: Beograd

Pa tako smo i mi radili kod prof. Lasla Krausa samo si kod njega jos morao da uradis i full paket sa preklapanjem operatora...



offline
  • Nom  Male
  • Građanin
  • Pridružio: 17 Nov 2004
  • Poruke: 168
  • Gde živiš: Shanghai, China

@bobby

1. sramota da ti je za ovo trebalo 3 meseca... klasa se pise za jedan sat.
2. nemas pravo da brises postove ukoliko te neko verbalno ne vredja ili vredja nekog drugog, ili ako se otislo offtopic. a kako se ovde raspravlja o zadatku koji je postavljen kao tema, ovo nije offtopic a isto tako nije ni vredjanje vec strucna debata.
3. ako si toliko sujetan, ni ne zasluzujes da budes admin.
4. to sto si polozio nesto na faxu ne znaci da znas. ja zivim od c++-a vec godinama, i firma za koju radim mi je pored povisice svake godine dala i profitni interes firme sto mislim dovoljno govori o mojoj kompetentnosti u ovoj stvari.
5. moja primedba na tvoje objasnjenje zadatka se odnosila na ovo:
    while(tekuci) { ..... }     tekuci->sled = ....;
da li ti uopste kapiras da se while petlja zavrsava kad je tekuci == NULL i da odmah posle toga (znaci kad je tekuci NULL) ti pokusavas da pristupis njegovom clanu??? da li znas da je to klasican lamerski (izraz koji si ti upotrebio) 'assertion failed'???? ako to ne kapiras, cudo da si polozio ispit. to dokazuje samo o losem kvalitetu kontrole znanja kod tebe na faxu.

offline
  • Pridružio: 04 Sep 2003
  • Poruke: 24135
  • Gde živiš: Wien

@Nom
1. Dato nam je 3 meseca, a trebalo mi je dva dana posto mi je ovo prvi C++ kod koji sam u zivotu napisao. Ispit nisam polagao na svom fakultetu gde se uci Java i C#, nego na drugom fakultetu sa kojim imamo saradnju. Cilj predmeta nije programiranje, vec teorija struktura podataka (stack, liste, binarno drvo itd) i teorija algoritama (bubble sort, merge sort, radix...)
2. nisam imao nameru da brisem iz sujete, jer za to nemam prava (moralnog), vec zato sto samo dajete neke podsmevajuce komentare bez argumentacije (osim par zadnjih komentara).
3. nisam sujetan, ali bih voleo da stanes na moje mesto par dana, pa da vidis koliko ima tih koji se podsmevaju, bez da sami nesto znaju (a nisam mogao iz tvojih ranijih postova da zakljucim da li nesto znas)
4. slazem se u potpunosti za prvu recenicu. Za drugu recenicu - nisam mogao ni da pretpostavim iz ranijih postova
5. Obrazlozio sam na pocetku proslog posta gde je moja greska u razmisljanju. Ne kontrolise se znanje C++ iz ovog predmeta, kontrolisala se samo brzina izvrsavanja programa (pojedinih metoda) i uporedjivana je sa ostalim rezultatima od drugih studenata. Kontrolisalo se takodje da li smo uspesno kompajlirali Shared Object fajl (SunOS, GCC)

No hard feelings, i izvinjavam se svima

offline
  • Nom  Male
  • Građanin
  • Pridružio: 17 Nov 2004
  • Poruke: 168
  • Gde živiš: Shanghai, China

@bobby

sto se mene tice, no hard feelings either Wink kao sto rekoh, ja ovo prihvatam samo kao debatu, nista licno ili provokativno. ako sam bio grub iz tvog ugla gledano, evo, ja se tebi izvinjavam.

@svima

kad pogledate, pitanje je bilo jednostavno a odgovora vise nego sto treba... mislim da topic 'ladno moze da se zakljuca...

Ko je trenutno na forumu
 

Ukupno su 882 korisnika na forumu :: 39 registrovanih, 5 sakrivenih i 838 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: A.R.Chafee.Jr., aleksmajstor, Atomski čoban, Ben Roj, bestguarder, Brana01, cenejac111, crnitrn, cuculo, darkojbn, dolinalima, DragoslavS, galijot, gasha, jackreacher011011, Još malo pa deda, Kibice, Leonov, mercedesamg, moldway, naki011, nenad81, nenooo, NikolaGTR, ozzy, procesor, rovac, Skywhaler, Srle993, styg, uruk, vladetije, vobo, x9, yrraf, yufighter, zeo, Zerajic, 79693