如何向链表中读入数据

    xiaoxiao2021-03-25  80

    #include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct LNode *PtrToLNode; struct LNode { ElementType Data; PtrToLNode Next; }; typedef PtrToLNode List; List Read() { int num; scanf("%d",&num); if(num==-1) { } List list=(List)malloc(sizeof(struct LNode)); //创建一个头结点 line20——line23 List last=list; list->Data=num; list->Next=NULL; scanf("%d",&num); while(num!=-1) { List node=(List)malloc(sizeof(struct LNode)); //创建一个节点node node->Data=num; node->Next=NULL; last->Next=node; // last(尾节点)的Next先找到node last=node; //然后last再指向node,所以node就是尾节点喽 scanf("%d",&num); } return list; } int Length(List L) { List p=L; if(L==NULL) { } int len=0; while(p!=NULL) { len++; p=p->Next; } return len; } int main() { List L = Read(); printf("%d\n", Length(L)); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-17478.html

    最新回复(0)