使用循环将输入输入到链表中

时间:2016-02-14 12:53:48

标签: c linked-list

我制作了这个程序来了解链接列表,因为我刚开始使用它们。该程序在“输入农药数量”(这是一项学校作业)声明后立即终止。另外,我不确定如何将列表的长度限制为用户输入的大小。

#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 plants\n");

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

3 个答案:

答案 0 :(得分:1)

#include<stdio.h>
#include<malloc.h>

int main()
{
    int count = 0;
    int size = 0;        
    printf("Enter the number of plants\n");
    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;
}

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

UIApplication.sharedApplication().cancelAllLocalNotifications()

答案 1 :(得分: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;
}
当您的用户数量少于数量时,

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

答案 2 :(得分:0)

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

#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 plants\n"); 
    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("%d\t",current->val);
        current= current->next;
    }
}