Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我无法在单循环链表中正确显示或插入元素_C_Data Structures_Linked List - Fatal编程技术网

C 我无法在单循环链表中正确显示或插入元素

C 我无法在单循环链表中正确显示或插入元素,c,data-structures,linked-list,C,Data Structures,Linked List,我无法插入并显示在单循环链表中,它只插入第一个元素,并且具有显示功能 void create(){ 结构节点*temp,*p; temp=(结构节点*)malloc(sizeof(结构节点)); printf(“输入节点数据:”); scanf(“%d”和(临时->数据)); temp->next=NULL; if(root==NULL){ 根=p=温度; } 否则{ 而(p->next!=根){ p->next=温度; p=温度; p->next=root; } } } void displ

我无法插入并显示在单循环链表中,它只插入第一个元素,并且具有显示功能

void create(){
结构节点*temp,*p;
temp=(结构节点*)malloc(sizeof(结构节点));
printf(“输入节点数据:”);
scanf(“%d”和(临时->数据));
temp->next=NULL;
if(root==NULL){
根=p=温度;
}
否则{
而(p->next!=根){
p->next=温度;
p=温度;
p->next=root;
}
}
}
void display(){
结构节点*temp;
if(root==NULL){
printf(“列表为空\n”);
}
否则{
温度=根;
while(临时->下一步!=根){
printf(“%d”,临时->数据);
温度=温度->下一步;
}
printf(“%d”,临时->下一步);
}
}

创建
功能的这一部分中:

        while(p->next!=root) {      
            p->next = temp;
            p = temp;
            p->next = root;
        }
变量
p
未初始化,因此
p->next
是无效的解引用

更改如下:

        p = root;
        while(p->next!=root) {      
            p->next = temp;
            p = temp;
            p->next = root;
        }
更正未初始化的变量。代码仍然不正确,因为它会丢弃列表中的现有元素,因此列表只包含新元素

以下内容将其更改为查找列表的最后一个元素,然后将新元素附加到最后一个元素:

        p = root;
        while(p->next!=root) {      
            p = p->next;
        }
        p->next = temp;
然后需要将新元素(现在是最后一个元素)链接到第一个元素以完成圆:

        temp->next = root;
对于列表为空(即
root==NULL
)的情况,在使
root
指向新节点(即
root=temp;
)后,还需要使新节点指向自身,因为它也是第一个节点:

        temp->next = temp;
或:

temp->next=root语句可以移动到
if
else
语句之后,因为它位于两个分支的末尾

下面是一个稍微清理过的
create
函数版本。我没有为
malloc
scanf
调用的结果添加任何错误检查,因为问答的主要焦点与循环链表处理有关:

void create(void) {
    struct node *temp , *p;
    temp = (struct node*)malloc(sizeof(struct node));
    printf("Enter node data :");
    fflush(stdout);
    scanf("%d",&(temp->data));
    if(root == NULL) {
        /* list is empty so new node will become both first and last node */
        root = temp;
    }
    else {
        /* find the current last node */
        for (p = root; p->next != root; p = p->next)
            ;
        /* append the new node so it will become last */
        p->next = temp;
    }
    /* link the new, last node to the first node */
    temp->next = root;
}
现有的
display
功能现在应该可以工作了,但是可以使用
do
来简化它,而
循环用于非空的情况:

void display(void) {
    struct node *temp;
    if(root == NULL) {
        printf("List is Empty\n");
    }
    else {
        temp = root;
        do {
            printf("%d ",temp->data);
            temp = temp->next;
        } while (temp!=root);
        printf("\n");
    }
}
它没有那么简单,仍然有两条
printf
语句(尽管最后一条可以更改为
putchar('\n');
)。它还会在换行符之前打印一个空格,因此您可能希望单独处理列表的最后一个元素:

void display(void) {
    struct node *temp;
    if(root == NULL) {
        printf("List is Empty\n");
    }
    else {
        for (temp = root; temp->next != root; temp = temp->next) {
            printf("%d ",temp->data);
        }
        printf("%d\n", temp->data);
    }
}

等待10分钟,我将在
create
中解决此问题,
p
root!=空
。不幸的是,
p
仅在这种情况下使用,具有未定义的值。请删除代码好吗?这样我才能完全理解你,伙计!
void display(void) {
    struct node *temp;
    if(root == NULL) {
        printf("List is Empty\n");
    }
    else {
        for (temp = root; temp->next != root; temp = temp->next) {
            printf("%d ",temp->data);
        }
        printf("%d\n", temp->data);
    }
}