How to swap index for a linked list C++ [duplicate]

感情迁移 提交于 2019-12-11 09:29:10

问题


I have the out put:

node 1: Bob Joe Jill Jeff Jill

but I want it to where if a name repeats that gets sent to the front of the singly linked list, so it would become

node 1: Jill Bob Joe Jeff

And I'm having trouble being able to implement that.

Here's my code:

string employers[] = {"Jill", "Jeff", "Bob", "Joe", "Monica", "Luis"}; 

struct node {
    node(string name="") {data=name; next=NULL; }

    string data;

    node *next;
    node *prev;
};


class list {
public:
    list(int N=0, int value=0);
    ~list();

    void put(int);
    friend ostream & operator << (ostream &, const list &);

private:
    int N;
    node *head;

};



void list::put(int i) {
    string employee_name = employers[i];
    node * p = new node(g);
    node * pp = head;

    while (pp - > next) {


        pp = pp - > next;
        for (int b=6; b<6; b++) {
           if (p-> data == names[b]
             cout << "found";
    }

    pp - > next = p;

    N++;

}

The difficulty I'm having is, how would I be able to compare each one of my entries in my linked list? I made a node *prev, but I'm not entirely sure how I can compare the nodes.


回答1:


  1. Always write small functions
  2. If a function seems bigger always break into smaller functions
  3. Try to avoid global data, if necessary, try to pass the global values rather than operating on them directly

Here's your solution. I've added a find function and corrected the pointer management.

class list {
public:
    list():head(NULL), N(0){}
    ~list(){
    //Implementation for cleanup
     }

void put(int i){ //left this function so that your code wont break but try removing it
  put(employee_names[i]);
}

void put(string name){  //rather than accessing the global data, use the value passed
    node* p = new node(name);
    p->next=p->prev=NULL;
    node* pp = find(name);
    if(pp==NULL){
      // No match found, append to rear
      if(head==NULL)
        head=p;  //list empty, add first element
      else{
        node* cur=head;
        while(cur->next!=NULL) //Keep looking until a slot is found
          cur=cur->next;
        cur->next=p;
        p->prev=cur;
      }
    }
    else{
        //Match found, detach it from its location
        node* pPrev = pp->prev;
        pPrev->next = pp->next;
        pp->next->prev=pPrev;
        p->next = head; //append it to the front & adjust pointers
        head->prev=p;
    }
    N++;
    }

    //MER: finds a matching element and returns the node otherwise returns NULL
    node* find(string name){
        node *cur=head;
        if(cur==NULL) // is it a blank list?
          return NULL;
        else if(cur->data==head) //is first element the same?
          return head;
        else   // Keep looking until the list ends
          while(cur->next!=NULL){
          if(cur->data==name)
            return cur;
            cur=cur->next;
          }
        return NULL;
}
friend ostream& operator << (ostream& os, const list& mylist);

private:
    int N;
    node *head;

};

Now some may tell you to use the list in STL n never to write your own code coz you can't beat STL, but to me it's good that you are implementing your own to get a clear idea on how it works in reality.




回答2:


Here's how I'd do it if it wasn't a school assignment.

class EmployerCollection
{    
public:
    bool AddEmployer(const std::string& name)
    {
        EmployerList::const_iterator it = std::find(m_employers.begin(), m_employers.end(), name);
        if (it != m_employers.end()) // Already exists in list.
        {
            m_employers.splice(m_employers.begin(), m_employers, it, std::next(it));
            return true;
        }
        m_employers.push_front(name);
        return false;
    }

private:
    typedef std::list<std::string> EmployerList;
    EmployerList m_employers;
};

int main()
{
    const int NUM_EMPLOYERS = 15;
    std::string employers[NUM_EMPLOYERS] = {"Jill", "Jeff", "Jill"};
    EmployerCollection c;

    for (int i=0; i<NUM_EMPLOYERS; i++)
    {
        bool duplicate = c.AddEmployer(employers[i]);
        printf("Added %s to employer list - duplicate: %s \n", employers[i].c_str(), duplicate ? "True" : "False");
    }
} 


来源:https://stackoverflow.com/questions/18938845/how-to-swap-index-for-a-linked-list-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!