Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将索引交换为链表C++;_C++_Linked List - Fatal编程技术网

C++ 如何将索引交换为链表C++;

C++ 如何将索引交换为链表C++;,c++,linked-list,C++,Linked List,我有以下建议: 节点1:鲍勃·乔·吉尔杰夫·吉尔 但是我想知道,如果一个名字重复,被发送到单链表的前面,那么它就会变成 节点1:吉尔·鲍勃·乔·杰夫 我很难做到这一点 这是我的密码: string employers[] = {"Jill", "Jeff", "Bob", "Joe", "Monica", "Luis"}; struct node { node(string name="") {data=name; next=NULL; } string data;

我有以下建议:

节点1:鲍勃·乔·吉尔杰夫·吉尔

但是我想知道,如果一个名字重复,被发送到单链表的前面,那么它就会变成

节点1:吉尔·鲍勃·乔·杰夫

我很难做到这一点

这是我的密码:

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++;
string雇主[]={“吉尔”、“杰夫”、“鲍勃”、“乔”、“莫妮卡”、“路易斯”};
结构节点{
节点(字符串名称=){data=name;next=NULL;}
字符串数据;
节点*下一步;
节点*prev;
};
班级名单{
公众:
列表(int N=0,int值=0);
~list();
无效认沽权(int);
friend ostream&接线员(下一步){
pp=pp->next;
for(int b=6;b data==名称[b]
cout-next=p;
N++;
}

我遇到的困难是,如何比较链接列表中的每个条目?我制作了一个node*prev,但我不完全确定如何比较这些节点

  • 始终编写小函数
  • 如果一个函数看起来更大,总是分解成更小的函数
  • 尽量避免使用全局数据,如有必要,尽量传递全局值,而不是直接对其进行操作
  • 这是您的解决方案。我添加了一个查找函数并更正了指针管理

    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;
    
    };
    
    类列表{
    公众:
    list():head(NULL),N(0){
    ~list(){
    //清理的实现
    }
    voidput(inti){//离开了这个函数,这样你的代码就不会中断,但是试着删除它
    填写(员工姓名[i]);
    }
    void put(字符串名){//使用传递的值,而不是访问全局数据
    节点*p=新节点(名称);
    p->next=p->prev=NULL;
    节点*pp=find(名称);
    if(pp==NULL){
    //未找到匹配项,请附加到后面
    if(head==NULL)
    head=p;//列表为空,添加第一个元素
    否则{
    节点*cur=头部;
    while(cur->next!=NULL)//继续查找,直到找到插槽
    cur=cur->next;
    cur->next=p;
    p->prev=cur;
    }
    }
    否则{
    //找到匹配项,将其从其位置分离
    节点*pPrev=pp->prev;
    pPrev->next=pp->next;
    pp->next->prev=pPrev;
    p->next=head;//将其附加到前面并调整指针
    头->上一个=p;
    }
    N++;
    }
    //MER:查找匹配的元素并返回节点,否则返回NULL
    节点*查找(字符串名称){
    节点*cur=头部;
    if(cur==NULL)//是否为空列表?
    返回NULL;
    否则如果(cur->data==head)//第一个元素是否相同?
    回流头;
    else//继续查找,直到列表结束
    while(cur->next!=NULL){
    如果(当前->数据==名称)
    返回电流;
    cur=cur->next;
    }
    返回NULL;
    }
    
    朋友ostream&operator如果不是学校作业,我会这样做

    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");
        }
    } 
    
    class EmployeerCollection
    {    
    公众:
    布尔加法器(常量标准::字符串和名称)
    {
    EmployerList::const_迭代器it=std::find(m_employers.begin(),m_employers.end(),name);
    if(it!=m_employers.end())//已存在于列表中。
    {
    拼接(m_employers.begin(),m_employers,it,std::next(it));
    返回true;
    }
    m_雇主。推_前线(姓名);
    返回false;
    }
    私人:
    typedef std::列出员工列表;
    雇主主义m_雇主;
    };
    int main()
    {
    const int NUM_雇主=15;
    字符串雇主[NUM_雇主]={“吉尔”、“杰夫”、“吉尔”};
    雇主集合c;
    
    对于(int i=0;iTrasverse the list,并使用比较运算符(
    operator==
    )来比较节点字符串。到底是什么问题?我怎么知道例如Jill已经添加了?我对编码是新手。我很抱歉:/sorry关于我的评论:您可以使用比较运算符检查两件事(本例中的字符串)是相等的。例如:
    if(one_string==other_string)std::你能签出我的最新编辑吗?我试过这样做,但我认为比较它们不正确。我不确定如何将新条目与函数中当前节点中的所有内容进行比较。
    b=6;b