c - 如何实现与push相反的功能?

标签 c list singly-linked-list

我正在尝试实现一个与 push 相反的函数:它检索 值存储在链表的头节点中,然后从链表中删除该节点。

参数head指向链表中的第一个节点。

我试图让函数将列表头节点中的值复制到参数 popped_value 指向的位置,然后取消头节点与列表的链接,并返回指向修改列表中第一个节点的指针。

这是我目前的代码。我真的坚持这一点,非常感谢任何帮助。谢谢。

typedef struct intnode {
  int value;
  struct intnode *next;
  } intnode_t;


intnode_t *pop(intnode_t *head, int *popped_value) {

assert(head!=NULL);

head = head->next;

popped_value=&head->value;


free(head);

return head;

}

最佳答案

你展示的节目好像是'shift',真正的'pop'在'shift'下面描述。

Shift:要返回指向下一项(不是当前头部)的指针,将popped_value设置为当前头部值

intnode_t *shift(intnode_t *head, int *popped_value) {
   assert(head!=NULL);
   // get next pointer here, since 'head' cannot be used after it's been freed
   intnode_t *next = head->next;
   // sets the int variable which pointer is given as argument to
   // the current head value
   *popped_value = head->value; 
   // you can now free head without worries
   free(head);
   // and return the next element (becoming the new head)
   return next;
}

例如,被称为

int myvalue;
intnode_t *newhead = shift(head, &myvalue);

请注意,此操作通常命名为 shift,因为您从列表中获取第一个项目值,然后删除该元素。 pop 通常是您获取(将 popped_value 设置为)最后 项值,然后删除最后一个元素。

pop 会是这样的

intnode_t *pop(intnode_t *head, int *popped_value) {
   assert(head!=NULL);
   intnode_t *last,*previous;
   // get last and last's previous element
   for(previous=NULL,last=head ; last->next ; last=last->next) previous=last;
   // get the last value
   *popped_value = last->value; 
   // free last element
   free(last);
   // If at least two elements, tell the previous one there is no more 
   if (previous) previous->next = NULL; // previous is last now
   // return the head or NULL if there no more element
   // (previous is NULL if there was only one element, initially)
   return previous ? head : NULL;
}

该算法假定最后一个元素的 next 指针设置为 NULL。如果列表只有一个元素,则返回值将再次为 head(指向列表)或 NULL 以告诉调用者列表没有更多元素存在。

你这样称呼'pop'

int myvalue;
// head had to be declared and initialized before
head = pop(head, &myvalue);
if ( ! head) { // no more element
   break; // for instance, depending on your program
}

既然你是一名学生,这里有一个递归版本的pop,它做同样的事情

intnode_t *recpop(intnode_t *this, int *popped_value) {
    if (this->next) {
        // this is not the last element
        intnode_t *next = recpop(this->next, popped_value);
        // next element was the last
        if ( ! next) this->next = NULL;
    }
    else {
        // this is the last element
        *popped_value = this->value;
        free(this);
        this = NULL;
    }
    return this;
}

供您学习。

关于c - 如何实现与push相反的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383735/

相关文章:

python - 在一行迭代中扩展多个元素

c - 我无法在链表中使用链表

c - 使用 C 的链表段错误

c - strerror_r 应该允许多大的尺寸?

python - 从python中的2个列表中删除不匹配的项目

c# - 平均分配资源列表

java - 反向打印链表的元素

c - 如何以不同的方式调用递归?

c - 如何在简单的游戏(Windows库)中使一些角色自动移动而无需sleep()

c - 如果我们不将缓冲区声明为 C 中的全局变量,为什么会发生堆栈溢出?