c++ - 如何交换链表 C++ 的索引

标签 c++ linked-list

<分区>

我有输出:

node 1: Bob Joe Jill Jeff Jill

但我希望它在重复的名字被发送到单向链表的前面,所以它会变成

node 1: Jill Bob Joe Jeff

而且我无法实现它。

这是我的代码:

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++;

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

最佳答案

  1. 总是写小函数
  2. 如果一个函数看起来更大,总是分解成更小的函数
  3. 尽量避免使用全局数据,必要时尽量传递全局值而不是直接对其进行操作

这是您的解决方案。我添加了一个查找功能并更正了指针管理。

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;

};

现在有些人可能会告诉您使用 STL 中的列表 n 永远不要编写自己的代码,因为您无法击败 STL,但对我来说,实现自己的代码以清楚地了解它在其中的工作原理是件好事现实。

关于c++ - 如何交换链表 C++ 的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18938845/

相关文章:

python - 力扣 : Problem 23 - Merge K Sorted Lists

c - Linux内核中的链表卡住机器

c++ - 由于 undefined reference 而无法编译?

c++ - 删除 *char[]

C++ Do-While 循环不接受多次输入

c++ - 尝试制作我自己的字符串类

c++ - 使用模板将堆栈实现为链表时出现 "symbols not found"错误

c++ - Qt:创建一个 QDoubleSlider

java - 切换链表中的值(处理节点)

c - N个节点的完全二叉树的高度是多少?