首页 > 其他 > 详细

单链表

时间:2015-07-13 12:00:22      阅读:186      评论:0      收藏:0      [点我收藏+]
#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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!