链表的基本操作C语言实现

链表的基本操作

写给自己平时复习和使用的,后面会写的更加详细一点

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

struct link *AppendNode(struct link *head);   //从链表尾部增加节点
void DisplayNode(struct link *head); //展示链表
struct link *DeleteNode(struct link *head,int nodedata);  //删除链表节点
struct link *InsertNode(struct link *head,int nodedata);  //按照大小关系插入节点
void DeleteMemory(struct link *head);  //释放链表的内存空间

struct link
{
    int data;
    struct link *next;
};
int main()
{
    int i=0;
    char c=0;
    struct link *head=NULL;
    scanf(" %c",&c);
    while(c=='y'||c=='Y')
    {
        printf("&&&\n");
        head=AppendNode(head);
        DisplayNode(head);
        printf("Do you want to continue ?\n");
        scanf(" %c",&c);
        i++;
    }
    printf("%d Node have been appended \n",i);
    head=DeleteNode(head,3); //删除节点测试
    DisplayNode(head);
    head=InsertNode(head,3);   // 插入节点测试
    DisplayNode(head);
    DeleteMemory(head);
}

struct link *AppendNode(struct link *head)
{
    struct link *p=NULL , *pr =head;
    int data;
    p=(struct link *)malloc(sizeof(struct link));
    if(p==NULL)
    {
        printf("ERROR!\n");
        exit(0);
    }
    if(head==NULL)
    {
        head=p;
    }
    else
    {
        while(pr->next!=NULL)
        {
            pr=pr->next;
        }
        pr->next=p;
    }
    printf("please input you data \n");
    scanf("%d",&data);
    p->data=data;
    p->next=NULL;
    return head;
}

void DisplayNode(struct link *head)
{
    struct link *p=head;
    int j=1;
    while(p!=NULL)
    {
        printf("%5d%10d\n",j,p->data);
        p=p->next;
        j++;
    }
}

void DeleteMemory(struct link *head)
{
    struct link *p=head,*pr=NULL;
    while(p!=NULL)
    {
        pr=p;
        p=p->next;
        free(pr);
    }
}

struct link *DeleteNode(struct link *head,int nodedata)
{
    struct link *p=head,*pr=head;
    if(head==NULL)
    {
        printf("Linked Table is empty !\n");
        return head;
    }
    while(nodedata!=p->data && p->next !=NULL)
    {
        pr=p;
        p=p->next;
    }
    if(nodedata==p->data)
    {
        if(p==head)
        {
            head=p->next;
        }
        else
        {
            pr->next=p->next;
        }
        free(p);
    }
    else
        printf("the node has not been found\n");
    return head;
}

struct link *InsertNode(struct link *head,int nodedata)
{
    struct link *pr=head,*p=head,*temp=NULL;
    p=(struct link *)malloc(sizeof(struct link));
    if(p==NULL)
    {
        printf("ERROR\n");
        exit(0);
    }
    p->next=NULL;
    p->data=nodedata;
    if(head==NULL)
    {
        head=p;
    }
    else
    {
        while(pr->data<nodedata&&pr->next!=NULL)
        {
            temp=pr;
            pr=pr->next;
        }
        if(pr->data>=nodedata)
        {
            if(pr==head)
            {
                p->next=head;
                head=p;
            }
            else
            {
                pr=temp;
                p->next=pr->next;
                pr->next=p;
            }
        }
        else
        {
            pr->next=p;
        }
    }
    return head;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值