我如何解决以下代码中的逻辑错误

时间:2019-11-17 11:04:57

标签: c

我在以下代码中遇到问题

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


struct linked
{
    int val;
    struct linked *before;
    struct linked *next;
};

void get(struct linked *var);
void printforward(struct linked *var);
void printbackward(struct linked *var);



int main()
{
    struct linked *ptr,*temp;
    ptr=(struct linked*)malloc(sizeof(struct linked));
    if(ptr==NULL)
    {
        printf("NOT ENOUGH MEMOREY");
        exit(2);
    }
    ptr->before=NULL;
    get(ptr);
    printf("\nForward:\n");
    printforward(ptr);
    for(temp=ptr;temp->next;temp=temp->next)
    {
        temp->next->before=(struct linked*)temp;
    }
    printf("\nBackward:\n");
    printbackward(temp->before);
 }


void get(struct linked *var)
 {
    printf("Enter the number (-99) to quit:");
    scanf("%d",&var->val);
    if(var->val==-99)
    {
         var->next=NULL;
         return;
     }
   else
   {
        var->next=(struct linked*)malloc(sizeof(struct linked));
        if(var->next==NULL)
        {
            printf("NOT ENOUGH MEMOREY");
            exit(2);
         }
         get(var->next);
   }
}

void printforward(struct linked *var)
{
    if(var->next==NULL)
   {
       return;
   }
   else
    {
        printf("\n%d",var->val);
        printforward(var->next);
    }
}

 void printbackward(struct linked *var)
 {
     if(var->before==NULL)
     {
         printf("\n%d",var->val);
         return;
     }
     else
     {
         printf("\n%d",var->val);
         printforward(var->before);
     }
    }

输出:

 Enter the number (-99) to quit:1
 Enter the number (-99) to quit:2
 Enter the number (-99) to quit:3
 Enter the number (-99) to quit:4
 Enter the number (-99) to quit:5
 Enter the number (-99) to quit:6
 Enter the number (-99) to quit:7
 Enter the number (-99) to quit:8
 Enter the number (-99) to quit:9
 Enter the number (-99) to quit:0
 Enter the number (-99) to quit:-99

Forward:

1
2
3
4
5
6
7
8
9
0
Backward:

0
9
0
Process returned 0 (0x0)   execution time : 22.297 s
Press any key to continue.

预期输出:

Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:6
Enter the number (-99) to quit:7
Enter the number (-99) to quit:8
Enter the number (-99) to quit:9
Enter the number (-99) to quit:0
Enter the number (-99) to quit:-99

Forward:

1 
2
3
4
5
6
7
8
9
0
Backward:
0
9
8
7
6
5
4
3
2
1

让我知道代码中有什么问题,我正在用c语言学习链表,我想为双向链表编写代码,但是我在上述问题中有一个逻辑错误,我不清楚为什么上面的代码无法正常工作,因此请您消除疑问。

2 个答案:

答案 0 :(得分:1)

为什么要递归地执行此操作,我不知道,但是本着这种精神,请考虑一下。

  • 您要从printforward调用printbackward,这是没有道理的。
  • 您的演员表无济于事,实际上,它们掩盖了实际的问题。 Read here for why
  • 中的内存分配,也不要求类似指针或无效指针的强制转换。

所有这四个操作都可以递归完成,实际上,您甚至不需要“ before”指针,但我还是保留了它。您可以:

  • 建立您的列表
  • 预先打印您的列表
  • 向后打印您的列表
  • 清理您的列表

...全部使用递归(我将为什么保留为另一个问题)。


代码

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

struct linked
{
    int val;
    struct linked *before;
    struct linked *next;
};

struct linked *get(struct linked *before);
void printforward(struct linked const *var);
void printbackward(struct linked const *var);
void cleanup(struct linked *lst);

int main()
{
    struct linked *ptr = get(NULL);

    printf("Forward: ");
    printforward(ptr);
    fputc('\n', stdout);

    printf("Backward: ");
    printbackward(ptr);
    fputc('\n', stdout);

    cleanup(ptr);
}


