简体   繁体   English

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

[英]How can i solve the logical error in following code

I Have a Problem in following code我在以下代码中有问题

#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);
     }
    }

output: output:

 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.

Expected output:预期 output:

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

Let me know What's problem in the code,I am learning linked list in c language,I want to write a code for double way linked list But I a have a logical error in above problem,I did not get clear idea Why The above code did not work,so I am request to clear the doubt.让我知道代码有什么问题,我正在学习 c 语言的链表,我想写一个双向链表的代码但是我在上面的问题中有一个逻辑错误,我不清楚为什么上面的代码没用,所以我请求消除疑问。

Why you're doing this with recursion, I have no idea, but in that spirit, consider this.为什么你用递归来做这件事,我不知道,但本着这种精神,考虑一下。

  • You're invoking printforward from printbackward , which makes no sense.您正在从printbackward调用printforward ,这是没有意义的。
  • Your casts are not helping, and in fact, they're masking the real problems.你的演员没有帮助,事实上,他们掩盖了真正的问题。 Memory allocations, nor like-pointer or to/from void-pointer casting is required, nor recommended, in C.在 C 中,Memory 分配、类似指针或指向/来自 void-pointer 的强制转换不是必需的,也不推荐使用。 Read here for why 阅读此处了解原因

All four operations can be done recursively, and in fact, you don't even need the "before" pointer but I kept it nonetheless.所有四个操作都可以递归完成,事实上,你甚至不需要“before”指针,但我还是保留了它。 You can:你可以:

  • Build your list建立你的清单
  • Forward-print your list转发打印您的清单
  • Backward-print your list向后打印您的清单
  • Cleanup your list清理你的清单

... all using recursion (I leave why you would want to as a different issue). ...全部使用递归(我将您为什么想要作为一个不同的问题留下)。


Code代码

#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);
}

Console安慰

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

here is an example of a simple double linked list construction with basic operation on it, such as addition and insertion of a node, and iteration in both directions.这是一个简单的双链表构造示例,其中包含基本操作,例如节点的添加和插入,以及双向迭代。 Basically, node is simple structure with two pointers on previous and next node in list, and simple data variable in form of an integer.基本上,节点是简单的结构,有两个指针指向列表中的前一个节点和下一个节点,以及 integer 形式的简单数据变量。 Key point on operations is proper manipulation of prev-next pointers in a node and head-last pointers in list structure.操作的关键点是正确操作节点中的 prev-next 指针和列表结构中的 head-last 指针。

#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 */
}

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

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