带头结点的单向链表的基本操作


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

struct node
{
    int num;
    struct node * next;
};
typedef struct node Node;
typedef struct node * Link;

void create_node(Link * new_node)
{
    *new_node=(Link)malloc(sizeof(Node));
}

void create_head(Link *head)
{
    *head=(Link)malloc(sizeof(Node));
    (*head)->next=NULL;
}//带头结点的单向链表初始化

void insert_node_head(Link head,Link new_node)
{
    Link p=head;
    new_node->next=p->next;
    p->next=new_node;
}//头插法

void insert_node_tail(Link head,Link new_node)
{
    Link p = head;
    while(p->next != NULL)
    {
        p=p->next;
    }
    if(p->next == NULL)
     {
         p->next=new_node;
         new_node->next=NULL;
     }
}//尾插法,插在最后结点后面,与无头结点的单向链表相比,不用判断首结点是否为空

void insert_node_mid_before(Link head,Link new_node,int insertlocation)
{
    Link p=head;
    if(p==NULL)
    {
        printf("the link is empty!\n");
    }
    else
    {    
       while(p->next!=NULL &&p->next->num!=insertlocation)
       {
         p=p->next;
       }
       if(p->next==NULL)
       {
           printf("no place no insert!\n");
       }
       else
       {
           new_node->next=p->next;
           p->next=new_node;
       }
    }
} 

void insert_node_mid_after(Link head,Link new_node,int insertlocation)
{
    Link p=head;
    if(p==NULL)
    {
        printf("the link is empty!\n");
    }
    else
    {
        while(p->next != NULL && p->num != insertlocation)
        {
            p=p->next;
        }
        if(p->next==NULL)
        {
            printf("no place to insert!\n");
        }
        else
        {
            new_node->next=p->next;
            p->next=new_node;
        }
    }
}

void delete_node(Link head,int deletenum)
{
    Link p,q;
    p=head;
    if(p==NULL)
    {
        printf("the link is empty!\n");
    }
    else
    {
        while(p->next!=NULL&&p->next->num!=deletenum)
        {
            q=p;
            p=p->next;
        }
        if(p->next==NULL)
        {
            printf("no num to delete!\n");
        }
        else
        {
            q=p->next;
            p->next=p->next->next;
            free(q);
        }
    }
}

void display(Link head)    
{
    Link p=head;
    if(p==NULL)
    {
        printf("the link is empty!\n");
    }
    while(p!=NULL)
    {
        printf("num=%d\n",p->num);
        p=p->next;
    }
}//回显函数

void release(Link *head)
{
    Link p=*head;
    if(p==NULL)
    {
        printf("the link is empty!\n");
    }
    while(p!=NULL)
    {
        *head=(*head)->next;
        free(p);
        p=*head;
    }
}//释放所有的结点

int main()
{
    Link head,new_node;
    int i;
    int insertlocation;
    int deletenum;
    create_head(&head);
    for(i=0;i<10;i++)
    {
        create_node(&new_node);
        new_node->num=i+1;
       // insert_node_head(head,new_node);
        insert_node_tail(head,new_node);
    }
    display(head);

    create_node(&new_node);
    printf("enter insert numbers:\n");
    scanf("%d",&new_node->num);
    printf("enter insertlocation:\n");
    scanf("%d",&insertlocation);
//    insert_node_mid_after(head,new_node,insertlocation);
    insert_node_mid_before(head,new_node,insertlocation);
    display(head);
    
    printf("enter deletenum:\n");
    scanf("%d",&deletenum);
    delete_node(head,deletenum);
    display(head);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值