简体   繁体   English

循环双链表结束功能

[英]Circular Doubly Linked List End Function

In the bool end() function will the program know whether the sentinel is the beginning or end? 在bool end()函数中,程序将知道前哨是开始还是结束? Is there a check I can make to make sure it's reading the sentinel as the end? 我是否可以进行检查以确保其最终读取了前哨?

#include "ring.h"
#include <stdlib.h>
#include <stdio.h>

struct node {
  int item;
  struct node *prev;
  struct node *next;
};
typedef struct node node;

struct ring {
  node *sentinel;
  node *current;
};

ring *new_ring() {
  ring *p;
  node *n;

  p = (ring *) malloc (sizeof(ring));
  n = malloc(sizeof(node));
  p->sentinel = n;
  n->next = n;
  n->prev = n;
  return p;
}

void start(ring *r) {
  r->current = r->sentinel;
}

bool end(ring *r) {
  return r->current == r->sentinel;
}

void forward(ring *r) {
  while (r->current != r->sentinel) {
     r->current = r->current->next;
  }
}

Your specifications are: 您的规格是:

  • start positions before first element of ring (ok) 环的第一个元素之前的开始位置(确定)
  • end tests if current is past last element (ok) 结束测试电流是否超过最后一个元素(确定)
  • forward steps one element on the ring (ok, unless that after calling start() , you have current == sentinel , so forward does nothing!) 前进一个环上的一个元素(好吧,除非调用start() ,您有current == sentinel ,所以forward什么都不做!)

But you should answer this question: do you need to implement the symetric functions? 但是您应该回答这个问题:是否需要实现对称功能?

If the answer is no, just change start to position on sentinel->next , ie on first element of ring if it exists and past the end if the ring is empty, and your are done. 如果答案是否定的,则只需更改sentinel->next上的开始位置,即如果ring的第一个元素存在(如果存在),然后更改到end(如果环为空),就可以了。

If the answer is yes, then you have to be able to distinguish before first on one hand, and past end on the other. 如果答案是肯定的,那么你必须要能够前未区分一方面,和过去的另一

There are 2 simple ways for that: 有两种简单的方法:

  • use a boolean to distinguish: 使用布尔值来区分:

     struct ring { node *sentinel; node *current; bool end; }; 

    you just set end to true when you position directly past the end of when forward (or any other read function) goes past the end, and to true in the opposite cases. 您只需设置end ,以true当你直接过去时的结束位置forward (或任何其它读取功能)进入过去的结束,并真正在相反的情况。

  • use 2 sentinels 使用2个前哨

     struct ring { node *before; node *after; node *current; }; 

    You should initialize them with before->next = after; 您应该使用before->next = after;来初始化它们before->next = after; and after->prev = before after->prev = before

As far as I understood, you need your end function to determine whether this is your last node of the ring or is this the beginning. 据我了解,您需要使用end函数来确定这是环的最后一个节点还是起点。

You may modify your end function to look something like this. 您可以修改您的end函数,使其看起来像这样。

bool end(ring *r) {
    return r->current->next == r->sentinel;
}

If there is only one element in the ring, then always after your start function, the end condition will be true. 如果环中只有一个元素,则始终在启动函数之后,终止条件为true。

If however there are more than one elements then once end returns true, r->current will be the last element before sentinel. 但是,如果有多个元素,那么一旦end返回true,r-> current将成为前哨之前的最后一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM