尝试以递归方式计算循环双向链表中的节点时出错

时间:2011-10-28 02:40:17

标签: c linked-list

这是我对count的实现:

int count(node *start)
{
    static int l ;
    node *current;            /* Node for travelling the linked list*/
    current=start;
    if(current->next!=start)
    {
        l = 1 + count ( current->next ) ;
        return ( l ) ;
    }


    else
    {
        return(1);
    }
}

这是我称之为主要功能的片段:

void main()
{
    node *head;
printf ( "Length of linked list = %d", count ( head ) ) ;
}

这是结构:

struct cirdoublelinklist
{
    struct cirdoublelinklist *prev;  /** Stores address of previous node **/
    int value;                   /** stores value **/
    struct cirdoublelinklist *next;  /** stores address of next node **/
};

/** Redefining list as node **/
  typedef struct cirdoublelinklist node;

在运行并尝试查看列表的长度时,它会因超出内存而崩溃。请帮助我,我已经做了很长时间了。

添加第一个节点的方法:

void initialize(node *start)
{
    start->prev=start;
    printf("\nEnter Value\n");
    scanf("%d",&start->value);
    start->next=start;
}

在指定位置后添加后续节点的方法:

void insert_after(node *start)
{
    int num;                  /* value for inserting a node */
    int flag=0;
    node *newnode;            /* New inputed node*/
    node *current;            /* Node for travelling the linked list*/
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the value after which you want to insert a node\n");
    scanf("%d",&num);
    init(newnode);
    current=start;
    while(current->next!=start)
    {

        if(current->value==num)
        {
            newnode->next=current->next;
            current->next->prev=newnode;
            current->next=newnode;
            newnode->prev=current;
            flag=1;
        }
        current=current->next;
    }
    if(flag==0 && current->next==start && current->value==num)
    {
        /***  Insertion checking for last node  ***/
        newnode->next=current->next;     /* Start is being copied */
        current->next->prev=newnode;
        current->next=newnode;
        newnode->prev=current;
        flag=1;
    }
    if(flag==0 && current->next==NULL)
        printf("\nNo match found\n");
} 

5 个答案:

答案 0 :(得分:1)

好吧,问题是你在一个NULL指针上调用main中的函数。声明了事实node *head;但从未分配给某事物。所以当你执行这一行时:

if(current->next!=start)

程序崩溃,因为它会检查显然不存在的NULL->next

答案 1 :(得分:1)

您需要在insert_after函数

中传递指针以启动指针
void insert_after(node **start)

而不是

void insert_after(node *start)

否则你只是更新* start的本地副本。

同样适用于初始化

void initialize(node **start)

答案 2 :(得分:1)

每次拨打count时,它都会有一个新的start,因此current->next!=start始终将节点与其后继节点进行比较,只有在列表长度为1时才会结束。你最想做的是有两个功能:

int count(node *start)
{
    if(start == NULL)
        return 0;
    return count_helper(start, start);
}

int count_helper(node *start, node *current)
{
    static int l;
    if(current->next!=start)
    {
        l = 1 + count (start, current->next);
        return ( l ) ;
    }
    else
    {
        return(1);
    }
}

正如其他人所提到的,静态变量不是必需的。编写我称之为count_helper的更好方法是:

int count_helper(node *start, node *current)
{
    if(current->next!=start)
    {
        return 1 + count (start, current->next);
    }
    else
    {
        return 1;
    }
}

最后,更有效的实现将是非递归的:

int count(node *start)
{
    if(start == NULL)
        return 0;
    node *current = start->next;
    int c = 1;
    while(current != start)
    {
        c++;
        current = current->next;
    }
    return c;
}

答案 3 :(得分:0)

简单地说,递归调用不知道原始的起始节点。您需要添加第二个node*参数并通过它传递起始节点。

答案 4 :(得分:0)

这是不使用静态或辅助变量的递归解决方案:

int count(Node* head) {
  // Base cases:
  // 0 nodes
  if (!head)
    return 0;
  // 1 node
  if (head->next == next)
    return 1;
  // Keep a pointer to the node to be removed
  Node* rest = head->next;
  // Remove the node
  head->next = head->next->next;
  // Get the length of the new list
  int result = 1 + count(head->next);
  // Reconnect the node
  head->next = rest;
  return result;
}