链表的一些基本操作,头插尾插,创建,删除,逆序,排序等等

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

typedef struct node
{
    int data;
    struct node *pNext;
}Node;

Node *create_list()
{
    Node *phead = (Node *)malloc(sizeof(Node));
    if(phead == NULL)
    {
        printf("分配内存失败,程序终止!\n");
        exit(-1);
    }
    phead->pNext = NULL;
    
    return phead;
}

Node *add_tail(Node *phead)
{
    int num = 0;
    int i = 0;
    int val = 0;
    
    Node *ptail = phead;
    ptail->pNext = NULL;
    
    printf("请输入想插入的节点数:\t");
    scanf("%d",&num);
    
    for(i = 0;i < num;i++)
    {
        printf("请输入第%d个插入的值:\t",i+1);
        scanf("%d",&val);
        
        Node *pnew = (Node *)malloc(sizeof(Node));
        if(pnew == NULL)
        {
            printf("分配内存失败,程序终止!\n");
            exit(-1);
        }
        
        pnew->data = val;
        
        ptail->pNext = pnew;
        pnew->pNext = NULL;
        ptail = pnew;
    }
    return phead;
}

Node *add_head(Node *phead)
{
    int num = 0;
    int i = 0;
    int val = 0;
        
    printf("请输入想插入的节点数:\t");
    scanf("%d",&num);
    
    for(i = 0;i < num;i++)
    {
        printf("请输入第%d个插入的值:\t",i+1);
        scanf("%d",&val);
        
        Node *pnew = (Node *)malloc(sizeof(Node));
        if(pnew == NULL)
        {
            printf("分配内存失败,程序终止!\n");
            exit(-1);
        }
        pnew->data = val;
        pnew->pNext = phead->pNext;
        phead->pNext = pnew;
    }
}

void traverse_list(Node *phead)
{
    printf("--------遍历链表--------\n");
    Node *ptr = phead;
    if(ptr->pNext == NULL)
    {
        printf("该链表为空!\n");
        return ;
    }
    
    while (ptr->pNext)
    {
        printf("%d\t",ptr->pNext->data);
        ptr = ptr->pNext;
    }
    
    printf("\n");
    
}

int is_Empty(Node *phead)
{
    if(phead->pNext == NULL)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int length_list(Node *phead)
{
    int len = 0;
    Node *ptr = phead;
    while (ptr->pNext)
    {
        ++len;
        ptr->pNext = ptr->pNext->pNext;
    }
    return len;
}

void insert_list(Node *phead)
{
    int pos = 0;
    int val = 0;
    
    Node *ptr = phead;
    
    if(pos < 0)
    {
        printf("pos error!\n");
        return ;
    }
    
    printf("请输入想要插入的位置:\t");
    scanf("%d",&pos);
    int count = 0;
    while(count < (pos - 1) && ptr != NULL)
    {
        ptr = ptr->pNext;
        ++count;
    }
    if(count > pos - 1 || ptr == NULL)
    {
        printf("pos error!\n");
        return ;
    }
    Node *pnew = (Node *)malloc(sizeof(Node));
    if(pnew == NULL)
    {
        printf("分配内存失败,程序终止!\n");
        return ;
    }
    
    printf("请输入想要插入的数据:\t");
    scanf("%d",&val);
    
    pnew->data = val;
    
    pnew->pNext = ptr->pNext;
    ptr->pNext = pnew;
}

void delete_list(Node *phead)
{
    int pos = 0;
    int val = 0;
    
    Node *ptr = phead;
    
    if(pos < 0)
    {
        printf("pos error!\n");
        return ;
    }
    
    printf("请输入想要删除的位置:\t");
    scanf("%d",&pos);
    int count = 0;
    while(count < (pos- 1) && ptr != NULL)
    {
        ptr = ptr->pNext;
        ++count;
    }
    if(count > pos - 1 || ptr->pNext == NULL)
    {
        printf("超出范围,无法删除!\n");
        return ;
    }
    printf("要删除的数据是:%d\n",ptr->pNext->data);
    
    ptr->pNext = ptr->pNext->pNext;
}

void sort_list(Node *phead)
{
    int temp = 0;
    
    Node *p,*q;
    
    for(p = phead->pNext ; p != NULL ; p = p->pNext)
    {

        for(q = p->pNext ; q != NULL ;q = q->pNext)
        {

            if(p->data < q->data)
            {
                temp = p->data;
                p->data = q->data;
                q->data = temp;
            }
        }
    }

}

void reverse_list(Node *phead)
{
    Node *ptr = phead->pNext;
    if (ptr != NULL)
    {
       reverse_list(ptr);
       printf("%d\t",ptr->data);
    }

}

int main()
{
    Node *phead = create_list();
    add_tail(phead);
    traverse_list(phead);
    
    // delete_list(phead);
    // traverse_list(phead);
    
    // int ret = is_Empty(phead);
    // printf("ret = %d\n",ret);
    insert_list(phead);
    traverse_list(phead);
    // delete_list(phead);
    sort_list(phead);
    traverse_list(phead);
    
    printf("---------逆序输出---------\n");
    reverse_list(phead);
    printf("\n");
    
    int ret_len = length_list(phead);
    printf("链表的长度:%d\n",ret_len);
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值