链表的插入方法1(逆序插入,逆序输出)

作者在 2018-04-06 17:58:52 发布以下内容
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;


typedef struct Node //atention the 'struct'//链表结点定义

{
    ElemType data;
    struct Node* next;
} Node,*LinkList;


LinkList GreateFormHead() //建立链表函数

{
    Node *h=NULL, *s;   //定义一个结点指向空
    char c;
    while ((c=getchar()) != '$')
    {
        s = (Node*)malloc(sizeof(Node));
        s->data = c;//反向插入结点
        s->next = h;
        h = s;//h是指向第一个结点(单链表)
    }
    return h;
}
void print(Node *L)    //输出函数   *L是头结点
{
    for (; L; L=L->next)//新的一种for循环方式
        printf("%c ", L->data);
    printf("\n");
}


int main()
{
    LinkList L = GreateFormHead();//调用时只用函数名
    print(L);
    return 0;
}
默认分类 | 阅读 1622 次
文章评论,共0条
游客请输入验证码
浏览7990次
文章分类
文章归档