C语言 使用循环将输入到链表中



我制作这个程序是为了学习链表,因为我刚刚开始使用它们。该程序在语句"输入农药数量"(这是学校作业(后立即终止。另外,我不确定如何将列表的长度限制为用户输入的大小。

#include<stdio.h>
struct plants{
    int val;
    struct plants *next;
};
void printlist();
int main(){
    struct plants* head = NULL;
    struct plants* current= head;
    head = malloc(sizeof(struct plants));
    int counter,size;
    printf("Enter the number of plantsn");
    scanf("%d",&size);
    printf("Enter the amount of pesticide each plant has.n");
    while(current!=NULL){
        scanf("%d",current->val);
        current= current->next;
    }
    return 0;
}
#include<stdio.h>
#include<malloc.h>
int main()
{
    int count = 0;
    int size = 0;        
    printf("Enter the number of plantsn");
    scanf("%d",&size);
    printf("Enter the amount of pesticide each plant has.n");

您必须为 while 循环中的每个节点分配内存。如果要在列表末尾添加新节点,请注意列表末尾的指针指向列表末尾的指针。除此之外,您必须将要读取的值的地址传递给scanf

    struct plants * head = NULL;
    struct plants ** current = &head; // current refers there, where next node has to be placed
    while( count < size ) // do it for "size" nodes
    {
        *current  = malloc(sizeof(struct plants)); // allocate memory for the node right to target 
        scanf( "%d", &((*current)->val));          // read the data
        (*current)->next = NULL;                   // node is last node in list, so its successor is NULL
        current = &((*current)->next);             // step on forward
        count ++;                                  // increment number of nodes
    }

请注意,由于current的类型struct plants **此代码将新节点置于列表的第一个元素head,并为列表的所有其他节点(*current)->next

列表的头部添加新节点会更容易:

    struct plants * head = NULL; // init head with NULL (this becomes end of the list)
    while( count < size ) // do it for "size" nodes
    {
        struct plants * current = malloc(sizeof(struct plants)); // allocate memory for the node 
        scanf( "%d", &current->val);       // read the data
        current->next = head;              // successor of node is head
        head = current;                    // new node is head of list
        count ++;                          // increment number of nodes
    }

这将打印您的列表:

    struct plants *temp = head;
    while( temp != NULL )
    {
        printf( "%d ", temp->val );
        temp = temp->next;
    }

不要忘记在程序结束时释放列表:

    while ( head != NULL )
    {
        struct plants *next = head->next;
        free( head );
        head = next; 
    }
    return 0;
}
struct plants* head = malloc(sizeof(struct plants));//declare an initialize in memory
struct plants* current= head;
int i = 0;
struct plants* aux = NULL;
while(i ++ < size)
{
    aux = malloc(sizeof(struct plants)); // used for new values
    scanf("%d", aux->val);
    aux->next = NULL;
    current->next = aux;
    current = aux;
}

循环,而您的数量少于用户所需的数量。使用辅助节点。读取该值,将 aux 节点的current下一个位置设置为 null,将 current 节点设置为 aux,以确保您位于列表中的最后一个节点。

很多事情需要更正,请检查下面的代码。 对于初学者来说应该很容易理解。为了便于理解,我没有更改代码的格式

#include<stdio.h>
#include<stdlib.h>
struct plants{
    int val;
    struct plants *next;
};
void printlist();
int main(){
    int i;
    struct plants* head = NULL,*temp=NULL;
    struct plants* current= head;
    int counter,size;
    printf("Enter the number of plantsn"); 
    scanf("%d",&size);  
    printf("Enter the amount of pesticide each plant has.n");
    for(i=0;i<size;i++){
        temp = malloc(sizeof(struct plants));
        scanf("%d",&temp->val);
        temp->next=NULL;
        if(head==NULL)
        {
            head=temp;
            current=head;
        }
        else
        {   
            current->next=temp;
            current=temp;
        }
    }
    current = head;
    while(current!=NULL){
        printf("%dt",current->val);
        current= current->next;
    }
}

相关内容

  • 没有找到相关文章

最新更新