标签:
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef int ElemType; /*==================单链表的结构===================*/ typedef struct Node{ ElemType element; struct Node *next; }Node; /*=================单链表的初始化==================*/ void initList(Node **pNode){ *pNode=NULL; } /*================链表的创建=======================*/ Node *creatList(Node *pHead){ Node *p1; Node *p2; p1=p2=(Node *)malloc(sizeof(Node)); //申请内存空间(新的节点) memset(p1,0,sizeof(Node)); scanf("%d",&p1->element); p1->next=NULL; while(p1->element!=EOF){ if(pHead==NULL) pHead=p1; else p2->next=p1; p2=p1; p1=(Node *)malloc(sizeof(Node)); memset(p1,0,sizeof(Node)); scanf("%d",&p1->element); p1->next=NULL; } return pHead; } /*=================链表的遍历======================*/ void printList(Node *pHead){ if(NULL==pHead) printf("链表为空"); else{ while(pHead!=NULL){ printf("%d ",pHead->element); pHead=pHead->next; } } printf("\n"); } int main(){ Node *pList=NULL; initList(&pList); pList=creatList(pList); printList(pList); return 0; }
标签:
原文地址:http://www.cnblogs.com/sky-z/p/4642264.html