struct linked *get(struct linked *before)
{
    printf("Enter the number (-99) to quit:");
    int value = -99;
    if (scanf("%d", &value) != 1 || value == -99)
        return NULL;

    struct linked *p = malloc(sizeof *p);
    if (p != NULL)
    {
        p->before = before;
        p->val = value;
        p->next = get(p);
    }
    return p;
}

void printforward(struct linked const *var)
{
    if (var)
    {
        printf("%d ", var->val);
        printforward(var->next);
    }
}

void printbackward(struct linked const *var)
{
    if (var)
    {
        printbackward(var->next);
        printf("%d ", var->val);
    }
}

void cleanup(struct linked *lst)
{
    if (!lst)
        return;

    cleanup(lst->next);
    free(lst);
}

控制台

Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:-99
Forward: 1 2 3 4 5
Backward: 5 4 3 2 1

答案 1 :(得分:0)

这是一个简单的双链表构造示例,上面带有基本操作,例如节点的添加和插入以及双向迭代。 基本上,节点是简单结构,在列表中的上一个和下一个节点上具有两个指针,并且简单数据变量采用整数形式。操作的关键是对节点中的上一个下一个指针和列表结构中的倒数第二个指针进行适当的操作。

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>


typedef struct tagNode
{
   int data;
   struct tagNode *next; /* pointer to previous node */
   struct tagNode *prev; /* pointe to next node */
} NODE;

/* head of list */
typedef struct tagList
{
    NODE *head;   /* pointer to first node in list */
    NODE *last;   /* pointer to last node in list */
} LIST;



/* func proto's */
NODE* create_node(int data);
NODE* insert_node(LIST* l,NODE *p);
NODE* add_node(LIST* l, NODE *p);
void print_node(NODE *p);


int main()
{
  LIST list = {NULL,NULL}; /* init list with NULLs */

    /* add some nodes to list */
    for( int i = 0; i < 6 ; i++ )
    {
       add_node(&list, create_node(i*5) );
    }

    /* print forward */
    printf("Forward:\n");
    NODE* p = list.head;
    for( ;p != NULL; p = p->next )
    {
       print_node(p);
    }

    /* print backward */
    printf("Backward\n");
    for(p = list.last;p != NULL; p = p->prev )
    {
       print_node(p);
    }


    /* insert some nodes */
    printf("Insert some nodes\n");
    insert_node(&list, create_node(33));
    insert_node(&list, create_node(23));
    insert_node(&list, create_node(13));

    /* print forward -list after inserts */
    printf("Print forward after insert\n");
    for(p = list.head; p != NULL; p = p->next )
    {
       print_node(p);
    }
    /* print backward */
    printf("print backward after insert\n");
    for( p = list.last;p != NULL; p = p->prev )
    {
       print_node(p);
    }


}

NODE* create_node(int data )
{
  NODE *p = malloc(sizeof(NODE));
  p->next = NULL;
  p->prev = NULL;
  p->data = data;
}

/* add node to end of list */
NODE* add_node(LIST* list, NODE* p)
{
  if(list->last == NULL )
  {
     printf("add first node  into list\n");
     list->last = p;
     list->head = p;
     p->next = NULL;
     p->prev = NULL;
  }
  else
  {
    printf("add num %d\n", p->data);
    list->last->next = p;
    p->prev = list->last;
    list->last = p;
    p->next = NULL; /* terminate list */
  }
  return list->last;
}

void print_node(NODE *p)
{
    printf("node data: %d\n",p->data);
}

/* insert a node into list,
 * position depends on value of data
 **/
NODE* insert_node(LIST* l, NODE *q)
{
    /* scan list ... */
    for( NODE* p = l->head;p != NULL; p = p->next )
    {
       if( p->next != NULL )
       {
          if( p->next->data > q->data )
          {
            q->next = p->next;
            q->prev = p;
            p->next->prev = q;
            p->next = q;
            return q;  /* indicate success */
          }
       }
    }
    return NULL;  /* indicate failure */
}