使用引用无效的遍历链接列表

时间:2017-10-16 11:55:38

标签: c++ c++11

我正在尝试创建一个队列列表,并且我正在传递链接列表的引用,但它无法正常工作并给出错误。

  

在函数' void insertDataToQueue(Node **,int)':    请求会员' next'在' * temp'中,它是指针类型' Node * {aka node *}' (也许你打算使用' - >'?)|

 void insertDataToQueue(Node **queueList, int burstTime){
    Node *newNode = new Node;
    newNode->burstTime = burstTime;
    newNode->next = NULL;

if(queueList == NULL){
    *queueList = newNode;
}
else{
    Node **temp = queueList;
    while(*temp != NULL)
        temp = *temp->next;
}
}

2 个答案:

答案 0 :(得分:2)

要迭代整个列表,只需一个指向public class ChatHeadFrameLayout extends FrameLayout { /** * Store the initial touch down x coordinate */ private float initialTouchX; /** * Store the initial touch down y coordinate */ private float initialTouchY; public ChatHeadFrameLayout(@NonNull Context context) { super(context); } public ChatHeadFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public ChatHeadFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); } @RequiresApi(21) public ChatHeadFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { // If the event is a move, the ViewGroup needs to handle it and return // the indication here switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //remember the initial position initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); return true; } return false; } @Override public boolean onTouchEvent(MotionEvent event) { // Will be called in case we decide to intercept the event // Handle the view move-with-touch behavior here switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //remember the initial position initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); return true; case MotionEvent.ACTION_UP: int Xdiff = (int) (event.getRawX() - initialTouchX); int Ydiff = (int) (event.getRawY() - initialTouchY); //The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking. //So that is click event. if (Xdiff < 10 && Ydiff < 10) { return false; } return true; case MotionEvent.ACTION_MOVE: //Calculate the X and Y coordinates of the view. setX(event.getRawX() - initialTouchX); setY(event.getRawY() - initialTouchY); //Update the layout with new X & Y coordinate invalidate(); return true; } return false; } } 的简单指针即可:

Node

现在,没有任何链接,我想(我希望)仅用于学习方面。因为C ++就像你需要的每个容器一样。例如,您有void insertDataToQueue(Node **queueList, int burstTime){ Node *newNode = new Node; newNode->burstTime = burstTime; newNode->next = NULL; if(queueList == NULL) { *queueList = newNode; } else { Node *temp = *queueList; // Find last element, ie element who "next" field is NULL while(temp->next != NULL) { temp = temp->next; } // Make last element to point to new element temp->next = newNode; } } std::list链接列表。对于生产代码,更喜欢使用它而不是自己开发。

答案 1 :(得分:0)

这个

*temp->next;

被解析为

*(temp->next); 

因为-> has higher precedence than *,如果你想首先取消引用然后访问该成员,你可以使用括号:

(*temp)->next;