本文介绍了链表头总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我们必须创建一个链表实验室分配。我写的方法来实现这一目标。我希望能够打印出我的链接列表时,我测试。我有一个应该在遍历所有节点的while循环,但测试条件总是失败,我想不出为什么。我把测试用例,看看每当我推一个节点到列表中,如果新的头为空。这里是code为我的链表:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括list.h结构叶子节点{
字符*字;
诠释计数;
INT线;
结构叶子节点*接下来的;
};结构叶子节点*头= NULL;结构叶子节点* newNode(字符*字,诠释行){
结构叶子节点* tempnode;
字符*新的=字;
tempnode =(结构叶子节点*)malloc的(的sizeof(结构叶子节点));
tempnode->字=新的;
tempnode->计数= 1;
tempnode->行=行;
返回tempnode;
}无效pushNode(结构叶子节点**头,结构叶子节点*节点){
如果(头== NULL){
    头=节点;
    头= nodeGetNext(头);
    头= NULL;
}
其他{
    于节点>接下来=头;
    节点= nodeGetNext(节点);
    节点=头;
}
}结构叶子节点* nodeGetNext(结构叶子节点*节点){
返回于节点>接下来,
}字符* nodeGetWord(结构叶子节点*节点){
返回:于节点GT;字;
}诠释主(){
结构叶子节点*一个;
结构叶子节点* B;
结构叶子节点* C;
结构叶子节点* D;
结构叶子节点* E;
一个= newNode(你好,0);
B = newNode(的Bonjour,1);
C = newNode(霍拉,2);
D = newNode(Bonjourno,3);
E = newNode(你好,4);
pushNode(头,);
如果(头== NULL)
    输出(是);
pushNode(头,B);
如果(头== NULL)
    输出(是);
pushNode(头,C);
如果(头== NULL)
    输出(是);
pushNode(头,D);
如果(头== NULL)
    输出(是);
pushNode(头,E);
如果(头== NULL)
    输出(是);
的printList();返回0;
}无效的printList(){
的printf(你好\\ n);
结构叶子节点* currentnode;currentnode =头;而(currentnode!= NULL){
    的printf(你好);
    的printf(%S:\\ n,nodeGetWord(currentnode));
    currentnode = nodeGetNext(currentnode);
}
}


解决方案

pushNode()你这样做:头= NULL; 是一个指针的指针...

和包装这一切......就下运行,头部为NULL再次.....

I have a lab assignment where we have to create a linked list. I have written methods to achieve this. I want to be able to print out my linked list when I am testing it. I have a while loop that is supposed to traverse over all of the nodes, but the test condition always fails and I can't figure out why. I put test cases in to see if whenever I push a node onto the list, if the new head is null. Here is the code for my linked list:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"

struct lnode {
char* word;
int count;
int line;
struct lnode* next;
};

struct lnode* head = NULL;

struct lnode* newNode(char *word, int line) {
struct lnode* tempnode;
char* new = word;
tempnode = (struct lnode *)malloc(sizeof(struct lnode));
tempnode->word = new;
tempnode->count = 1;
tempnode->line = line;
return tempnode;
}

void pushNode(struct lnode** head, struct lnode* node) {
if(head == NULL) {
    head = node;
    head = nodeGetNext(head);
    head = NULL;
}
else {
    node->next = head;
    node = nodeGetNext(node);
    node = head;
}
}

struct lnode* nodeGetNext(struct lnode* node) {
return node->next;
}

char* nodeGetWord(struct lnode* node) {
return node->word;
}

int main() {
struct lnode* a;
struct lnode* b;
struct lnode* c;
struct lnode* d;
struct lnode* e;
a = newNode("Hello", 0);
b = newNode("Bonjour", 1);
c = newNode("Hola", 2);
d = newNode("Bonjourno", 3);
e = newNode("Hallo", 4);
pushNode(head, a);
if(head == NULL)
    printf("YES");
pushNode(head, b);
if(head == NULL)
    printf("YES");
pushNode(head, c);
if(head == NULL)
    printf("YES");
pushNode(head, d);
if(head == NULL)
    printf("YES");
pushNode(head, e);
if(head == NULL)
    printf("YES");
printList();

return 0;
}

void printList() {
printf("Hello\n");
struct lnode *currentnode;

currentnode = head;

while (currentnode != NULL) {
    printf("Hello");
    printf("%s:\n",nodeGetWord(currentnode));
    currentnode = nodeGetNext(currentnode);
}
}
解决方案

in pushNode() you do: head = NULL;. head is a pointer to a pointer...

And to wrap it all up... on the next run, head is NULL again.....

这篇关于链表头总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:12