线性链表的建立与显示

来源:互联网 发布:重生之星际淘宝主 bl 编辑:程序博客网 时间:2024/05/20 18:46
#include<stdio.h>#include<malloc.h>typedef int ElemType;typedef struct Node{ElemType data;struct Node*next;}LNode;void createLinkList(LNode*head);void displayLinkList(LNode*head);int main(){LNode*head;head=(LNode*)malloc(sizeof(LNode));head->next=NULL;createLinkList(head);displayLinkList(head);}void createLinkList(LNode*head){LNode*p,*rear=head;ElemType x;printf("输入数据元素x,当x=0时退出:");scanf("%d",&x);while(x!=0){p=(LNode*)malloc(sizeof(LNode));p->data=x;p->next=NULL;rear->next=p;rear=p;scanf("%d",&x);}}void displayLinkList(LNode*head){LNode*p=head->next;printf("线性链表:");while(p!=NULL){printf("%d ",p->data);p=p->next;}printf("\n");}

0 0
原创粉丝点击