无法在c中打印链表

时间:2017-10-12 18:02:44

标签: c linked-list

完成代码后,我无法将链接列表打印出来。我只是学习指针,所以问题最有可能发生。我也不确定我是否使用了正确类型的循环来执行此操作。

def onchange_data(self, cr, uid, vals, ids, context=None):
    res =  super(stock_picking, self).create(cr, uid, vals, context=context)
    stocks_picking_onchange = self.browse(cr, uid)
    products = []

2 个答案:

答案 0 :(得分:0)

看起来你需要调试你的代码。到目前为止,我发现了很多东西。

第39行newptr-> next == NULL应为newptr->next = NULL
使用double equals比较两个值,看起来您需要将Null指定为newptr->next

第64行printf("%d\n %c\n", head.count, newptr);您无法打印newptr,因为它只是您的"节点"的位置。你想要打印的是节点数据成员。

newptr->fld1(你的int值)

newptr->fld2(你的char值)

也是为了什么目的 第30行printf("in");只是打印"在"在您接受输入后。如果你保留第30行,我建议你添加一个换行符printf("in\n");,因为它会在你以后打印节点时弄乱你的输出格式。

完成上面提到的修复后,您的代码实际上会打印"某些内容"当你创建新节点时。当您尝试打印节点时,我认为您正在第64行挣扎。解决这个问题,我认为你会做得更好。

答案 1 :(得分:0)

请在评论部分的下方代码中查看您的错误。使用以下更正的代码并享受: -

#include <stdlib.h>

typedef struct node {
int fld1;
char fld2;
struct node *next;

}node;

int main(void) {
struct head {
    struct node *first;
    int count;
}   head;
int x;
char y;
node *newptr = NULL;
node *currentptr = NULL;
/* node *previousptr; *//* this pointer is not required*/
head.first = NULL;
head.count = 0;
int zz = 1;
do {
    printf("Enter a value between 1 and 10: ");
    scanf(" %d", &x);
    printf("Enter a letter: ");
    scanf(" %c", &y);
    printf("in");

    newptr = (node*)malloc(sizeof(node));
    newptr->fld1 = x;
    newptr->fld2 = y;
    newptr->next = NULL;/* assign it only in one place*/

    if (head.first == NULL) {
        head.first = newptr;
        head.count++;
        currentptr = head.first;
        /* newptr->next = NULL; */ /* it should be single assignment */
    }
    else {
        /* currentptr = head.first; */ /* you need this statement inside above if condition because currentptr should alwasy pointing to the latest node*/
        if (currentptr->next == NULL) {
            head.count++;
            currentptr->next = newptr;
            /* newptr->next = NULL; */
        }
        else {
            currentptr = currentptr->next; /* every iteration this pointer will pointing to the last node*/
            /* int i; */
            /* for (i = 1; i<head.count; i++) {*/ /* why you need this for loop here??? */
                if (currentptr->next == NULL) {/* is last node next is NULL - yes true, it will always true*/
                    currentptr->next = newptr;
                }
                else {
                    currentptr = currentptr->next;/* this statement will never execute why because currentptr alwasy pointing to the last node */
                }
                head.count++;
            }
        }
        /* printf("%d\n %c\n", head.count, newptr); */ /* what you want to print here just think */
} while (x != 99); 
/* your list has been created */
/* now you need to print your list here */
currentptr = head.first; /* first pointing to the first node*/
while (currentptr != NULL) {/*iterate the list till last node and print value of each node */
    printf("fdl1 =%d  and fdl2 =%c\n", currentptr->fld1, currentptr->fld2);
    currentptr = currentptr->next;
}

}