单向链表例1

#include<stdio.h>
#include<stdlib.h>

typedef char datatype;
struct node 
{
    datatype data;
    struct node *next;
};
typedef struct node LNode;

LNode *AppendNode(LNode *head,datatype ch)
{
    LNode *p,*pr=head;
    p=(LNode*)malloc(sizeof(LNode));
    if(p==NULL)
    {
        printf("内存分配失败!\n");
        exit(0);
    }
    p->data=ch;
    if(head==NULL)
        head=p;
    else
    {
        while(pr->next!=NULL)
        {
            pr=pr->next;
        }
        pr->next=p;
    }
    p->next=NULL;
        return head;
}

void DeleteLink(struct node *head)
{
    struct node *current = head->next;
    struct node *temp = current;
    while (current != NULL)
    {
    temp = current;
    current = current->next;
    free(temp);
    }
    head->next = NULL;
}

void output(LNode *head)
{
    LNode *p;
    p=head->next;
    printf("The content of list:");
    if(head->next==NULL)
        printf("null");
    else
    {
        while(p)
        {
            printf("%c",p->data);
            p=p->next;
        }
    }
    putchar('\n');
}

int main()
{
    int n;
    int i;
    datatype ch;
    LNode *head=NULL;
    head=(LNode*)malloc(sizeof(LNode));
    if(head==NULL)
    {
        printf("内存分配失败!\n");
        exit(0);
    }
    head->next=NULL;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%c",&ch);
        head=AppendNode(head,ch);
    }
    output(head);
    DeleteLink(head);
    output(head);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值