這是我實現計數:錯誤,試圖計算在循環雙向鏈表中的節點遞歸
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;
在運行並試圖查看列表的長度,它崩潰與b記憶力。請幫我解決這個問題,現在我已經研究了很長時間了。後指定的位置
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");
}
你爲什麼要把'l'靜態的?這只是防止函數完全重入,這通常是正確遞歸的要求。 –